handle sigint

This commit is contained in:
Victor Shyba 2019-03-11 14:35:58 -03:00 committed by Jack Robison
parent b266cde511
commit f390ff144d
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -1,5 +1,6 @@
import os
import sys
import signal
import pathlib
import json
import asyncio
@ -272,8 +273,20 @@ def main(argv=None):
log.addHandler(loggly_handler)
daemon = Daemon(conf)
started = False
def __exit():
if started:
daemon.stop_event.set()
else:
raise GracefulExit()
try:
loop.add_signal_handler(signal.SIGINT, __exit)
loop.add_signal_handler(signal.SIGTERM, __exit)
except NotImplementedError:
pass # Not implemented on Windows
try:
loop.run_until_complete(daemon.start())
started = True
loop.run_until_complete(daemon.stop_event.wait())
except (GracefulExit, KeyboardInterrupt):
pass