Skip to main content

Celery Task Lifecycle States

The Celery Task Lifecycle diagram illustrates the various states a task transitions through from its initial submission to its final resolution.

Key components and transitions:

  • Celery Task Lifecycle States: The initial state of a task. It is the default state when a task is first submitted and before it is picked up by a worker.
  • Celery Task Lifecycle States: A state used primarily in events to indicate that a worker has received the task message from the broker.
  • Celery Task Lifecycle States: Indicates that a worker has begun executing the task. This state is only recorded if the task_track_started setting is enabled.
  • Celery Task Lifecycle States: A terminal state indicating the task completed successfully and returned a result.
  • Celery Task Lifecycle States: A terminal state indicating the task failed with an exception or exceeded its retry limit.
  • Celery Task Lifecycle States: A state indicating the task failed but is being rescheduled for another attempt. This leads back to the PENDING state for the next execution.
  • Celery Task Lifecycle States: A terminal state indicating the task was cancelled, either before it started or while it was running (terminated).
  • Celery Task Lifecycle States: A state (mostly for events) indicating the worker rejected the task, for example, if it was malformed or expired.
  • Celery Task Lifecycle States: A state indicating the task was intentionally ignored by the user (e.g., via an Ignore exception).

The diagram shows the flow from submission through the worker's handling logic, including the possibility of retries and revocations at different stages.

Key Architectural Findings:

  • States are defined as constants in celery/states.py.
  • PENDING is the default state in the result backend when no record exists for a task ID.
  • RECEIVED and REJECTED are primarily event-driven states and are not typically persisted in result backends.
  • STARTED state tracking is optional and depends on the task_track_started configuration.
  • RETRY triggers the creation of a new task message, effectively restarting the lifecycle for that task ID.
  • REVOKED can be reached from PENDING, RECEIVED, or STARTED states if a revocation signal is received or the task is found in the revoked list.
  • IGNORED is a specific state reached when a task raises the celery.exceptions.Ignore exception.
Loading diagram...