only set signal handler on server start and stop

This commit is contained in:
Izalia Mae 2022-12-20 06:08:17 -05:00
parent 4979d598f1
commit be556163c9

View file

@ -40,7 +40,6 @@ class Application(web.Application):
self['last_worker'] = 0 self['last_worker'] = 0
self.database.create() self.database.create()
self.set_signal_handler()
with self.database.session as s: with self.database.session as s:
set_level(s.get_config('log_level')) set_level(s.get_config('log_level'))
@ -98,10 +97,10 @@ class Application(web.Application):
self['last_worker'] = 0 self['last_worker'] = 0
def set_signal_handler(self): def set_signal_handler(self, enable=True):
for sig in {'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGTERM'}: for sig in {'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGTERM'}:
try: try:
signal.signal(getattr(signal, sig), self.stop) signal.signal(getattr(signal, sig), self.stop if enable else signal.SIG_DFL)
# some signals don't exist in windows, so skip them # some signals don't exist in windows, so skip them
except AttributeError: except AttributeError:
@ -129,6 +128,7 @@ class Application(web.Application):
async def handle_run(self): async def handle_run(self):
self.set_signal_handler(True)
self['running'] = True self['running'] = True
with self.database.session as s: with self.database.session as s:
@ -161,6 +161,7 @@ class Application(web.Application):
self['starttime'] = None self['starttime'] = None
self['running'] = False self['running'] = False
self['workers'].clear() self['workers'].clear()
self.set_signal_handler(False)
class PushWorker(threading.Thread): class PushWorker(threading.Thread):