Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

Commit

Permalink
Graceful shutdown of the worker
Browse files Browse the repository at this point in the history
Thanks @grvhi for the implementation
  • Loading branch information
igalarzab committed Aug 29, 2017
1 parent 24491ad commit f34557a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sqjobs/worker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import signal
import sys
import traceback

Expand All @@ -16,6 +17,10 @@ def __init__(self, broker, queue_name, timeout=None):
self.timeout = timeout or self.DEFAULT_TIMEOUT
self.registered_jobs = {}
self.exception_handlers = []
self._shutting_down = False

signal.signal(signal.SIGINT, self._exit_gracefully)
signal.signal(signal.SIGTERM, self._exit_gracefully)

def __repr__(self):
return 'Worker({connector})'.format(
Expand Down Expand Up @@ -43,6 +48,9 @@ def run(self):
logger.info('Running worker, %d jobs registered...', len(self.registered_jobs))

for payload in self.broker.jobs(self.queue_name, self.timeout):
if self._shutting_down:
break

try:
job_class = self.registered_jobs.get(payload['name'])

Expand Down Expand Up @@ -95,3 +103,7 @@ def _handle_exception(self, job, args, kwargs, *exc_info):
for handler in reversed(self.exception_handlers):
logger.debug('Executing exception handler %s', handler)
handler(job, args, kwargs, *exc_info)

def _exit_gracefully(self, signum, frame):
logger.info('Shutting down the worker...')
self._shutting_down = True

0 comments on commit f34557a

Please sign in to comment.