forked from CEduStartup/Pyxis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.py
67 lines (53 loc) · 2.03 KB
/
launcher.py
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
## Run Pyxis.
##
## This program uses configuration from config/processes.py
import gevent
from gevent import monkey
monkey.patch_all()
import subprocess
import os
from config.mq import queue_host, queue_port
from build_python_path import process_dir
from shared.events.GEventDispatcher import GEventDispatcher
from shared.events.Event import ServiceChangedStateEvent, LAUNCHER_TUBE
# Some processes have to inform Launcher that they're started and ready.
# For example, collector depends on shared services. Dependent processes are
# specified in process configuration (config/processes.py).
ready_processes = []
def on_service_state_event(event):
"""This happens when some processes tells Launcher that it is ready."""
if event.state == 'started':
ready_processes.append(event.service_id)
# Waiting for "Service Started" events.
receiver = GEventDispatcher(queue_host, queue_port, (LAUNCHER_TUBE,))
receiver.subscribe((ServiceChangedStateEvent,), on_service_state_event)
event_thread = gevent.spawn(receiver.dispatch)
def dependent_processes_ready(process):
"""Checks if all dependent processes are ready.
:Parameters:
`process`: process instance (from config/processes.py)
"""
for p in process.depends_on:
if not p.pid in ready_processes:
return False
return True
def run_processes():
executed_processes = []
for process in processes:
while(not dependent_processes_ready(process)):
gevent.sleep(0.5)
cmd = [process.command]
if isinstance(process.params, (list, tuple)):
cmd.extend(process.params)
else:
cmd.append(process.params)
subprocess.Popen(cmd, cwd=process.cwd)
if __name__ == '__main__':
os.environ['PYXIS_ROOT'] = os.getcwd()
from config.processes import processes
pythonpath_dirs = process_dir(os.getcwd())
pythonpath_dirs.append('%s/webui' %os.environ['PYXIS_ROOT'])
os.environ['PYTHONPATH'] = ':'.join(pythonpath_dirs)
run_processes()
while True:
gevent.sleep(0.01)