Skip to content

Commit

Permalink
Merge c6589c4 into 80b6aa3
Browse files Browse the repository at this point in the history
  • Loading branch information
willgraf committed May 17, 2019
2 parents 80b6aa3 + c6589c4 commit d105d35
Show file tree
Hide file tree
Showing 5 changed files with 345 additions and 160 deletions.
26 changes: 25 additions & 1 deletion consume-redis-events.py
Expand Up @@ -32,13 +32,32 @@
from __future__ import print_function

import sys
import signal
import traceback
import logging
import logging.handlers

import redis_consumer
from redis_consumer import settings
from redis_consumer import storage


class GracefulDeath:
"""Catch signals to allow graceful shutdown.
Adapted from: https://stackoverflow.com/questions/18499497
"""

def __init__(self):
self.signum = None
self.kill_now = False
self.logger = logging.getLogger(str(self.__class__.__name__))
signal.signal(signal.SIGINT, self.handle_signal)
signal.signal(signal.SIGTERM, self.handle_signal)

def handle_signal(self, signum, frame): # pylint: disable=unused-argument
self.signum = signum
self.kill_now = True
self.logger.debug('Received signal `%s` and frame `%s`', signum, frame)


def initialize_logger(debug_mode=True):
Expand Down Expand Up @@ -76,6 +95,7 @@ def get_consumer(consumer_type, **kwargs):

if __name__ == '__main__':
initialize_logger(settings.DEBUG)
sighandler = GracefulDeath()

_logger = logging.getLogger(__file__)

Expand All @@ -98,8 +118,12 @@ def get_consumer(consumer_type, **kwargs):
while True:
try:
consumer.consume()
if sighandler.kill_now:
break
except Exception as err: # pylint: disable=broad-except
_logger.critical('Fatal Error: %s: %s\n%s',
type(err).__name__, err, traceback.format_exc())

sys.exit(1)

_logger.info('Gracefully exited after signal number %s', sighandler.signum)

0 comments on commit d105d35

Please sign in to comment.