Completed
Pull Request — master (#2299)
by Manas
06:08
created

st2actions.cmd._setup_sigterm_handler()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4286
cc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A st2actions.cmd.sigterm_handler() 0 4 1
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):
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 8 were found.
Loading history...
26
            # This will cause SystemExit to be throw and we call sensor_container.shutdown()
27
            # there which cleans things up.
28
            sys.exit(0)
0 ignored issues
show
Coding Style introduced by
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
Coding Style introduced by
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