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

st2stream.cmd._run_server()   A

Complexity

Conditions 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 9.3142
cc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A st2stream.cmd.queue_shutdown() 0 3 1
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.wsgi import shutdown_server_kill_pending_requests
27
from st2stream.signal_handlers import register_stream_signal_handlers
28
from st2stream.listener import get_listener_if_set
29
from st2stream import config
30
config.register_opts()
31
from st2stream import app
32
33
__all__ = [
34
    'main'
35
]
36
37
38
eventlet.monkey_patch(
39
    os=True,
40
    select=True,
41
    socket=True,
42
    thread=False if '--use-debugger' in sys.argv else True,
43
    time=True)
44
45
LOG = logging.getLogger(__name__)
46
47
# How much time to give to the request in progress to finish in seconds before killing them
48
WSGI_SERVER_REQUEST_SHUTDOWN_TIME = 2
49
50
51
def _setup():
52
    common_setup(service='stream', config=config, setup_db=True, register_mq_exchanges=True,
53
                 register_signal_handlers=True, register_internal_trigger_types=False,
54
                 run_migrations=False)
55
56
57
def _run_server():
58
    host = cfg.CONF.stream.host
59
    port = cfg.CONF.stream.port
60
61
    LOG.info('(PID=%s) ST2 Stream API is serving on http://%s:%s.', os.getpid(), host, port)
62
63
    max_pool_size = eventlet.wsgi.DEFAULT_MAX_SIMULTANEOUS_REQUESTS
64
    worker_pool = eventlet.GreenPool(max_pool_size)
65
    sock = eventlet.listen((host, port))
66
67
    def queue_shutdown(signal_number, stack_frame):
68
        eventlet.spawn_n(shutdown_server_kill_pending_requests, sock=sock,
69
                         worker_pool=worker_pool, wait_time=WSGI_SERVER_REQUEST_SHUTDOWN_TIME)
70
71
    # We register a custom SIGINT handler which allows us to kill long running active requests.
72
    # Note: Eventually we will support draining (waiting for short-running requests), but we
73
    # will still want to kill long running stream requests.
74
    register_stream_signal_handlers(handler_func=queue_shutdown)
75
76
    wsgi.server(sock, app.setup_app(), custom_pool=worker_pool)
77
    return 0
78
79
80
def _teardown():
81
    common_teardown()
82
83
84
def main():
85
    try:
86
        _setup()
87
        return _run_server()
88
    except SystemExit as exit_code:
89
        sys.exit(exit_code)
90
    except KeyboardInterrupt:
91
        listener = get_listener_if_set()
92
93
        if listener:
94
            listener.shutdown()
95
    except Exception:
96
        LOG.exception('(PID=%s) ST2 Stream API quit due to exception.', os.getpid())
97
        return 1
98
    finally:
99
        _teardown()
100