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

@@ -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,))