def handle(self, addrport, **options): # type: (str, **bool) -> None interactive_debug_listen() import django from tornado import httpserver, web try: addr, port = addrport.split(':') except ValueError: addr, port = '', addrport if not addr: addr = '127.0.0.1' if not port.isdigit(): raise CommandError("%r is not a valid port number." % (port, )) xheaders = options.get('xheaders', True) no_keep_alive = options.get('no_keep_alive', False) quit_command = 'CTRL-C' if settings.DEBUG: logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s') def inner_run(): # type: () -> None from django.conf import settings from django.utils import translation translation.activate(settings.LANGUAGE_CODE) print("Validating Django models.py...") self.validate(display_num_errors=True) print("\nDjango version %s" % (django.get_version())) print("Tornado server is running at http://%s:%s/" % (addr, port)) print("Quit the server with %s." % (quit_command, )) if settings.USING_RABBITMQ: queue_client = get_queue_client() # Process notifications received via RabbitMQ queue_client.register_json_consumer('notify_tornado', process_notification) queue_client.register_json_consumer('tornado_return', respond_send_message) try: urls = ( r"/notify_tornado", r"/json/events", r"/api/v1/events", ) # Application is an instance of Django's standard wsgi handler. application = web.Application( [(url, AsyncDjangoHandler) for url in urls] + get_sockjs_router().urls, debug=django.conf.settings.DEBUG, # Disable Tornado's own request logging, since we have our own log_function=lambda x: None) # start tornado web server in single-threaded mode http_server = httpserver.HTTPServer( application, xheaders=xheaders, no_keep_alive=no_keep_alive) http_server.listen(int(port), address=addr) if django.conf.settings.DEBUG: ioloop.IOLoop.instance().set_blocking_log_threshold(5) setup_event_queue() add_client_gc_hook(missedmessage_hook) setup_tornado_rabbitmq() ioloop.IOLoop.instance().start() except KeyboardInterrupt: sys.exit(0) inner_run()
def handle(self, addrport: str, **options: bool) -> None: interactive_debug_listen() import django from tornado import httpserver try: addr, port = addrport.split(':') except ValueError: addr, port = '', addrport if not addr: addr = '127.0.0.1' if not port.isdigit(): raise CommandError("%r is not a valid port number." % (port, )) xheaders = options.get('xheaders', True) no_keep_alive = options.get('no_keep_alive', False) if settings.DEBUG: logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s') def inner_run() -> None: from django.conf import settings from django.utils import translation translation.activate(settings.LANGUAGE_CODE) # We pass display_num_errors=False, since Django will # likely display similar output anyway. self.check(display_num_errors=False) print("Tornado server is running at http://%s:%s/" % (addr, port)) if settings.USING_RABBITMQ: queue_client = get_queue_client() # Process notifications received via RabbitMQ queue_name = notify_tornado_queue_name(int(port)) queue_client.register_json_consumer( queue_name, get_wrapped_process_notification(queue_name)) try: # Application is an instance of Django's standard wsgi handler. application = create_tornado_application(int(port)) if settings.AUTORELOAD: zulip_autoreload_start() # start tornado web server in single-threaded mode http_server = httpserver.HTTPServer( application, xheaders=xheaders, no_keep_alive=no_keep_alive) http_server.listen(int(port), address=addr) from zerver.tornado.ioloop_logging import logging_data logging_data['port'] = port setup_event_queue(int(port)) add_client_gc_hook(missedmessage_hook) setup_tornado_rabbitmq() instance = ioloop.IOLoop.instance() if django.conf.settings.DEBUG: instance.set_blocking_log_threshold(5) instance.handle_callback_exception = handle_callback_exception instance.start() except KeyboardInterrupt: sys.exit(0) inner_run()
def handle(self, addrport: str, **options: bool) -> None: interactive_debug_listen() import django from tornado import httpserver try: addr, port = addrport.split(':') except ValueError: addr, port = '', addrport if not addr: addr = '127.0.0.1' if not port.isdigit(): raise CommandError("%r is not a valid port number." % (port, )) xheaders = options.get('xheaders', True) no_keep_alive = options.get('no_keep_alive', False) quit_command = 'CTRL-C' if settings.DEBUG: logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s') def inner_run() -> None: from django.conf import settings from django.utils import translation translation.activate(settings.LANGUAGE_CODE) print("Validating Django models.py...") self.check(display_num_errors=True) print("\nDjango version %s" % (django.get_version())) print("Tornado server is running at http://%s:%s/" % (addr, port)) print("Quit the server with %s." % (quit_command, )) if settings.USING_RABBITMQ: queue_client = get_queue_client() # Process notifications received via RabbitMQ queue_client.register_json_consumer('notify_tornado', process_notification) queue_client.register_json_consumer('tornado_return', respond_send_message) try: # Application is an instance of Django's standard wsgi handler. application = create_tornado_application() # start tornado web server in single-threaded mode http_server = httpserver.HTTPServer( application, xheaders=xheaders, no_keep_alive=no_keep_alive) http_server.listen(int(port), address=addr) setup_event_queue() add_client_gc_hook(missedmessage_hook) setup_tornado_rabbitmq() instance = ioloop.IOLoop.instance() if django.conf.settings.DEBUG: instance.set_blocking_log_threshold(5) instance.handle_callback_exception = handle_callback_exception instance.start() except KeyboardInterrupt: sys.exit(0) inner_run()
def handle(self, addrport: str, **options: bool) -> None: interactive_debug_listen() import django from tornado import httpserver try: addr, port = addrport.split(':') except ValueError: addr, port = '', addrport if not addr: addr = '127.0.0.1' if not port.isdigit(): raise CommandError("%r is not a valid port number." % (port,)) xheaders = options.get('xheaders', True) no_keep_alive = options.get('no_keep_alive', False) quit_command = 'CTRL-C' if settings.DEBUG: logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s') def inner_run() -> None: from django.conf import settings from django.utils import translation translation.activate(settings.LANGUAGE_CODE) print("Validating Django models.py...") self.check(display_num_errors=True) print("\nDjango version %s" % (django.get_version())) print("Tornado server is running at http://%s:%s/" % (addr, port)) print("Quit the server with %s." % (quit_command,)) if settings.USING_RABBITMQ: queue_client = get_queue_client() # Process notifications received via RabbitMQ queue_client.register_json_consumer('notify_tornado', process_notification) queue_client.register_json_consumer('tornado_return', respond_send_message) try: # Application is an instance of Django's standard wsgi handler. application = create_tornado_application() # start tornado web server in single-threaded mode http_server = httpserver.HTTPServer(application, xheaders=xheaders, no_keep_alive=no_keep_alive) http_server.listen(int(port), address=addr) setup_event_queue() add_client_gc_hook(missedmessage_hook) setup_tornado_rabbitmq() instance = ioloop.IOLoop.instance() if django.conf.settings.DEBUG: instance.set_blocking_log_threshold(5) instance.handle_callback_exception = handle_callback_exception instance.start() except KeyboardInterrupt: sys.exit(0) inner_run()
def handle(self, addrport, **options): # setup unbuffered I/O sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0) interactive_debug_listen() import django from tornado import httpserver, web try: addr, port = addrport.split(':') except ValueError: addr, port = '', addrport if not addr: addr = '127.0.0.1' if not port.isdigit(): raise CommandError("%r is not a valid port number." % (port,)) xheaders = options.get('xheaders', True) no_keep_alive = options.get('no_keep_alive', False) quit_command = 'CTRL-C' if settings.DEBUG: logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s') def inner_run(): from django.conf import settings from django.utils import translation translation.activate(settings.LANGUAGE_CODE) print("Validating Django models.py...") self.validate(display_num_errors=True) print("\nDjango version %s" % (django.get_version())) print("Tornado server is running at http://%s:%s/" % (addr, port)) print("Quit the server with %s." % (quit_command,)) if settings.USING_RABBITMQ: queue_client = get_queue_client() # Process notifications received via RabbitMQ queue_client.register_json_consumer('notify_tornado', process_notification) queue_client.register_json_consumer('tornado_return', respond_send_message) try: urls = (r"/notify_tornado", r"/json/get_events", r"/json/events", r"/api/v1/events", ) # Application is an instance of Django's standard wsgi handler. application = web.Application([(url, AsyncDjangoHandler) for url in urls] + get_sockjs_router().urls, debug=django.conf.settings.DEBUG, # Disable Tornado's own request logging, since we have our own log_function=lambda x: None) # start tornado web server in single-threaded mode http_server = httpserver.HTTPServer(application, xheaders=xheaders, no_keep_alive=no_keep_alive) http_server.listen(int(port), address=addr) if django.conf.settings.DEBUG: ioloop.IOLoop.instance().set_blocking_log_threshold(5) setup_event_queue() add_client_gc_hook(missedmessage_hook) setup_tornado_rabbitmq() ioloop.IOLoop.instance().start() except KeyboardInterrupt: sys.exit(0) inner_run()
def handle(self, *args: Any, **options: Any) -> None: interactive_debug_listen() addrport = options["addrport"] assert isinstance(addrport, str) from tornado import httpserver if addrport.isdigit(): addr, port = "", int(addrport) else: r = SplitResult("", addrport, "", "", "") if r.port is None: raise CommandError( f"{addrport!r} does not have a valid port number.") addr, port = r.hostname or "", r.port if not addr: addr = "127.0.0.1" if settings.DEBUG: logging.basicConfig( level=logging.INFO, format="%(asctime)s %(levelname)-8s %(message)s") async def inner_run() -> None: from django.utils import translation AsyncIOMainLoop().install() loop = asyncio.get_running_loop() stop_fut = loop.create_future() def stop() -> None: if not stop_fut.done(): stop_fut.set_result(None) def add_signal_handlers() -> None: loop.add_signal_handler(signal.SIGINT, stop), loop.add_signal_handler(signal.SIGTERM, stop), def remove_signal_handlers() -> None: loop.remove_signal_handler(signal.SIGINT), loop.remove_signal_handler(signal.SIGTERM), async with AsyncExitStack() as stack: stack.push_async_callback( sync_to_async(remove_signal_handlers, thread_sensitive=True)) await sync_to_async(add_signal_handlers, thread_sensitive=True)() translation.activate(settings.LANGUAGE_CODE) # We pass display_num_errors=False, since Django will # likely display similar output anyway. self.check(display_num_errors=False) print(f"Tornado server (re)started on port {port}") if settings.USING_RABBITMQ: queue_client = TornadoQueueClient() set_queue_client(queue_client) # Process notifications received via RabbitMQ queue_name = notify_tornado_queue_name(port) stack.callback(queue_client.close) queue_client.start_json_consumer( queue_name, get_wrapped_process_notification(queue_name)) # Application is an instance of Django's standard wsgi handler. application = create_tornado_application() # start tornado web server in single-threaded mode http_server = httpserver.HTTPServer(application, xheaders=True) stack.push_async_callback(http_server.close_all_connections) stack.callback(http_server.stop) http_server.listen(port, address=addr) from zerver.tornado.ioloop_logging import logging_data logging_data["port"] = str(port) await setup_event_queue(http_server, port) stack.callback(dump_event_queues, port) add_client_gc_hook(missedmessage_hook) if settings.USING_RABBITMQ: setup_tornado_rabbitmq(queue_client) if hasattr(__main__, "add_reload_hook"): autoreload.start() await stop_fut # Monkey patch tornado.autoreload to prevent it from continuing # to watch for changes after catching our SystemExit. Otherwise # the user needs to press Ctrl+C twice. __main__.wait = lambda: None async_to_sync(inner_run, force_new_loop=True)()
def handle(self, *args: Any, **options: Any) -> None: interactive_debug_listen() addrport = options["addrport"] assert isinstance(addrport, str) import django from tornado import httpserver if addrport.isdigit(): addr, port = "", int(addrport) else: r = SplitResult("", addrport, "", "", "") if r.port is None: raise CommandError( f"{addrport!r} does not have a valid port number.") addr, port = r.hostname or "", r.port if not addr: addr = "127.0.0.1" if settings.DEBUG: logging.basicConfig( level=logging.INFO, format="%(asctime)s %(levelname)-8s %(message)s") def inner_run() -> None: from django.utils import translation translation.activate(settings.LANGUAGE_CODE) # We pass display_num_errors=False, since Django will # likely display similar output anyway. self.check(display_num_errors=False) print(f"Tornado server (re)started on port {port}") if settings.USING_RABBITMQ: queue_client = get_queue_client() assert isinstance(queue_client, TornadoQueueClient) # Process notifications received via RabbitMQ queue_name = notify_tornado_queue_name(port) queue_client.start_json_consumer( queue_name, get_wrapped_process_notification(queue_name)) try: # Application is an instance of Django's standard wsgi handler. application = create_tornado_application() if settings.AUTORELOAD: zulip_autoreload_start() # start tornado web server in single-threaded mode http_server = httpserver.HTTPServer(application, xheaders=True) http_server.listen(port, address=addr) from zerver.tornado.ioloop_logging import logging_data logging_data["port"] = str(port) setup_event_queue(http_server, port) add_client_gc_hook(missedmessage_hook) setup_tornado_rabbitmq() instance = ioloop.IOLoop.instance() if django.conf.settings.DEBUG: instance.set_blocking_log_threshold(5) instance.handle_callback_exception = handle_callback_exception instance.start() except KeyboardInterrupt: sys.exit(0) inner_run()