2017-11-16 00:43:27 +01:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
from typing import Any, Callable
|
2020-06-27 03:03:26 +02:00
|
|
|
from urllib.parse import SplitResult
|
2013-03-28 19:01:04 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.conf import settings
|
2020-06-11 00:54:34 +02:00
|
|
|
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
|
2020-06-11 00:54:34 +02:00
|
|
|
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
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.tornado.event_queue import (
|
|
|
|
add_client_gc_hook,
|
|
|
|
get_wrapped_process_notification,
|
|
|
|
missedmessage_hook,
|
|
|
|
setup_event_queue,
|
|
|
|
)
|
dependencies: Remove WebSockets system for sending messages.
Zulip has had a small use of WebSockets (specifically, for the code
path of sending messages, via the webapp only) since ~2013. We
originally added this use of WebSockets in the hope that the latency
benefits of doing so would allow us to avoid implementing a markdown
local echo; they were not. Further, HTTP/2 may have eliminated the
latency difference we hoped to exploit by using WebSockets in any
case.
While we’d originally imagined using WebSockets for other endpoints,
there was never a good justification for moving more components to the
WebSockets system.
This WebSockets code path had a lot of downsides/complexity,
including:
* The messy hack involving constructing an emulated request object to
hook into doing Django requests.
* The `message_senders` queue processor system, which increases RAM
needs and must be provisioned independently from the rest of the
server).
* A duplicate check_send_receive_time Nagios test specific to
WebSockets.
* The requirement for users to have their firewalls/NATs allow
WebSocket connections, and a setting to disable them for networks
where WebSockets don’t work.
* Dependencies on the SockJS family of libraries, which has at times
been poorly maintained, and periodically throws random JavaScript
exceptions in our production environments without a deep enough
traceback to effectively investigate.
* A total of about 1600 lines of our code related to the feature.
* Increased load on the Tornado system, especially around a Zulip
server restart, and especially for large installations like
zulipchat.com, resulting in extra delay before messages can be sent
again.
As detailed in
https://github.com/zulip/zulip/pull/12862#issuecomment-536152397, it
appears that removing WebSockets moderately increases the time it
takes for the `send_message` API query to return from the server, but
does not significantly change the time between when a message is sent
and when it is received by clients. We don’t understand the reason
for that change (suggesting the possibility of a measurement error),
and even if it is a real change, we consider that potential small
latency regression to be acceptable.
If we later want WebSockets, we’ll likely want to just use Django
Channels.
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-07-23 01:43:40 +02:00
|
|
|
from zerver.tornado.sharding import notify_tornado_queue_name
|
2018-04-17 23:06:30 +02:00
|
|
|
|
2013-03-25 20:37:00 +01:00
|
|
|
if settings.USING_RABBITMQ:
|
2020-10-10 05:24:35 +02:00
|
|
|
from zerver.lib.queue import TornadoQueueClient, 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:
|
2020-08-11 03:19:00 +02:00
|
|
|
logging.exception("Exception in callback", stack_info=True)
|
2016-11-03 16:56:13 +01:00
|
|
|
app_log.error("Exception in callback %r", callback, exc_info=True)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"addrport",
|
2021-08-15 06:00:50 +02:00
|
|
|
help="[port number or ipaddr:port]",
|
2021-02-12 08:19:30 +01: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
|
|
|
|
2020-06-27 03:03:26 +02:00
|
|
|
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
|
2012-10-30 21:37:19 +01:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
if not addr:
|
2021-02-12 08:20:45 +01:00
|
|
|
addr = "127.0.0.1"
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
if settings.DEBUG:
|
2021-02-12 08:19:30 +01:00
|
|
|
logging.basicConfig(
|
2021-02-12 08:20:45 +01:00
|
|
|
level=logging.INFO, format="%(asctime)s %(levelname)-8s %(message)s"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
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
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
translation.activate(settings.LANGUAGE_CODE)
|
|
|
|
|
2020-05-07 20:54:11 +02:00
|
|
|
# We pass display_num_errors=False, since Django will
|
|
|
|
# likely display similar output anyway.
|
|
|
|
self.check(display_num_errors=False)
|
2020-12-20 21:26:29 +01:00
|
|
|
print(f"Tornado server (re)started on port {port}")
|
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()
|
2020-10-10 05:24:35 +02:00
|
|
|
assert isinstance(queue_client, TornadoQueueClient)
|
2013-03-22 20:55:40 +01:00
|
|
|
# Process notifications received via RabbitMQ
|
2020-06-27 03:03:26 +02:00
|
|
|
queue_name = notify_tornado_queue_name(port)
|
2021-02-12 08:19:30 +01:00
|
|
|
queue_client.start_json_consumer(
|
|
|
|
queue_name, get_wrapped_process_notification(queue_name)
|
|
|
|
)
|
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.
|
2020-09-16 22:17:31 +02:00
|
|
|
application = create_tornado_application()
|
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
|
2021-03-23 23:29:02 +01:00
|
|
|
http_server = httpserver.HTTPServer(application, xheaders=True)
|
2020-06-27 03:03:26 +02:00
|
|
|
http_server.listen(port, address=addr)
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2020-03-24 12:48:41 +01:00
|
|
|
from zerver.tornado.ioloop_logging import logging_data
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
logging_data["port"] = str(port)
|
2020-06-27 03:03:26 +02:00
|
|
|
setup_event_queue(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()
|