MCPcopy Create free account
hub / github.com/StackStorm/st2 / shutdown_server_kill_pending_requests

Function shutdown_server_kill_pending_requests

st2common/st2common/util/wsgi.py:30–59  ·  view source on GitHub ↗

Custom WSGI server shutdown function which gives outgoing requests some time to finish before killing them. Without that, long running requests such as stream block and prevent server from shutting down. :param sock: WSGI server socket. :param worker_pool: WSGI server worker p

(sock, worker_pool, wait_time=2)

Source from the content-addressed store, hash-verified

28
29
30def shutdown_server_kill_pending_requests(sock, worker_pool, wait_time=2):
31 """
32 Custom WSGI server shutdown function which gives outgoing requests some time to finish
33 before killing them.
34
35 Without that, long running requests such as stream block and prevent server from shutting down.
36
37 :param sock: WSGI server socket.
38 :param worker_pool: WSGI server worker pool.
39 :param wait_time: How long to give to the active requests to finish processing before
40 forcefully killing them.
41 :type wait_time: ``int``
42 """
43 worker_pool.resize(0)
44 sock.close()
45
46 active_requests = worker_pool.running()
47 LOG.info("Shutting down. Requests left: %s", active_requests)
48
49 # Give active requests some time to finish
50 if active_requests > 0:
51 eventlet.sleep(wait_time)
52
53 # Kill requests which still didn't finish
54 running_corutines = worker_pool.coroutines_running.copy()
55 for coro in running_corutines:
56 eventlet.greenthread.kill(coro)
57
58 LOG.info("Exiting...")
59 raise SystemExit()

Callers

nothing calls this directly

Calls 3

runningMethod · 0.80
killMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected