Completed
Pull Request — master (#2258)
by Manas
06:15
created

st2actions.cmd._run_worker()   A

Complexity

Conditions 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 14
rs 9.4286
1
import eventlet
2
import os
3
import sys
4
5
from st2common import log as logging
6
from st2common.service_setup import setup as common_setup
7
from st2common.service_setup import teardown as common_teardown
8
from st2actions.notifier import config
9
from st2actions.notifier import notifier
10
from st2actions.notifier import scheduler
11
12
13
LOG = logging.getLogger(__name__)
14
15
16
eventlet.monkey_patch(
17
    os=True,
18
    select=True,
19
    socket=True,
20
    thread=False if '--use-debugger' in sys.argv else True,
21
    time=True)
22
23
24
def _setup():
25
    common_setup(service='notifier', config=config, setup_db=True, register_mq_exchanges=True,
26
                 register_signal_handlers=True)
27
28
29
def _run_worker():
30
    LOG.info('(PID=%s) Actions notifier started.', os.getpid())
31
    actions_notifier = notifier.get_notifier()
32
    actions_rescheduler = scheduler.get_rescheduler()
33
    try:
34
        eventlet.spawn(actions_rescheduler.start)
35
        actions_notifier.start(wait=True)
36
    except (KeyboardInterrupt, SystemExit):
37
        LOG.info('(PID=%s) Actions notifier stopped.', os.getpid())
38
        actions_rescheduler.shutdown()
39
        actions_notifier.shutdown()
40
    except:
41
        return 1
42
    return 0
43
44
45
def _teardown():
46
    common_teardown()
47
48
49
def main():
50
    try:
51
        _setup()
52
        return _run_worker()
53
    except SystemExit as exit_code:
54
        sys.exit(exit_code)
55
    except:
56
        LOG.exception('(PID=%s) Results tracker quit due to exception.', os.getpid())
57
        return 1
58
    finally:
59
        _teardown()
60