Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2actions/st2actions/cmd/actionrunner.py (3 issues)

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