Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for graceful shutdown in SIGTERM #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added support for graceful shutdown in SIGTERM
  • Loading branch information
Apple authored and Apple committed Nov 1, 2023
commit 843eae0e803e521b7912cb341b97e631d0a55220
26 changes: 15 additions & 11 deletions huey/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,22 @@ def check_worker_health(self):
return not restart_occurred

def _set_signal_handlers(self):
signal.signal(signal.SIGTERM, self._handle_stop_signal)
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)
if hasattr(signal, 'SIGTERM'):
signal.signal(signal.SIGTERM, signal.default_int_handler)
else:
signal.signal(signal.SIGINT, signal.default_int_handler)
if hasattr(signal, 'SIGHUP'):
signal.signal(signal.SIGHUP, self._handle_restart_signal)
signal.signal(signal.SIGTERM, self._handle_stop_signal)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shubhrajyoti PRs need to be more explanative.. why is this needed?

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')
Expand Down