Forked from mywaiting/graceful_shutdown_tornado_web_server.py
Last active
October 23, 2024 16:13
-
-
Save wonderbeyond/d38cd85243befe863cdde54b84505784 to your computer and use it in GitHub Desktop.
The example to how to shutdown tornado web server gracefully...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
How to use it: | |
1. Just `kill -2 PROCESS_ID` or `kill -15 PROCESS_ID`, | |
The Tornado Web Server Will shutdown after process all the request. | |
2. When you run it behind Nginx, it can graceful reboot your production server. | |
""" | |
import time | |
import signal | |
import logging | |
from functools import partial | |
import tornado.httpserver | |
import tornado.ioloop | |
import tornado.options | |
import tornado.web | |
from tornado.options import define, options | |
define("port", default=8888, help="run on the given port", type=int) | |
MAX_WAIT_SECONDS_BEFORE_SHUTDOWN = 3 | |
class MainHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write("Hello, world") | |
def sig_handler(server, sig, frame): | |
io_loop = tornado.ioloop.IOLoop.instance() | |
def stop_loop(deadline): | |
now = time.time() | |
if now < deadline and (io_loop._callbacks or io_loop._timeouts): | |
logging.info('Waiting for next tick') | |
io_loop.add_timeout(now + 1, stop_loop, deadline) | |
else: | |
io_loop.stop() | |
logging.info('Shutdown finally') | |
def shutdown(): | |
logging.info('Stopping http server') | |
server.stop() | |
logging.info('Will shutdown in %s seconds ...', | |
MAX_WAIT_SECONDS_BEFORE_SHUTDOWN) | |
stop_loop(time.time() + MAX_WAIT_SECONDS_BEFORE_SHUTDOWN) | |
logging.warning('Caught signal: %s', sig) | |
io_loop.add_callback_from_signal(shutdown) | |
def main(): | |
tornado.options.parse_command_line() | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
server = tornado.httpserver.HTTPServer(application) | |
server.listen(options.port) | |
signal.signal(signal.SIGTERM, partial(sig_handler, server)) | |
signal.signal(signal.SIGINT, partial(sig_handler, server)) | |
tornado.ioloop.IOLoop.instance().start() | |
logging.info("Exit...") | |
if __name__ == "__main__": | |
main() |
@ewhauser, what is FLAGS.TORNADO_SHUTDOWN_WAIT
? Where did you get this from? Is it the same as MAX_WAIT_SECONDS_BEFORE_SHUTDOWN
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ewhauser thanks, I'll try that!
Not sure what I am missing.
As far as I understand, in a rolling-update, the pod state should change to "Terminating" state and stop receiving new traffic.
That's why my current
/ready
path simply returns 200 and my sig_handler for SIGTERM just prints a log and that it (to prevent the app from being closed and let the currently processing requests time to finish.