Browse Source

perf: 优化启动脚本,避免启动超时

pull/7385/head
ibuler 3 years ago committed by Jiangjie.Bai
parent
commit
fa5c433c7c
  1. 2
      apps/common/management/commands/services/services/base.py
  2. 110
      jms

2
apps/common/management/commands/services/services/base.py

@ -170,7 +170,7 @@ class BaseService(object):
def _restart(self): def _restart(self):
if self.retry > self.max_retry: if self.retry > self.max_retry:
logging.info("Service start failed, exit: ", self.name) logging.info("Service start failed, exit: {}".format(self.name))
self.EXIT_EVENT.set() self.EXIT_EVENT.set()
return return
self.retry += 1 self.retry += 1

110
jms

@ -2,20 +2,27 @@
# coding: utf-8 # coding: utf-8
import os import os
import subprocess
import logging import logging
import logging.handlers import logging.handlers
import time import time
import argparse import argparse
import sys import sys
import django
from django.core import management
from django.db.utils import OperationalError
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, BASE_DIR) APP_DIR = os.path.join(BASE_DIR, 'apps')
os.chdir(APP_DIR)
sys.path.insert(0, APP_DIR)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "jumpserver.settings")
django.setup()
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S") logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
try: try:
from apps.jumpserver import const from jumpserver import const
__version__ = const.VERSION __version__ = const.VERSION
except ImportError as e: except ImportError as e:
print("Not found __version__: {}".format(e)) print("Not found __version__: {}".format(e))
@ -25,7 +32,7 @@ except ImportError as e:
sys.exit(1) sys.exit(1)
try: try:
from apps.jumpserver.const import CONFIG from jumpserver.const import CONFIG
except ImportError as e: except ImportError as e:
print("Import error: {}".format(e)) print("Import error: {}".format(e))
print("Could not find config file, `cp config_example.yml config.yml`") print("Could not find config file, `cp config_example.yml config.yml`")
@ -48,56 +55,45 @@ except:
def check_database_connection(): def check_database_connection():
os.chdir(os.path.join(BASE_DIR, 'apps'))
for i in range(60): for i in range(60):
logging.info("Check database connection ...") logging.info(f"Check database connection: {i}")
_code = subprocess.call("python manage.py showmigrations users ", shell=True) try:
if _code == 0: management.call_command('check', '--database', 'default')
logging.info("Database connect success") logging.info("Database connect success")
return return
except OperationalError:
logging.info('Database not setup, retry')
except Exception as e:
logging.error('Unexpect error occur: {}'.format(str(e)))
time.sleep(1) time.sleep(1)
logging.error("Connection database failed, exit") logging.error("Connection database failed, exit")
sys.exit(10) sys.exit(10)
def check_migrations():
_apps_dir = os.path.join(BASE_DIR, 'apps')
_cmd = "python manage.py showmigrations | grep '\[.\]' | grep -v '\[X\]'"
_code = subprocess.call(_cmd, shell=True, cwd=_apps_dir)
if _code == 1:
return
# for i in range(3):
# print("!!! Warning: Has SQL migrations not perform, 有 SQL 变更没有执行")
# print("You should run `./PROC upgrade_db` first, 请先运行 ./PROC upgrade_db, 进行表结构变更")
# sys.exit(1)
def expire_caches(): def expire_caches():
_apps_dir = os.path.join(BASE_DIR, 'apps') try:
_code = subprocess.call("python manage.py expire_caches", shell=True, cwd=_apps_dir) management.call_command('expire_caches')
except:
if _code == 1: pass
return
def perform_db_migrate(): def perform_db_migrate():
logging.info("Check database structure change ...") logging.info("Check database structure change ...")
os.chdir(os.path.join(BASE_DIR, 'apps'))
logging.info("Migrate model change to database ...") logging.info("Migrate model change to database ...")
_code = subprocess.call('python3 manage.py migrate', shell=True) try:
if _code == 0: management.call_command('migrate')
return except Exception:
logging.error('Perform migrate failed, exit') logging.error('Perform migrate failed, exit')
sys.exit(11) sys.exit(11)
def collect_static(): def collect_static():
logging.info("Collect static files") logging.info("Collect static files")
os.chdir(os.path.join(BASE_DIR, 'apps')) try:
_cmd = 'python3 manage.py collectstatic --no-input -c &> /dev/null ' management.call_command('collectstatic', '--no-input', '-c', verbosity=0, interactive=False)
subprocess.call(_cmd, shell=True)
logging.info("Collect static files done") logging.info("Collect static files done")
except:
pass
def compile_i81n_file(): def compile_i81n_file():
@ -105,8 +101,7 @@ def compile_i81n_file():
if os.path.exists(django_mo_file): if os.path.exists(django_mo_file):
return return
os.chdir(os.path.join(BASE_DIR, 'apps')) os.chdir(os.path.join(BASE_DIR, 'apps'))
_cmd = 'python3 manage.py compilemessages --no-input -c &> /dev/null ' management.call_command('compilemessages', verbosity=0, interactive=False)
subprocess.call(_cmd, shell=True)
logging.info("Compile i18n files done") logging.info("Compile i18n files done")
@ -116,13 +111,33 @@ def upgrade_db():
def prepare(): def prepare():
# installer(check) & k8s(no check)
check_database_connection() check_database_connection()
check_migrations()
upgrade_db() upgrade_db()
expire_caches() expire_caches()
def start_services():
services = args.services if isinstance(args.services, list) else [args.services]
if action == 'start' and {'all', 'web'} & set(services):
prepare()
services_string = ' '.join(services)
cmd = f'{args.action} {services_string}'
if args.daemon:
cmd += ' --daemon'
if args.worker:
cmd += f' --worker {args.worker}'
if args.force:
cmd += ' --force'
lines = cmd.split()
try:
management.call_command(*lines)
except Exception as e:
logging.error("Start service error {}: {}".format(lines[0], e))
time.sleep(2)
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description=""" description="""
@ -155,23 +170,4 @@ if __name__ == '__main__':
elif action == "collect_static": elif action == "collect_static":
collect_static() collect_static()
else: else:
services = args.services if isinstance(args.services, list) else [args.services] start_services()
if action == 'start' and {'all', 'web'} & set(services):
prepare()
services_string = ' '.join(services)
cmd = f'python manage.py {args.action} {services_string}'
if args.daemon:
cmd += ' --daemon'
if args.worker:
cmd += f' --worker {args.worker}'
if args.force:
cmd += ' --force'
apps_dir = os.path.join(BASE_DIR, 'apps')
try:
# processes: main(3s) -> call(0.25s) -> service -> sub-process
code = subprocess.call(cmd, shell=True, cwd=apps_dir)
except KeyboardInterrupt:
time.sleep(2)
pass

Loading…
Cancel
Save