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)
| 28 | |
| 29 | |
| 30 | def 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() |