From 1ffc536f0c5c8d15ef0d61429ddfcb370566f637 Mon Sep 17 00:00:00 2001
From: Tulir Asokan <tulir@maunium.net>
Date: Sat, 20 Oct 2018 21:01:13 +0300
Subject: [PATCH] Add SIGINT/SIGTERM handler

---
 maubot/__main__.py | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/maubot/__main__.py b/maubot/__main__.py
index aaae853..08ec358 100644
--- a/maubot/__main__.py
+++ b/maubot/__main__.py
@@ -20,6 +20,7 @@ import argparse
 import asyncio
 import copy
 import sys
+import signal
 
 from .config import Config
 from .db import Base, init as init_db
@@ -63,16 +64,20 @@ plugins = PluginInstance.all()
 for plugin in plugins:
     plugin.load()
 
+signal.signal(signal.SIGINT, signal.default_int_handler)
+signal.signal(signal.SIGTERM, signal.default_int_handler)
+
 try:
     loop.run_until_complete(asyncio.gather(
         server.start(),
         *[plugin.start() for plugin in plugins]))
-    log.debug("Startup actions complete, running forever.")
+    log.debug("Startup actions complete, running forever")
     loop.run_forever()
 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():
         client.stop()
     db_session.commit()
     loop.run_until_complete(server.stop())
+    log.debug("Everything stopped, shutting down")
     sys.exit(0)