How a Procrastinate worker shuts down
mainA Procrastinate worker continues running until one of the following conditions is met:
- No jobs left: If the worker is started with
wait=False(the default isTrue), it will shut down once the queue is empty. - Stop signals: If
install_signal_handlers=True(the default isTrue), the worker will shut down upon receivingSIGINTorSIGTERMsignals. - Task cancellation: If the
asyncio.Taskcreated byapp.run_worker_asyncis cancelled viatask.cancel().
Graceful Shutdown Behavior
When a shutdown is requested, the worker attempts a graceful shutdown by waiting for running jobs to complete.
- With
shutdown_graceful_timeout: The worker will attempt to abort any jobs that have not completed within the specified timeout. - Without
shutdown_graceful_timeout: The worker will wait indefinitely for all running jobs to complete.
How jobs are aborted
When the worker aborts a job, it performs two actions:
- It sets the context so that
JobContext.should_abortreturnsAbortReason.SHUTDOWN. - It calls
task.cancel()on the underlyingasynciotask running the job (for asynchronous jobs).
Note: Jobs that do not respect the abort request (e.g., by checking should_abort or handling asyncio.CancelledError) will prevent the worker from shutting down until they finish. This effectively extends the graceful shutdown period for those specific jobs beyond the shutdown_graceful_timeout.
Forceful Termination: Procrastinate does not have a built-in method for forceful termination. To forcefully kill a worker, use your process manager (e.g., systemd, Docker, Kubernetes). In such cases, jobs may be left in a 'stale' state.