def __init__(self, **settings): options.sentry_dsn = 'http://*****:*****@127.0.0.1:{}/sentry'.format( settings['port']) bootstrap_logger('custom_logger', logging.DEBUG, False) super().__init__(**settings)
def run_server(app: FrontikApplication): """Starts Frontik server for an application""" try: if options.asyncio_task_threshold_sec is not None: slow_tasks_logger = bootstrap_logger('slow_tasks', logging.WARNING, use_json_formatter=False) import asyncio import reprlib reprlib.aRepr.maxother = 128 old_run = asyncio.Handle._run def run(self): start_time = self._loop.time() old_run(self) delta = self._loop.time() - start_time if delta >= options.asyncio_task_threshold_sec: slow_tasks_logger.warning('%s took %.2fms', self, delta * 1000) asyncio.Handle._run = run log.info('starting server on %s:%s', options.host, options.port) http_server = tornado.httpserver.HTTPServer(app, xheaders=options.xheaders) http_server.bind(options.port, options.host, reuse_port=options.reuse_port) http_server.start() io_loop = tornado.ioloop.IOLoop.current() if options.autoreload: tornado.autoreload.start(1000) def sigterm_handler(signum, frame): log.info('requested shutdown') log.info('shutting down server on %s:%d', options.host, options.port) io_loop.add_callback_from_signal(server_stop) signal.signal(signal.SIGTERM, signal.SIG_IGN) def ioloop_is_running(): return io_loop.asyncio_loop.is_running() def server_stop(): http_server.stop() if ioloop_is_running(): log.info('going down in %s seconds', options.stop_timeout) def ioloop_stop(): if ioloop_is_running(): log.info('stopping IOLoop') io_loop.stop() log.info('stopped') io_loop.add_timeout(time.time() + options.stop_timeout, ioloop_stop) signal.signal(signal.SIGTERM, sigterm_handler) except Exception: log.exception('failed to start Tornado application') sys.exit(1)
def __init__(self, **settings): options.sentry_dsn = 'http://*****:*****@127.0.0.1:{}/sentry'.format( settings['port']) bootstrap_logger('custom_logger', logging.DEBUG, False) super().__init__(**settings) try: from frontik.integrations.kafka import KafkaIntegration kafka_integration = next(i for i in self.available_integrations if isinstance(i, KafkaIntegration)) kafka_integration.kafka_producers = { 'infrastructure': TestKafkaProducer() } except Exception: pass
async def run_server(app: FrontikApplication, ioloop: BaseAsyncIOLoop, need_to_register_in_service_discovery): """Starts Frontik server for an application""" if options.asyncio_task_threshold_sec is not None: slow_tasks_logger = bootstrap_logger('slow_tasks', logging.WARNING, use_json_formatter=False) import asyncio import reprlib reprlib.aRepr.maxother = 256 wrap_handle_with_time_logging(asyncio.Handle, app, slow_tasks_logger) log.info('starting server on %s:%s', options.host, options.port) http_server = tornado.httpserver.HTTPServer(app, xheaders=options.xheaders) http_server.bind(options.port, options.host, reuse_port=options.reuse_port) http_server.start() if options.autoreload: tornado.autoreload.start(1000) def sigterm_handler(signum, frame): log.info('requested shutdown') log.info('shutting down server on %s:%d', options.host, options.port) ioloop.add_callback_from_signal(server_stop) def ioloop_is_running(): return ioloop.asyncio_loop.is_running() def server_stop(): ioloop.asyncio_loop.create_task( _deinit_app(app, ioloop, need_to_register_in_service_discovery)) http_server.stop() if ioloop_is_running(): log.info('going down in %s seconds', options.stop_timeout) def ioloop_stop(): if ioloop_is_running(): log.info('stopping IOLoop') ioloop.stop() log.info('stopped') ioloop.asyncio_loop.call_later(options.stop_timeout, ioloop_stop) signal.signal(signal.SIGTERM, sigterm_handler)
def run_server(app: FrontikApplication): """Starts Frontik server for an application""" try: if options.asyncio_task_threshold_sec is not None: slow_tasks_logger = bootstrap_logger('slow_tasks', logging.WARNING, use_json_formatter=False) import asyncio import reprlib reprlib.aRepr.maxother = 256 old_run = asyncio.Handle._run def run(self): start_time = self._loop.time() old_run(self) delta = self._loop.time() - start_time if delta >= options.asyncio_task_threshold_sec: slow_tasks_logger.warning('%s took %.2fms', self, delta * 1000) if options.asyncio_task_critical_threshold_sec and delta >= options.asyncio_task_critical_threshold_sec: request = get_request() or HTTPServerRequest( 'GET', '/asyncio_long_task_stub') sentry_logger = app.get_sentry_logger(request) sentry_logger.update_user_info(ip='127.0.0.1') if sentry_logger: slow_tasks_logger.warning('no sentry logger available') sentry_logger.capture_message( f'{self} took {(delta * 1000):.2f} ms', stack=True) asyncio.Handle._run = run log.info('starting server on %s:%s', options.host, options.port) http_server = tornado.httpserver.HTTPServer(app, xheaders=options.xheaders) http_server.bind(options.port, options.host, reuse_port=options.reuse_port) http_server.start() io_loop = tornado.ioloop.IOLoop.current() if options.autoreload: tornado.autoreload.start(1000) def sigterm_handler(signum, frame): log.info('requested shutdown') log.info('shutting down server on %s:%d', options.host, options.port) io_loop.add_callback_from_signal(server_stop) signal.signal(signal.SIGTERM, signal.SIG_IGN) def ioloop_is_running(): return io_loop.asyncio_loop.is_running() def server_stop(): deinit_futures = [ integration.deinitialize_app(app) for integration in app.available_integrations ] if deinit_futures: def await_deinit(future): if future.exception() is not None: log.error('failed to deinit, deinit returned: %s', future.exception()) io_loop.add_future(gen.multi(deinit_futures), await_deinit) http_server.stop() if ioloop_is_running(): log.info('going down in %s seconds', options.stop_timeout) def ioloop_stop(): if ioloop_is_running(): log.info('stopping IOLoop') io_loop.stop() log.info('stopped') io_loop.add_timeout(time.time() + options.stop_timeout, ioloop_stop) signal.signal(signal.SIGTERM, sigterm_handler) except Exception: log.exception('failed to start Tornado application') sys.exit(1)
def __init__(self, **settings): options.sentry_dsn = 'http://*****:*****@127.0.0.1:{}/sentry'.format(settings['port']) bootstrap_logger('custom_logger', logging.DEBUG, False) super().__init__(**settings)