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