Skip to content

Commit

Permalink
Add special INT handler for gevent, and fix TERM behavior as well.
Browse files Browse the repository at this point in the history
Refs #755
  • Loading branch information
coleifer committed Jul 18, 2023
1 parent 28b024b commit 361e380
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion huey/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,15 +497,33 @@ def check_worker_health(self):

def _set_signal_handlers(self):
signal.signal(signal.SIGTERM, self._handle_stop_signal)
signal.signal(signal.SIGINT, signal.default_int_handler)
if self.worker_type == WORKER_GREENLET:
# Add a special INT handler when using gevent. If the running
# greenlet is not the main hub, then Gevent will raise a
# KeyboardInterrupt in the running greenlet by default. This
# ensures that when INT is received we properly flag the main loop
# for graceful shutdown and do NOT propagate the exception.
signal.signal(signal.SIGINT, self._handle_interrupt_signal_gevent)
else:
signal.signal(signal.SIGINT, signal.default_int_handler)
if hasattr(signal, 'SIGHUP'):
signal.signal(signal.SIGHUP, self._handle_restart_signal)

def _handle_interrupt_signal_gevent(self, sig_num, frame):
self._logger.info('Received SIGINT')
self._received_signal = True
self._restart = False
self._graceful = True
signal.signal(signal.SIGINT, signal.default_int_handler)

def _handle_stop_signal(self, sig_num, frame):
self._logger.info('Received SIGTERM')
self._received_signal = True
self._restart = False
self._graceful = False
if self.worker_type == WORKER_GREENLET:
# Ensure that the running greenlet (if not main) is interrupted.
raise KeyboardInterrupt

def _handle_restart_signal(self, sig_num, frame):
self._logger.info('Received SIGHUP, will restart')
Expand Down

0 comments on commit 361e380

Please sign in to comment.