fix: 修改启动脚本

pull/6619/head
Bai 2021-08-10 14:43:54 +08:00 committed by Jiangjie.Bai
parent 0c96bf61ef
commit 7959f84bba
2 changed files with 16 additions and 20 deletions

View File

@ -117,11 +117,6 @@ class BaseService(object):
sig = 9 if force else 15 sig = 9 if force else 15
os.kill(self.pid, sig) os.kill(self.pid, sig)
try:
self.process.wait(2)
except:
pass
for i in range(self.STOP_TIMEOUT): for i in range(self.STOP_TIMEOUT):
if i == self.STOP_TIMEOUT - 1: if i == self.STOP_TIMEOUT - 1:
print("\033[31m Error\033[0m") print("\033[31m Error\033[0m")
@ -143,14 +138,10 @@ class BaseService(object):
def _check(self): def _check(self):
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f"{now} Check service status: {self.name} -> ", end='') print(f"{now} Check service status: {self.name} -> ", end='')
try:
self.process.wait(timeout=1) # 不wait子进程可能无法回收
except subprocess.TimeoutExpired:
pass
if self.is_running: if self.is_running:
print(f'running at {self.process.pid}') print(f'running at {self.pid}')
else: else:
print(f'stopped with code: {self.process.returncode}({self.process.pid})') print(f'stopped at {self.pid}')
def _restart(self): def _restart(self):
if self.retry > self.max_retry: if self.retry > self.max_retry:
@ -158,7 +149,7 @@ class BaseService(object):
self.EXIT_EVENT.set() self.EXIT_EVENT.set()
return return
self.retry += 1 self.retry += 1
logging.info(f'> Find {self.name} stopped, retry {self.retry}, {self.process.pid}') logging.info(f'> Find {self.name} stopped, retry {self.retry}, {self.pid}')
self.start() self.start()
def _rotate_log(self): def _rotate_log(self):

21
jms
View File

@ -56,7 +56,7 @@ def check_database_connection():
logging.info("Database connect success") logging.info("Database connect success")
return return
time.sleep(1) time.sleep(1)
logging.error("Connection database failed, exist") logging.error("Connection database failed, exit")
sys.exit(10) sys.exit(10)
@ -85,7 +85,11 @@ def perform_db_migrate():
logging.info("Check database structure change ...") logging.info("Check database structure change ...")
os.chdir(os.path.join(BASE_DIR, 'apps')) os.chdir(os.path.join(BASE_DIR, 'apps'))
logging.info("Migrate model change to database ...") logging.info("Migrate model change to database ...")
subprocess.call('python3 manage.py migrate', shell=True) _code = subprocess.call('python3 manage.py migrate', shell=True)
if _code == 0:
return
logging.error('Perform migrate failed, exit')
sys.exit(11)
def collect_static(): def collect_static():
@ -96,18 +100,19 @@ def collect_static():
logging.info("Collect static files done") logging.info("Collect static files done")
def upgrade_db():
collect_static()
perform_db_migrate()
def prepare(): def prepare():
# installer(check) & k8s(no check)
check_database_connection() check_database_connection()
check_migrations() check_migrations()
upgrade_db() upgrade_db()
expire_caches() expire_caches()
def upgrade_db():
collect_static()
perform_db_migrate()
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description=""" description="""
@ -139,7 +144,7 @@ if __name__ == '__main__':
upgrade_db() upgrade_db()
else: else:
services = args.services if isinstance(args.services, list) else [args.services] services = args.services if isinstance(args.services, list) else [args.services]
if action == 'start' and ({'gunicorn', 'all'} & set(services)): if action == 'start' and {'all', 'web'} & set(services):
prepare() prepare()
services_string = ' '.join(services) services_string = ' '.join(services)