From f73e8988746ce7e69ff44c7820f30d65318f840e Mon Sep 17 00:00:00 2001 From: Greg Price Date: Thu, 17 Aug 2017 21:36:37 -0700 Subject: [PATCH] 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. --- manage.py | 7 ++----- scripts/lib/log-management-command | 25 ------------------------- scripts/lib/zulip_tools.py | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 31 deletions(-) delete mode 100755 scripts/lib/log-management-command diff --git a/manage.py b/manage.py index 823c1558e5..c6c74289b4 100755 --- a/manage.py +++ b/manage.py @@ -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") diff --git a/scripts/lib/log-management-command b/scripts/lib/log-management-command deleted file mode 100755 index 5e2be57852..0000000000 --- a/scripts/lib/log-management-command +++ /dev/null @@ -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],)) diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py index 49a678f12e..5acc8870be 100755 --- a/scripts/lib/zulip_tools.py +++ b/scripts/lib/zulip_tools.py @@ -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,))