forked from XX-net/XX-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_init.py
More file actions
107 lines (83 loc) · 3.35 KB
/
module_init.py
File metadata and controls
107 lines (83 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import subprocess
import threading
from instances import xlog
import os
import sys
import config
import web_control
import time
proc_handler = {}
current_path = os.path.dirname(os.path.abspath(__file__))
root_path = os.path.abspath( os.path.join(current_path, os.pardir))
if root_path not in sys.path:
sys.path.append(root_path)
def start(module):
if not os.path.isdir(os.path.join(root_path, module)):
return
try:
if module not in config.config["modules"]:
xlog.error("module not exist %s", module)
raise Exception("module not exist %s" % module)
if module in proc_handler:
xlog.error("module %s is running", module)
return "module is running"
if module not in proc_handler:
proc_handler[module] = {}
if os.path.isfile(os.path.join(root_path, module, "__init__.py")):
if "imp" not in proc_handler[module]:
proc_handler[module]["imp"] = __import__(module, globals(), locals(), ['start'], 0)
_start = proc_handler[module]["imp"].start
p = threading.Thread(target=_start.main)
p.daemon = True
p.start()
proc_handler[module]["proc"] = p
while not _start.client.ready:
time.sleep(0.1)
else:
script_path = os.path.join(root_path, module, 'start.py')
if not os.path.isfile(script_path):
xlog.warn("start module script not exist:%s", script_path)
return "fail"
proc_handler[module]["proc"] = subprocess.Popen([sys.executable, script_path], shell=False)
xlog.info("module %s started", module)
except Exception as e:
xlog.exception("start module %s fail:%s", module, e)
raise
return "start success."
def stop(module):
try:
if module not in proc_handler:
xlog.error("module %s not running", module)
return
if os.path.isfile(os.path.join(root_path, module, "__init__.py")):
_start = proc_handler[module]["imp"].start
xlog.debug("start to terminate %s module", module)
_start.client.terminate()
xlog.debug("module %s stopping", module)
while _start.client.ready:
time.sleep(0.1)
else:
proc_handler[module]["proc"].terminate() # Sends SIGTERM
proc_handler[module]["proc"].wait()
del proc_handler[module]
xlog.info("module %s stopped", module)
except Exception as e:
xlog.exception("stop module %s fail:%s", module, e)
return "Except:%s" % e
return "stop success."
def start_all_auto():
for module in config.config["modules"]:
if module == "launcher":
continue
if not os.path.isdir(os.path.join(root_path, module)):
continue
if "auto_start" in config.config['modules'][module] and config.config['modules'][module]["auto_start"]:
start_time = time.time()
start(module)
# web_control.confirm_module_ready(config.get(["modules", module, "control_port"], 0))
finished_time = time.time()
xlog.info("start %s time cost %d", module, (finished_time - start_time) * 1000)
def stop_all():
running_modules = [k for k in proc_handler]
for module in running_modules:
stop(module)