1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
2 | # contributor license agreements. See the NOTICE file distributed with |
||
3 | # this work for additional information regarding copyright ownership. |
||
4 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
||
5 | # (the "License"); you may not use this file except in compliance with |
||
6 | # the License. You may obtain a copy of the License at |
||
7 | # |
||
8 | # http://www.apache.org/licenses/LICENSE-2.0 |
||
9 | # |
||
10 | # Unless required by applicable law or agreed to in writing, software |
||
11 | # distributed under the License is distributed on an "AS IS" BASIS, |
||
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
13 | # See the License for the specific language governing permissions and |
||
14 | # limitations under the License. |
||
15 | |||
16 | # Monkey patching should be done as early as possible. |
||
17 | # See http://eventlet.net/doc/patching.html#monkeypatching-the-standard-library |
||
18 | |||
19 | from __future__ import absolute_import |
||
20 | |||
21 | from st2common.util.monkey_patch import monkey_patch |
||
22 | monkey_patch() |
||
23 | |||
24 | import os |
||
25 | import signal |
||
26 | import sys |
||
27 | import traceback |
||
28 | |||
29 | from st2actions.workflows import config |
||
30 | from st2actions.workflows import workflows |
||
31 | from st2common import log as logging |
||
32 | from st2common.service_setup import setup as common_setup |
||
33 | from st2common.service_setup import teardown as common_teardown |
||
34 | |||
35 | __all__ = [ |
||
36 | 'main' |
||
37 | ] |
||
38 | |||
39 | LOG = logging.getLogger(__name__) |
||
40 | |||
41 | |||
42 | def setup_sigterm_handler(): |
||
43 | |||
44 | def sigterm_handler(signum=None, frame=None): |
||
45 | # This will cause SystemExit to be throw and allow for component cleanup. |
||
46 | sys.exit(0) |
||
47 | |||
48 | # Register a SIGTERM signal handler which calls sys.exit which causes SystemExit to |
||
49 | # be thrown. We catch SystemExit and handle cleanup there. |
||
50 | signal.signal(signal.SIGTERM, sigterm_handler) |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
51 | |||
52 | |||
53 | def setup(): |
||
54 | common_setup( |
||
55 | service='workflow_engine', |
||
56 | config=config, |
||
57 | setup_db=True, |
||
58 | register_mq_exchanges=True, |
||
59 | register_signal_handlers=True |
||
60 | ) |
||
61 | |||
62 | setup_sigterm_handler() |
||
63 | |||
64 | |||
65 | def run_server(): |
||
66 | LOG.info('(PID=%s) Workflow engine started.', os.getpid()) |
||
67 | |||
68 | engine = workflows.get_engine() |
||
69 | |||
70 | try: |
||
71 | engine.start(wait=True) |
||
72 | except (KeyboardInterrupt, SystemExit): |
||
73 | LOG.info('(PID=%s) Workflow engine stopped.', os.getpid()) |
||
74 | engine.shutdown() |
||
75 | except: |
||
76 | LOG.exception('(PID=%s) Workflow engine unexpectedly stopped.', os.getpid()) |
||
77 | return 1 |
||
78 | |||
79 | return 0 |
||
80 | |||
81 | |||
82 | def teardown(): |
||
83 | common_teardown() |
||
84 | |||
85 | |||
86 | def main(): |
||
87 | try: |
||
88 | setup() |
||
89 | return run_server() |
||
90 | except SystemExit as exit_code: |
||
91 | sys.exit(exit_code) |
||
92 | except Exception: |
||
93 | traceback.print_exc() |
||
94 | LOG.exception('(PID=%s) Workflow engine quit due to exception.', os.getpid()) |
||
95 | return 1 |
||
96 | finally: |
||
97 | teardown() |
||
98 |