2022-03-18 08:34:10 +01:00
|
|
|
import asyncio
|
2017-11-16 00:43:27 +01:00
|
|
|
import logging
|
2022-03-18 08:34:10 +01:00
|
|
|
import signal
|
|
|
|
from contextlib import AsyncExitStack
|
2022-03-18 00:38:40 +01:00
|
|
|
from typing import Any
|
2020-06-27 03:03:26 +02:00
|
|
|
from urllib.parse import SplitResult
|
2013-03-28 19:01:04 +01:00
|
|
|
|
2022-03-17 22:09:11 +01:00
|
|
|
import __main__
|
2022-03-18 08:34:10 +01:00
|
|
|
from asgiref.sync import async_to_sync, sync_to_async
|
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
|
2022-03-18 08:34:10 +01:00
|
|
|
from tornado import autoreload
|
2022-03-17 21:42:25 +01:00
|
|
|
from tornado.platform.asyncio import AsyncIOMainLoop
|
2016-11-27 04:56:26 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
settings.RUNNING_INSIDE_TORNADO = True
|
2022-09-23 23:24:11 +02:00
|
|
|
if settings.PRODUCTION:
|
|
|
|
settings.SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
|
2016-01-28 01:26:48 +01:00
|
|
|
|
2022-03-18 08:34:10 +01:00
|
|
|
from zerver.lib.async_utils import NoAutoCreateEventLoopPolicy
|
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
|
2022-09-22 22:09:34 +02:00
|
|
|
from zerver.tornado.descriptors import set_current_port
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.tornado.event_queue import (
|
|
|
|
add_client_gc_hook,
|
2022-03-18 08:34:10 +01:00
|
|
|
dump_event_queues,
|
2020-06-11 00:54:34 +02:00
|
|
|
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:
|
2022-04-16 02:30:58 +02:00
|
|
|
from zerver.lib.queue import TornadoQueueClient, set_queue_client
|
2013-03-25 20:37:00 +01:00
|
|
|
|
2022-03-18 08:34:10 +01:00
|
|
|
asyncio.set_event_loop_policy(NoAutoCreateEventLoopPolicy())
|
|
|
|
|
2016-11-03 16:56:13 +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
|
|
|
)
|
|
|
|
|
2021-08-14 16:51:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2013-03-06 19:28:41 +01:00
|
|
|
interactive_debug_listen()
|
2021-08-14 16:51:57 +02:00
|
|
|
addrport = options["addrport"]
|
|
|
|
assert isinstance(addrport, str)
|
2012-09-05 17:23:58 +02:00
|
|
|
|
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
|
|
|
|
2022-03-18 08:34:10 +01:00
|
|
|
async def inner_run() -> None:
|
2012-08-28 22:56:21 +02:00
|
|
|
from django.utils import translation
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-03-18 08:34:10 +01:00
|
|
|
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:
|
2023-07-31 21:59:55 +02:00
|
|
|
loop.add_signal_handler(signal.SIGINT, stop)
|
|
|
|
loop.add_signal_handler(signal.SIGTERM, stop)
|
2012-08-28 22:56:21 +02:00
|
|
|
|
2022-03-18 08:34:10 +01:00
|
|
|
def remove_signal_handlers() -> None:
|
2023-07-31 21:59:55 +02:00
|
|
|
loop.remove_signal_handler(signal.SIGINT)
|
|
|
|
loop.remove_signal_handler(signal.SIGTERM)
|
2012-09-05 17:23:58 +02:00
|
|
|
|
2022-03-18 08:34:10 +01:00
|
|
|
async with AsyncExitStack() as stack:
|
|
|
|
stack.push_async_callback(
|
|
|
|
sync_to_async(remove_signal_handlers, thread_sensitive=True)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2022-03-18 08:34:10 +01:00
|
|
|
await sync_to_async(add_signal_handlers, thread_sensitive=True)()
|
|
|
|
|
2022-09-22 22:09:34 +02:00
|
|
|
set_current_port(port)
|
2022-03-18 08:34:10 +01:00
|
|
|
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)
|
|
|
|
)
|
2013-01-18 23:16:53 +01:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
# Application is an instance of Django's standard wsgi handler.
|
2020-09-16 22:17:31 +02:00
|
|
|
application = create_tornado_application()
|
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)
|
2022-03-17 21:42:25 +01:00
|
|
|
stack.push_async_callback(http_server.close_all_connections)
|
2022-03-18 08:34:10 +01:00
|
|
|
stack.callback(http_server.stop)
|
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)
|
2022-03-18 08:34:10 +01:00
|
|
|
await setup_event_queue(http_server, port)
|
|
|
|
stack.callback(dump_event_queues, port)
|
2014-04-24 02:16:53 +02:00
|
|
|
add_client_gc_hook(missedmessage_hook)
|
2022-04-16 02:30:58 +02:00
|
|
|
if settings.USING_RABBITMQ:
|
|
|
|
setup_tornado_rabbitmq(queue_client)
|
2016-11-03 16:56:13 +01:00
|
|
|
|
2022-03-17 22:09:11 +01:00
|
|
|
if hasattr(__main__, "add_reload_hook"):
|
|
|
|
autoreload.start()
|
|
|
|
|
2022-03-18 08:34:10 +01:00
|
|
|
await stop_fut
|
|
|
|
|
2022-03-17 22:09:11 +01:00
|
|
|
# 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
|
|
|
|
|
2022-03-18 08:34:10 +01:00
|
|
|
async_to_sync(inner_run, force_new_loop=True)()
|