From ed4664dc078002773ebb4be78496a6fb9fe50ed9 Mon Sep 17 00:00:00 2001 From: Alex Olson Date: Sat, 17 Oct 2015 05:08:41 -0600 Subject: [PATCH] handle sigterm just like a ctrl-c (keyboard interrupt) (cherry picked from commit ff53b616242b1d34df9a0b23f9658bdf72620069) --- slackbotExercise.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/slackbotExercise.py b/slackbotExercise.py index d66d712..8a40e5e 100644 --- a/slackbotExercise.py +++ b/slackbotExercise.py @@ -1,3 +1,5 @@ +import sys +import signal import random import time import requests @@ -17,7 +19,6 @@ HASH = "%23" - # Configuration values to be set in setConfiguration class Bot: def __init__(self): @@ -281,6 +282,11 @@ def isOfficeHours(bot): return False def main(): + + # Handle a SIGTERM so that killing the bot via other methods (like "docker stop") + # triggers the same functionality as Ctrl-C (KeyboardInterrupt) + signal.signal(signal.SIGTERM, lambda signum, frame: sys.exit(0)) + bot = Bot() try: @@ -303,7 +309,7 @@ def main(): # If debugging, check again in 5 seconds time.sleep(5) - except KeyboardInterrupt: + except (KeyboardInterrupt, SystemExit): saveUsers(bot)