Skip to content

Commit

Permalink
manage.py: Save an extra Django startup by converting one script to a…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
gnprice authored and timabbott committed Aug 21, 2017
1 parent 517d9b7 commit f73e898
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
7 changes: 2 additions & 5 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import os
import sys
import logging
import subprocess

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
Expand All @@ -20,10 +18,9 @@

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")
Expand Down
25 changes: 0 additions & 25 deletions scripts/lib/log-management-command

This file was deleted.

18 changes: 17 additions & 1 deletion scripts/lib/zulip_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import print_function
import datetime
import errno
import logging
import os
import pwd
import re
Expand All @@ -11,7 +12,7 @@
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")
Expand Down Expand Up @@ -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,))

0 comments on commit f73e898

Please sign in to comment.