1
|
|
|
import eventlet |
2
|
|
|
import os |
3
|
|
|
import signal |
4
|
|
|
import sys |
5
|
|
|
|
6
|
|
|
from st2actions import config |
7
|
|
|
from st2actions import scheduler, worker |
8
|
|
|
from st2common import log as logging |
9
|
|
|
from st2common.service_setup import setup as common_setup |
10
|
|
|
from st2common.service_setup import teardown as common_teardown |
11
|
|
|
|
12
|
|
|
LOG = logging.getLogger(__name__) |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
eventlet.monkey_patch( |
16
|
|
|
os=True, |
17
|
|
|
select=True, |
18
|
|
|
socket=True, |
19
|
|
|
thread=False if '--use-debugger' in sys.argv else True, |
20
|
|
|
time=True) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def _setup_sigterm_handler(): |
24
|
|
|
|
25
|
|
|
def sigterm_handler(signum=None, frame=None): |
|
|
|
|
26
|
|
|
# This will cause SystemExit to be throw and allow for component cleanup. |
27
|
|
|
sys.exit(0) |
|
|
|
|
28
|
|
|
|
29
|
|
|
# Register a SIGTERM signal handler which calls sys.exit which causes SystemExit to |
30
|
|
|
# be thrown. We catch SystemExit and handle cleanup there. |
31
|
|
|
signal.signal(signal.SIGTERM, sigterm_handler) |
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
def _setup(): |
35
|
|
|
common_setup(service='actionrunner', config=config, setup_db=True, register_mq_exchanges=True, |
36
|
|
|
register_signal_handlers=True) |
37
|
|
|
_setup_sigterm_handler() |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def _run_worker(): |
41
|
|
|
LOG.info('(PID=%s) Worker started.', os.getpid()) |
42
|
|
|
|
43
|
|
|
components = [ |
44
|
|
|
scheduler.get_scheduler(), |
45
|
|
|
worker.get_worker() |
46
|
|
|
] |
47
|
|
|
|
48
|
|
|
try: |
49
|
|
|
for component in components: |
50
|
|
|
component.start() |
51
|
|
|
|
52
|
|
|
for component in components: |
53
|
|
|
component.wait() |
54
|
|
|
except (KeyboardInterrupt, SystemExit): |
55
|
|
|
LOG.info('(PID=%s) Worker stopped.', os.getpid()) |
56
|
|
|
|
57
|
|
|
errors = False |
58
|
|
|
|
59
|
|
|
for component in components: |
60
|
|
|
try: |
61
|
|
|
component.shutdown() |
62
|
|
|
except: |
63
|
|
|
LOG.exception('Unable to shutdown %s.', component.__class__.__name__) |
64
|
|
|
errors = True |
65
|
|
|
|
66
|
|
|
if errors: |
67
|
|
|
return 1 |
68
|
|
|
except: |
69
|
|
|
LOG.exception('(PID=%s) Worker unexpectedly stopped.', os.getpid()) |
70
|
|
|
return 1 |
71
|
|
|
|
72
|
|
|
return 0 |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def _teardown(): |
76
|
|
|
common_teardown() |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def main(): |
80
|
|
|
try: |
81
|
|
|
_setup() |
82
|
|
|
return _run_worker() |
83
|
|
|
except SystemExit as exit_code: |
84
|
|
|
sys.exit(exit_code) |
85
|
|
|
except: |
86
|
|
|
LOG.exception('(PID=%s) Worker quit due to exception.', os.getpid()) |
87
|
|
|
return 1 |
88
|
|
|
finally: |
89
|
|
|
_teardown() |
90
|
|
|
|