Completed
Pull Request — master (#2549)
by Manas
04:56
created

st2api.cmd.main()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 3
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
import os
17
import sys
18
19
import eventlet
20
from oslo_config import cfg
21
from eventlet import wsgi
22
23
from st2common import log as logging
24
from st2common.service_setup import setup as common_setup
25
from st2common.service_setup import teardown as common_teardown
26
from st2common.util.monkey_patch import monkey_patch
27
from st2api import config
28
config.register_opts()
29
from st2api import app
30
31
__all__ = [
32
    'main'
33
]
34
35
monkey_patch()
36
37
LOG = logging.getLogger(__name__)
38
39
# How much time to give to the request in progress to finish in seconds before killing them
40
WSGI_SERVER_REQUEST_SHUTDOWN_TIME = 2
41
42
43
def _setup():
44
    common_setup(service='api', config=config, setup_db=True, register_mq_exchanges=True,
45
                 register_signal_handlers=True, register_internal_trigger_types=True)
46
47
48
def _run_server():
49
    host = cfg.CONF.api.host
50
    port = cfg.CONF.api.port
51
52
    LOG.info('(PID=%s) ST2 API is serving on http://%s:%s.', os.getpid(), host, port)
53
54
    max_pool_size = eventlet.wsgi.DEFAULT_MAX_SIMULTANEOUS_REQUESTS
55
    worker_pool = eventlet.GreenPool(max_pool_size)
56
    sock = eventlet.listen((host, port))
57
58
    wsgi.server(sock, app.setup_app(), custom_pool=worker_pool)
59
    return 0
60
61
62
def _teardown():
63
    common_teardown()
64
65
66
def main():
67
    try:
68
        _setup()
69
        return _run_server()
70
    except SystemExit as exit_code:
71
        sys.exit(exit_code)
72
    except Exception:
73
        LOG.exception('(PID=%s) ST2 API quit due to exception.', os.getpid())
74
        return 1
75
    finally:
76
        _teardown()
77