graceful shutdown management

This commit is contained in:
Paolo Asperti 2018-01-15 22:11:23 +01:00
parent 2223711f6b
commit f650a5770f

View File

@ -12,6 +12,7 @@ import os
import argparse import argparse
import logging import logging
import atexit import atexit
import signal
from telepot.loop import MessageLoop from telepot.loop import MessageLoop
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler from watchdog.events import PatternMatchingEventHandler
@ -167,6 +168,15 @@ def checkFiles():
processFile("%s/%s" % (spool_dir,f) ) processFile("%s/%s" % (spool_dir,f) )
def initSignals():
signal.signal(signal.SIGINT, exitGracefully)
signal.signal(signal.SIGTERM, exitGracefully)
def exitGracefully(signum, frame):
global shutdown
shutdown = True
def setDefaults(): def setDefaults():
global configFile global configFile
global defaults global defaults
@ -300,7 +310,7 @@ def initBot():
observer.schedule(FilesChangedHandler(), path=spool_dir) observer.schedule(FilesChangedHandler(), path=spool_dir)
observer.start() observer.start()
initSignals()
setDefaults() setDefaults()
parseCmdLine() parseCmdLine()
readConfigFile() readConfigFile()
@ -311,8 +321,13 @@ initBot()
daemon = configParser.get('general', 'daemon') daemon = configParser.get('general', 'daemon')
shutdown = False
while 1: while 1:
checkFiles() checkFiles()
if re.match('false', daemon, re.IGNORECASE): if re.match('false', daemon, re.IGNORECASE):
sys.exit(0) sys.exit(0)
time.sleep(10) for x in range(0, 40):
if shutdown:
logger.info('Daemon shutdown requested. Exiting...')
sys.exit(0)
time.sleep(0.25)