manage.py: Save an extra Django startup by converting one script to a library.

This saves us from spending 200-250ms of CPU time importing Django
again just to log that we're running a management command.  On
`scripts/restart-server`, this saves us from one thundering herd of
Django startups when all the queue workers are restarted; but there's
still the Django startup for the `manage.py` process itself for each
worker, so on a machine with e.g. 2 (virtual) cores the restart is
still painful.
This commit is contained in:
Greg Price 2017-08-17 21:36:37 -07:00 committed by Tim Abbott
parent 517d9b7594
commit f73e898874
3 changed files with 19 additions and 31 deletions

View File

@ -4,8 +4,6 @@ from __future__ import print_function
import os
import sys
import logging
import subprocess
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
@ -20,10 +18,9 @@ if __name__ == "__main__":
from django.conf import settings
from django.core.management.base import CommandError
from scripts.lib.zulip_tools import log_management_command
logger = logging.getLogger("zulip.management")
subprocess.check_call([os.path.join(BASE_DIR, "scripts", "lib", "log-management-command"),
" ".join(sys.argv)])
log_management_command(" ".join(sys.argv), settings.MANAGEMENT_LOG_PATH)
if "--no-traceback" not in sys.argv and len(sys.argv) > 1:
sys.argv.append("--traceback")

View File

@ -1,25 +0,0 @@
#!/usr/bin/env python3
import sys
import logging
import os
from os.path import dirname, abspath
BASE_DIR = dirname(dirname(dirname(abspath(__file__))))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")
from django.conf import settings
logger = logging.getLogger("zulip.management")
if not os.path.exists(settings.MANAGEMENT_LOG_PATH):
dir = os.path.dirname(settings.MANAGEMENT_LOG_PATH)
if not os.path.exists(dir):
os.makedirs(dir)
file_handler = logging.FileHandler(settings.MANAGEMENT_LOG_PATH)
formatter = logging.Formatter("%(asctime)s: %(message)s")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)
logger.info("Ran '%s'" % (sys.argv[1],))

View File

@ -2,6 +2,7 @@
from __future__ import print_function
import datetime
import errno
import logging
import os
import pwd
import re
@ -11,7 +12,7 @@ import sys
import time
if False:
from typing import Sequence, Any
from typing import Sequence, Text, Any
DEPLOYMENTS_DIR = "/home/zulip/deployments"
LOCK_DIR = os.path.join(DEPLOYMENTS_DIR, "lock")
@ -131,3 +132,18 @@ def run(args, **kwargs):
ENDC)
print()
raise
def log_management_command(cmd, log_path):
# type: (Text, Text) -> None
log_dir = os.path.dirname(log_path)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
formatter = logging.Formatter("%(asctime)s: %(message)s")
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(formatter)
logger = logging.getLogger("zulip.management")
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)
logger.info("Ran '%s'" % (cmd,))