Ejemplo n.º 1
0
def main():
    global STORAGE

    arguments = parser.parse_args()

    basic_config(level=arguments.log_level,
                 log_format=arguments.log_format,
                 buffered=False)

    setproctitle(os.path.basename("[Master] %s" % sys.argv[0]))

    tcp_sock = bind_socket(address=arguments.tcp_listen,
                           port=arguments.tcp_port)

    pickle_sock = bind_socket(address=arguments.pickle_listen,
                              port=arguments.pickle_port)

    udp_sock = bind_socket(
        socket.AF_INET6 if ':' in arguments.udp_listen else socket.AF_INET,
        socket.SOCK_DGRAM,
        address=arguments.udp_listen,
        port=arguments.udp_port)

    test_path = os.path.join(arguments.storage, '.test')
    with open(test_path, 'ab+') as fp:
        assert fp.truncate(0) == 0, ("Truncating not supported on %r" %
                                     arguments.storage)

    forkme.fork(arguments.forks)

    setproctitle(os.path.basename("[Worker] %s" % sys.argv[0]))

    loop = new_event_loop(arguments.pool_size)
    loop.set_debug(arguments.debug)

    basic_config(level=arguments.log_level,
                 log_format=arguments.log_format,
                 buffered=True,
                 loop=loop)

    loop.create_task(amain(loop, tcp_sock, pickle_sock, udp_sock))
    loop.create_task(
        sender(arguments.carbon_proxy_url, arguments.carbon_proxy_secret))

    STORAGE = Storage(os.path.join(arguments.storage,
                                   "%03d.bin" % (forkme.get_id() or 0)),
                      loop=loop)

    try:
        loop.run_forever()
    finally:
        loop.stop()
        loop.run_until_complete(loop.shutdown_asyncgens())
Ejemplo n.º 2
0
    def factory(*args, **kwargs) -> PortSocket:
        sock = bind_socket(*args, address=localhost, port=0, **kwargs)
        port = sock.getsockname()[1]

        # Close socket after teardown
        request.addfinalizer(sock.close)

        return PortSocket(port=port, socket=sock)
Ejemplo n.º 3
0
 def _create_socket(self) -> None:
     self.socket = bind_socket(
         INET_AF,
         socket.SOCK_STREAM,
         address="localhost",
         port=0,
     )
     self.address = self.socket.getsockname()[:2]
Ejemplo n.º 4
0
 def _create_socket(self) -> None:
     path = mktemp(suffix=".sock", prefix="worker-")
     self.socket = bind_socket(
         socket.AF_UNIX,
         socket.SOCK_STREAM,
         address=path,
     )
     self.address = path
     chmod(path, 0o600)
Ejemplo n.º 5
0
def main():
    arguments = parser.parse_args()
    os.environ.clear()

    basic_config(
        level=arguments.log_level,
        log_format=arguments.log_format,
        buffered=False,
    )

    setproctitle(os.path.basename("[Master] %s" % sys.argv[0]))

    sock = bind_socket(address=arguments.http_address,
                       port=arguments.http_port)
    queue = deque()
    services = [
        API(
            sock=sock,
            password=arguments.http_password,
            login=arguments.http_login,
            queue=queue,
        ),
        Sender(
            jaeger_route=arguments.jaeger_route,
            interval=arguments.sender_interval,
            queue=queue,
        ),
    ]

    if arguments.user is not None:
        logging.info("Changing user to %r", arguments.user.pw_name)
        os.setgid(arguments.user.pw_gid)
        os.setuid(arguments.user.pw_uid)

    def run():
        setproctitle(os.path.basename("[Worker] %s" % sys.argv[0]))

        with entrypoint(*services,
                        pool_size=arguments.pool_size,
                        log_level=arguments.log_level,
                        log_format=arguments.log_format,
                        debug=arguments.debug) as loop:
            loop.run_forever()

    if arguments.forks:
        forklib.fork(arguments.forks, run, auto_restart=True)
    else:
        run()
Ejemplo n.º 6
0
def main():
    global SECRET

    arguments = parser.parse_args()
    os.environ.clear()

    basic_config(level=arguments.log_level,
                 log_format=arguments.log_format,
                 buffered=False)

    setproctitle(os.path.basename("[Master] %s" % sys.argv[0]))

    sock = bind_socket(address=arguments.http_address,
                       port=arguments.http_port)

    services = [
        API(
            secret=arguments.http_secret,
            sock=sock,
        ),
        Sender(
            routes=arguments.routes,
            interval=arguments.sender_interval,
        )
    ]

    if arguments.user is not None:
        logging.info('Changing user to %r', arguments.user.pw_name)
        os.setgid(arguments.user.pw_gid)
        os.setuid(arguments.user.pw_uid)

    def run():
        setproctitle(os.path.basename("[Worker] %s" % sys.argv[0]))

        with entrypoint(*services,
                        pool_size=arguments.pool_size,
                        log_level=arguments.log_level,
                        log_format=arguments.log_format) as loop:
            loop.set_debug(arguments.debug)
            loop.run_forever()

    if arguments.forks:
        forklib.fork(arguments.forks, run, auto_restart=True)
    else:
        run()
Ejemplo n.º 7
0
def main():
    global SECRET

    arguments = parser.parse_args()
    basic_config(level=arguments.log_level,
                 log_format=arguments.log_format,
                 buffered=False)

    setproctitle(os.path.basename("[Master] %s" % sys.argv[0]))

    sock = bind_socket(address=arguments.http_address,
                       port=arguments.http_port)

    forkme.fork(arguments.forks)

    setproctitle(os.path.basename("[Worker] %s" % sys.argv[0]))

    SECRET = arguments.http_secret

    loop = new_event_loop(arguments.pool_size)
    loop.set_debug(arguments.debug)

    app = make_app(arguments)
    app._debug = arguments.debug

    app.on_startup.append(setup_routes)
    app.on_startup.append(partial(setup_sender, arguments=arguments))

    log.info("Starting application http://%s:%d", arguments.http_address,
             arguments.http_port)

    basic_config(level=arguments.log_level,
                 log_format=arguments.log_format,
                 buffered=True,
                 loop=loop)

    try:
        run_app(sock=sock, loop=loop, app=app, print=log.debug)
    except GracefulExit:
        log.info("Exiting")
Ejemplo n.º 8
0
def test_bind_address(address, family, unused_tcp_port):
    sock = bind_socket(address=address, port=unused_tcp_port)

    assert isinstance(sock, socket.socket)
    assert sock.family == family
Ejemplo n.º 9
0
p.add_argument("--redis-url",
               default="127.0.0.1",
               help="Url for redis database",
               type=str)
p.add_argument("--host-url", required=True)

p.add_argument("--memory-tracer", action="store_true", default=False)
p.add_argument("--webhook-port", type=int, default=443)

if __name__ == "__main__":
    arguments = p.parse_args()

    loop = new_event_loop()
    bot, manager = loop.run_until_complete(init(arguments.redis_url))

    socket = bind_socket(address="0.0.0.0", port=80, proto_name="http")

    services = [
        TelegramWebhook(
            sock=socket,
            bot=bot,
            manager=manager,
            host=arguments.host_url,
            webhooks_port=arguments.webhook_port,
        ),
        UpdaterService(bot=bot, manager=manager),
        RavenSender(sentry_dsn=sentry_key),
    ]
    if arguments.memory_tracer:
        services.append(MemoryTracer(interval=60))