Add SIGINT/SIGTERM handler

This commit is contained in:
Tulir Asokan 2018-10-20 21:01:13 +03:00
parent d7bbe1498a
commit 1ffc536f0c

View File

@ -20,6 +20,7 @@ import argparse
import asyncio import asyncio
import copy import copy
import sys import sys
import signal
from .config import Config from .config import Config
from .db import Base, init as init_db from .db import Base, init as init_db
@ -63,16 +64,20 @@ plugins = PluginInstance.all()
for plugin in plugins: for plugin in plugins:
plugin.load() plugin.load()
signal.signal(signal.SIGINT, signal.default_int_handler)
signal.signal(signal.SIGTERM, signal.default_int_handler)
try: try:
loop.run_until_complete(asyncio.gather( loop.run_until_complete(asyncio.gather(
server.start(), server.start(),
*[plugin.start() for plugin in plugins])) *[plugin.start() for plugin in plugins]))
log.debug("Startup actions complete, running forever.") log.debug("Startup actions complete, running forever")
loop.run_forever() loop.run_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
log.debug("Keyboard interrupt received, stopping...") log.debug("Interrupt received, stopping HTTP clients/servers and saving database")
for client in Client.cache.values(): for client in Client.cache.values():
client.stop() client.stop()
db_session.commit() db_session.commit()
loop.run_until_complete(server.stop()) loop.run_until_complete(server.stop())
log.debug("Everything stopped, shutting down")
sys.exit(0) sys.exit(0)