MCPcopy Create free account
hub / github.com/celery/celery / _shutdown_handler

Function _shutdown_handler

celery/apps/worker.py:324–363  ·  view source on GitHub ↗

Install signal handler for warm/cold shutdown. The handler will run from the MainProcess. Args: worker (Worker): The worker that received the signal. sig (str, optional): The signal that was received. Defaults to 'TERM'. how (str, optional): The type of shutdown to

(worker: Worker, sig='SIGTERM', how='Warm', callback=None, exitcode=EX_OK, verbose=True)

Source from the content-addressed store, hash-verified

322
323
324def _shutdown_handler(worker: Worker, sig='SIGTERM', how='Warm', callback=None, exitcode=EX_OK, verbose=True):
325 """Install signal handler for warm/cold shutdown.
326
327 The handler will run from the MainProcess.
328
329 Args:
330 worker (Worker): The worker that received the signal.
331 sig (str, optional): The signal that was received. Defaults to 'TERM'.
332 how (str, optional): The type of shutdown to perform. Defaults to 'Warm'.
333 callback (Callable, optional): Signal handler. Defaults to None.
334 exitcode (int, optional): The exit code to use. Defaults to EX_OK.
335 verbose (bool, optional): Whether to print the type of shutdown. Defaults to True.
336 """
337 def _handle_request(*args):
338 with in_sighandler():
339 from celery.worker import state
340 try:
341 if current_process()._name == 'MainProcess':
342 if callback:
343 callback(worker)
344 if verbose:
345 safe_say(f'worker: {how} shutdown (MainProcess)', sys.__stdout__)
346 signals.worker_shutting_down.send(
347 sender=worker.hostname, sig=sig, how=how,
348 exitcode=exitcode,
349 )
350 finally:
351 # Guarantee the shutdown flag is set even if the callback,
352 # safe_say(), or the signal dispatch above raises — this is
353 # the actual mechanism that stops the worker from accepting
354 # new work. Kept in the *finally*, not moved ahead of this
355 # block, because on_cold_shutdown() relies on should_terminate
356 # staying unset while it runs: it waits out
357 # worker_soft_shutdown_timeout before cancelling tasks, and
358 # state.maybe_shutdown()/Request.on_timeout()/on_failure()
359 # all branch on should_terminate during that window.
360 setattr(state, {'Warm': 'should_stop',
361 'Cold': 'should_terminate'}[how], exitcode)
362 _handle_request.__name__ = str(f'worker_{how}')
363 platforms.signals[sig] = _handle_request
364
365
366def on_hard_shutdown(worker: Worker):

Callers 1

_installMethod · 0.90

Calls

no outgoing calls

Tested by 1

_installMethod · 0.72