Standalone server, do a clean shutdown on SIGTERM

This commit is contained in:
Kovid Goyal 2015-06-05 14:19:31 +05:30
parent 32be03b694
commit 2854665e78

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import sys, os import sys, os, signal
from calibre import as_unicode from calibre import as_unicode
from calibre.constants import plugins, iswindows from calibre.constants import plugins, iswindows
@ -67,6 +67,7 @@ class Server(object):
self.loop = ServerLoop(create_http_handler(self.handler.dispatch), opts=opts, log=log, plugins=plugins) self.loop = ServerLoop(create_http_handler(self.handler.dispatch), opts=opts, log=log, plugins=plugins)
self.handler.set_log(self.loop.log) self.handler.set_log(self.loop.log)
self.serve_forever = self.loop.serve_forever self.serve_forever = self.loop.serve_forever
self.stop = self.loop.stop
def create_option_parser(): def create_option_parser():
@ -127,4 +128,5 @@ def main(args=sys.argv):
if opts.pidfile: if opts.pidfile:
with lopen(opts.pidfile, 'wb') as f: with lopen(opts.pidfile, 'wb') as f:
f.write(str(os.getpid())) f.write(str(os.getpid()))
signal.signal(signal.SIGTERM, lambda s,f: server.stop())
server.serve_forever() server.serve_forever()