Пример #1
0
def main(options):

    # all forks belong to the same happy family
    try:
        os.setpgrp()
    except OSError:
        print(
            "Failed to create process group - there is probably already one (daemontools).",
            file=sys.stderr)

    # Set a low (but not too low) open file limit to make
    # dispatch.update_valid_fds faster.
    # The runners will set the highest limit they can
    # before actually running any methods.
    r1, r2 = resource.getrlimit(resource.RLIMIT_NOFILE)
    r1 = min(r1, r2, 1024)
    resource.setrlimit(resource.RLIMIT_NOFILE, (r1, r2))

    # setup statmsg sink and tell address using ENV
    statmsg_rd, statmsg_wr = socket.socketpair(socket.AF_UNIX,
                                               socket.SOCK_DGRAM)
    os.environ['BD_STATUS_FD'] = str(statmsg_wr.fileno())

    def buf_up(fh, opt):
        sock = socket.fromfd(fh.fileno(), socket.AF_UNIX, socket.SOCK_DGRAM)
        sock.setsockopt(socket.SOL_SOCKET, opt, 256 * 1024)

    buf_up(statmsg_wr, socket.SO_SNDBUF)
    buf_up(statmsg_rd, socket.SO_RCVBUF)

    CONFIG = configfile.get_config(options.config, verbose=False)

    t = Thread(target=statmsg_sink,
               args=(CONFIG['logfilename'], statmsg_rd),
               name="statmsg sink")
    t.daemon = True
    t.start()

    # do all main-stuff, i.e. run server
    sys.stdout = autoflush.AutoFlush(sys.stdout)
    sys.stderr = autoflush.AutoFlush(sys.stderr)
    atexit.register(exitfunction)
    signal.signal(signal.SIGTERM, exitfunction)
    signal.signal(signal.SIGINT, exitfunction)

    signal.signal(signal.SIGUSR1, siginfo)
    signal.siginterrupt(signal.SIGUSR1, False)
    if hasattr(signal, 'SIGINFO'):
        signal.signal(signal.SIGINFO, siginfo)
        signal.siginterrupt(signal.SIGINFO, False)

    if options.port:
        server = ThreadedHTTPServer(('', options.port), XtdHandler)
        daemon_url = 'http://localhost:%d' % (options.port, )
    else:
        check_socket(options.socket)
        # We want the socket to be world writeable, protect it with dir permissions.
        u = os.umask(0)
        server = ThreadedUnixHTTPServer(options.socket, XtdHandler)
        os.umask(u)
        daemon_url = configfile.resolve_socket_url(options.socket)

    ctrl = control.Main(options, daemon_url)
    print()
    ctrl.print_workspaces()
    print()

    XtdHandler.ctrl = ctrl

    for n in ("result_directory", "source_directory", "urd"):
        print("%16s: %s" % (
            n.replace("_", " "),
            CONFIG.get(n),
        ))
    print()

    if options.port:
        serving_on = "port %d" % (options.port, )
    else:
        serving_on = options.socket
    print("Serving on %s\n" % (serving_on, ), file=sys.stderr)
    server.serve_forever()
Пример #2
0
def main(options):

    # all forks belong to the same happy family
    try:
        os.setpgrp()
    except OSError:
        print(
            "Failed to create process group - there is probably already one (daemontools).",
            file=sys.stderr)

    # increase number of open file per process
    r1, r2 = resource.getrlimit(resource.RLIMIT_NOFILE)
    resource.setrlimit(resource.RLIMIT_NOFILE, (r2, r2))
    print("DAEMON:  Set max number of open files to (%d, %d)" %
          resource.getrlimit(resource.RLIMIT_NOFILE))

    # setup statmsg sink and tell address using ENV
    statmsg_rd, statmsg_wr = socket.socketpair(socket.AF_UNIX,
                                               socket.SOCK_DGRAM)
    os.environ['BD_STATUS_FD'] = str(statmsg_wr.fileno())

    def buf_up(fh, opt):
        sock = socket.fromfd(fh.fileno(), socket.AF_UNIX, socket.SOCK_DGRAM)
        sock.setsockopt(socket.SOL_SOCKET, opt, 256 * 1024)

    buf_up(statmsg_wr, socket.SO_SNDBUF)
    buf_up(statmsg_rd, socket.SO_RCVBUF)

    CONFIG = configfile.get_config(options.config)

    t = Thread(target=statmsg_sink,
               args=(CONFIG['logfilename'], statmsg_rd),
               name="statmsg sink")
    t.daemon = True
    t.start()

    # do all main-stuff, i.e. run server
    sys.stdout = autoflush.AutoFlush(sys.stdout)
    sys.stderr = autoflush.AutoFlush(sys.stderr)
    atexit.register(exitfunction)
    signal.signal(signal.SIGTERM, exitfunction)
    signal.signal(signal.SIGINT, exitfunction)

    signal.signal(signal.SIGUSR1, siginfo)
    signal.siginterrupt(signal.SIGUSR1, False)
    if hasattr(signal, 'SIGINFO'):
        signal.signal(signal.SIGINFO, siginfo)
        signal.siginterrupt(signal.SIGINFO, False)

    if options.port:
        server = ThreadedHTTPServer(('', options.port), XtdHandler)
        daemon_url = 'http://localhost:%d' % (options.port, )
    else:
        check_socket(options.socket)
        # We want the socket to be world writeable, protect it with dir permissions.
        u = os.umask(0)
        server = ThreadedUnixHTTPServer(options.socket, XtdHandler)
        os.umask(u)
        daemon_url = configfile.resolve_socket_url(options.socket)

    ctrl = control.Main(options, daemon_url)
    print("DAEMON:  Available workspaces")
    for x in ctrl.list_workspaces():
        print("DAEMON:    %s" % x)
    print("DAEMON:  Current workspace is          \"%s\"" %
          ctrl.get_current_workspace())
    print("DAEMON:  Current remote workspaces are %s" % ', '.join(
        ['\"' + x + '\"' for x in ctrl.get_current_remote_workspaces()]))

    XtdHandler.ctrl = ctrl

    print("Start serving on port %s." % (options.port or options.socket, ),
          file=sys.stderr)
    print('-' * 79)
    print()
    server.serve_forever()