upload
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
# 启动 mtgsig 出签常驻服务(幂等:已在跑就不重复起)
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import subprocess
|
||||
import requests
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
JS_DIR = os.path.join(BASE_DIR, 'mtgsig_js')
|
||||
PORT = int(os.environ.get('MTGSIG_PORT', 18092))
|
||||
BASE_URL = 'http://127.0.0.1:%d' % PORT
|
||||
# BASE_URL = 'http://192.168.1.139:%d' % PORT
|
||||
LOG_PATH = os.path.join(JS_DIR, 'serve.log')
|
||||
PROXY = {'http': None, 'https': None}
|
||||
|
||||
|
||||
def is_ready():
|
||||
try:
|
||||
r = requests.get(BASE_URL + '/health', timeout=3, proxies=PROXY)
|
||||
return r.status_code == 200 and r.json().get('ok')
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def start():
|
||||
if is_ready():
|
||||
print('出签服务已在运行: %s' % BASE_URL)
|
||||
return
|
||||
log = open(LOG_PATH, 'a', encoding='utf-8')
|
||||
flags = 0
|
||||
if os.name == 'nt':
|
||||
flags = 0x00000008 | 0x00000200 # DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP
|
||||
subprocess.Popen(
|
||||
['node', 'serve.mjs'],
|
||||
cwd=JS_DIR,
|
||||
stdout=log,
|
||||
stderr=log,
|
||||
creationflags=flags,
|
||||
)
|
||||
for _ in range(60):
|
||||
time.sleep(1)
|
||||
if is_ready():
|
||||
print('出签服务已就绪: %s' % BASE_URL)
|
||||
return
|
||||
print('等服务起来超时,看日志: %s' % LOG_PATH)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def find_pids_on_port():
|
||||
"""按端口找出正在监听的进程号(重启用)"""
|
||||
out = subprocess.run(['netstat', '-ano', '-p', 'tcp'], capture_output=True, text=True).stdout
|
||||
pids = set()
|
||||
for line in out.splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) >= 5 and parts[1].endswith(':%d' % PORT) and parts[3].upper() == 'LISTENING':
|
||||
pids.add(parts[4])
|
||||
return pids
|
||||
|
||||
|
||||
def stop():
|
||||
"""杀掉监听该端口的出签服务(重启用)"""
|
||||
pids = find_pids_on_port()
|
||||
if not pids:
|
||||
print('出签服务没在运行: %s' % BASE_URL)
|
||||
return
|
||||
for pid in pids:
|
||||
subprocess.run(['taskkill', '/F', '/PID', pid], capture_output=True, text=True)
|
||||
for _ in range(20):
|
||||
time.sleep(0.5)
|
||||
if not find_pids_on_port():
|
||||
print('出签服务已停止: %s' % BASE_URL)
|
||||
return
|
||||
print('停止超时,端口仍被占用: %s' % BASE_URL)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def restart():
|
||||
"""重启:先杀掉旧的,再重新起一个(改了 serve.mjs 后用这个)"""
|
||||
stop()
|
||||
start()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
mode = 'restart'
|
||||
if mode == 'restart':
|
||||
restart()
|
||||
elif mode == 'stop':
|
||||
stop()
|
||||
else:
|
||||
start()
|
||||
Reference in New Issue
Block a user