Completed
Push — master ( 42b44a...334d31 )
by Edward
03:30
created

st2actions.cmd._run_worker()   A

Complexity

Conditions 4

Size

Total Lines 18

Duplication

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