tornado: Fix autoreload for Tornado 6.4 changes.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-12-09 13:31:30 -08:00 committed by Anders Kaseorg
parent a1fa969693
commit cf16ddd4d6
3 changed files with 14 additions and 19 deletions

View File

@ -92,10 +92,17 @@ if options.test:
settings_module = "zproject.test_settings" settings_module = "zproject.test_settings"
# Don't auto-reload when running Puppeteer tests # Don't auto-reload when running Puppeteer tests
runserver_args = ["--noreload"] runserver_args = ["--noreload"]
tornado_autoreload = [] runtornado_command = ["./manage.py", "runtornado"]
else: else:
settings_module = "zproject.settings" settings_module = "zproject.settings"
tornado_autoreload = ["-m", "tornado.autoreload"] runtornado_command = [
"-m",
"tornado.autoreload",
"--until-success",
"./manage.py",
"runtornado",
"--autoreload",
]
manage_args = [f"--settings={settings_module}"] manage_args = [f"--settings={settings_module}"]
os.environ["DJANGO_SETTINGS_MODULE"] = settings_module os.environ["DJANGO_SETTINGS_MODULE"] = settings_module
@ -148,9 +155,7 @@ def server_processes() -> List[List[str]]:
"env", "env",
"PYTHONUNBUFFERED=1", "PYTHONUNBUFFERED=1",
"python3", "python3",
*tornado_autoreload, *runtornado_command,
"./manage.py",
"runtornado",
*manage_args, *manage_args,
f"127.0.0.1:{tornado_port}", f"127.0.0.1:{tornado_port}",
], ],

View File

@ -8,12 +8,9 @@ from urllib.parse import SplitResult
from asgiref.sync import async_to_sync, sync_to_async from asgiref.sync import async_to_sync, sync_to_async
from django.conf import settings from django.conf import settings
from django.core.management.base import BaseCommand, CommandError, CommandParser from django.core.management.base import BaseCommand, CommandError, CommandParser
from tornado import autoreload
from tornado.platform.asyncio import AsyncIOMainLoop from tornado.platform.asyncio import AsyncIOMainLoop
from typing_extensions import override from typing_extensions import override
import __main__
settings.RUNNING_INSIDE_TORNADO = True settings.RUNNING_INSIDE_TORNADO = True
if settings.PRODUCTION: if settings.PRODUCTION:
settings.SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") settings.SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
@ -42,6 +39,7 @@ class Command(BaseCommand):
@override @override
def add_arguments(self, parser: CommandParser) -> None: def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("--autoreload", action="store_true", help="Enable Tornado autoreload")
parser.add_argument( parser.add_argument(
"addrport", "addrport",
help="[port number or ipaddr:port]", help="[port number or ipaddr:port]",
@ -118,7 +116,7 @@ class Command(BaseCommand):
) )
# Application is an instance of Django's standard wsgi handler. # Application is an instance of Django's standard wsgi handler.
application = create_tornado_application() application = create_tornado_application(autoreload=options["autoreload"])
# start tornado web server in single-threaded mode # start tornado web server in single-threaded mode
http_server = httpserver.HTTPServer(application, xheaders=True) http_server = httpserver.HTTPServer(application, xheaders=True)
@ -135,14 +133,6 @@ class Command(BaseCommand):
if settings.USING_RABBITMQ: if settings.USING_RABBITMQ:
setup_tornado_rabbitmq(queue_client) setup_tornado_rabbitmq(queue_client)
if hasattr(__main__, "add_reload_hook"):
autoreload.start()
await stop_fut 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)() async_to_sync(inner_run, force_new_loop=True)()

View File

@ -12,7 +12,7 @@ def setup_tornado_rabbitmq(queue_client: TornadoQueueClient) -> None: # nocover
autoreload.add_reload_hook(lambda: queue_client.close()) autoreload.add_reload_hook(lambda: queue_client.close())
def create_tornado_application() -> tornado.web.Application: def create_tornado_application(*, autoreload: bool = False) -> tornado.web.Application:
django_handler = BaseHandler() django_handler = BaseHandler()
django_handler.load_middleware() django_handler.load_middleware()
@ -26,7 +26,7 @@ def create_tornado_application() -> tornado.web.Application:
return tornado.web.Application( return tornado.web.Application(
[(url, AsyncDjangoHandler, dict(django_handler=django_handler)) for url in urls], [(url, AsyncDjangoHandler, dict(django_handler=django_handler)) for url in urls],
debug=settings.DEBUG, debug=settings.DEBUG,
autoreload=False, autoreload=autoreload,
# Disable Tornado's own request logging, since we have our own # Disable Tornado's own request logging, since we have our own
log_function=lambda x: None, log_function=lambda x: None,
) )