2013-04-23 18:51:17 +02:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
from typing import Any, Callable
|
2013-03-28 19:01:04 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management.base import BaseCommand, \
|
|
|
|
CommandError, CommandParser
|
2013-01-09 23:34:19 +01:00
|
|
|
from tornado import ioloop
|
2016-11-03 16:56:13 +01:00
|
|
|
from tornado.log import app_log
|
2016-11-27 04:56:26 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
# We must call zerver.tornado.ioloop_logging.instrument_tornado_ioloop
|
|
|
|
# before we import anything else from our project in order for our
|
|
|
|
# Tornado load logging to work; otherwise we might accidentally import
|
|
|
|
# zerver.lib.queue (which will instantiate the Tornado ioloop) before
|
|
|
|
# this.
|
|
|
|
from zerver.tornado.ioloop_logging import instrument_tornado_ioloop
|
2013-01-09 23:34:19 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
settings.RUNNING_INSIDE_TORNADO = True
|
|
|
|
instrument_tornado_ioloop()
|
2016-01-28 01:26:48 +01:00
|
|
|
|
2018-04-17 23:06:30 +02:00
|
|
|
from zerver.lib.debug import interactive_debug_listen
|
|
|
|
from zerver.tornado.application import create_tornado_application, \
|
|
|
|
setup_tornado_rabbitmq
|
2018-05-21 00:38:54 +02:00
|
|
|
from zerver.tornado.autoreload import start as zulip_autoreload_start
|
2018-04-17 23:06:30 +02:00
|
|
|
from zerver.tornado.event_queue import add_client_gc_hook, \
|
|
|
|
missedmessage_hook, process_notification, setup_event_queue
|
2018-11-03 00:06:13 +01:00
|
|
|
from zerver.tornado.sharding import notify_tornado_queue_name, tornado_return_queue_name
|
2018-04-17 23:06:30 +02:00
|
|
|
from zerver.tornado.socket import respond_send_message
|
|
|
|
|
2013-03-25 20:37:00 +01:00
|
|
|
if settings.USING_RABBITMQ:
|
2013-10-23 22:33:59 +02:00
|
|
|
from zerver.lib.queue import get_queue_client
|
2013-03-25 20:37:00 +01:00
|
|
|
|
2016-11-03 16:56:13 +01:00
|
|
|
|
2017-11-02 17:02:49 +01:00
|
|
|
def handle_callback_exception(callback: Callable[..., Any]) -> None:
|
2016-11-03 16:56:13 +01:00
|
|
|
logging.exception("Exception in callback")
|
|
|
|
app_log.error("Exception in callback %r", callback, exc_info=True)
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Starts a Tornado Web server wrapping Django."
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2016-11-03 10:22:19 +01:00
|
|
|
parser.add_argument('addrport', nargs="?", type=str,
|
|
|
|
help='[optional port number or ipaddr:port]\n '
|
|
|
|
'(use multiple ports to start multiple servers)')
|
|
|
|
|
|
|
|
parser.add_argument('--nokeepalive', action='store_true',
|
|
|
|
dest='no_keep_alive', default=False,
|
|
|
|
help="Tells Tornado to NOT keep alive http connections.")
|
|
|
|
|
|
|
|
parser.add_argument('--noxheaders', action='store_false',
|
|
|
|
dest='xheaders', default=True,
|
|
|
|
help="Tells Tornado to NOT override remote IP with X-Real-IP.")
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, addrport: str, **options: bool) -> None:
|
2013-03-06 19:28:41 +01:00
|
|
|
interactive_debug_listen()
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
import django
|
2016-11-27 04:56:26 +01:00
|
|
|
from tornado import httpserver
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2012-10-30 21:37:19 +01:00
|
|
|
try:
|
|
|
|
addr, port = addrport.split(':')
|
|
|
|
except ValueError:
|
|
|
|
addr, port = '', addrport
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
if not addr:
|
|
|
|
addr = '127.0.0.1'
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
if not port.isdigit():
|
2015-12-01 17:11:16 +01:00
|
|
|
raise CommandError("%r is not a valid port number." % (port,))
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
xheaders = options.get('xheaders', True)
|
|
|
|
no_keep_alive = options.get('no_keep_alive', False)
|
|
|
|
quit_command = 'CTRL-C'
|
|
|
|
|
|
|
|
if settings.DEBUG:
|
2012-09-05 19:20:32 +02:00
|
|
|
logging.basicConfig(level=logging.INFO,
|
2016-12-03 00:04:17 +01:00
|
|
|
format='%(asctime)s %(levelname)-8s %(message)s')
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def inner_run() -> None:
|
2012-08-28 22:56:21 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.utils import translation
|
|
|
|
translation.activate(settings.LANGUAGE_CODE)
|
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Validating Django models.py...")
|
2016-11-14 11:10:42 +01:00
|
|
|
self.check(display_num_errors=True)
|
2015-11-01 17:11:06 +01:00
|
|
|
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,))
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2013-01-18 23:16:53 +01:00
|
|
|
if settings.USING_RABBITMQ:
|
2013-10-23 22:33:59 +02:00
|
|
|
queue_client = get_queue_client()
|
2013-03-22 20:55:40 +01:00
|
|
|
# Process notifications received via RabbitMQ
|
2018-11-03 00:06:13 +01:00
|
|
|
queue_client.register_json_consumer(notify_tornado_queue_name(int(port)),
|
|
|
|
process_notification)
|
|
|
|
queue_client.register_json_consumer(tornado_return_queue_name(int(port)),
|
|
|
|
respond_send_message)
|
2013-01-18 23:16:53 +01:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
try:
|
|
|
|
# Application is an instance of Django's standard wsgi handler.
|
2018-11-03 00:09:16 +01:00
|
|
|
application = create_tornado_application(int(port))
|
2018-05-21 00:38:54 +02:00
|
|
|
if settings.AUTORELOAD:
|
|
|
|
zulip_autoreload_start()
|
2012-08-28 22:56:21 +02:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2018-10-04 00:22:39 +02:00
|
|
|
setup_event_queue(int(port))
|
2014-04-24 02:16:53 +02:00
|
|
|
add_client_gc_hook(missedmessage_hook)
|
2013-04-16 17:49:03 +02:00
|
|
|
setup_tornado_rabbitmq()
|
2016-11-03 16:56:13 +01:00
|
|
|
|
|
|
|
instance = ioloop.IOLoop.instance()
|
|
|
|
|
|
|
|
if django.conf.settings.DEBUG:
|
|
|
|
instance.set_blocking_log_threshold(5)
|
2016-11-28 23:29:01 +01:00
|
|
|
instance.handle_callback_exception = handle_callback_exception
|
2016-11-03 16:56:13 +01:00
|
|
|
instance.start()
|
2012-08-28 22:56:21 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(0)
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
inner_run()
|