Skip to content
Ken Hibino edited this page Jan 20, 2020 · 11 revisions

This page is an in-depth guide to task lifecycle.

Good Understanding in task lifecycle helps you better understand the use of asynqmon CLI.

When you create and schedule a task, asynq manages the task internally to make sure that a handler gets invoked with the task at the specified time. In the process, the task can go through different lifecycle states.

Here's the list of different lifecycle states:

  • Scheduled : A task is in the scheduled state when it's pending to be processed in the future.
  • Enqueued : A task is in the enqueued state when it's ready to be processed.
  • InProgress : A task is in the in-progress state when it's being processed by a worker (i.e. handler is invoked with the task).
  • Retry : A task is in the retry state after a failed attempt to process the task and is pending to be retried in the future.
  • Dead : A task is in the dead state when it exhausted all of its retry count.

Let's use an example to look at different lifecycle states.

// Task 1 : Schedule to be processed 24 hours later.
client.Schedule(task1, time.Now().Add(24 * time.Hour))

// Task 2 : Schedule to be processed immediately.
client.Schedule(task2, time.Now())

In this example, task1 will stay in the scheduled state for the next 24 hours. After 24 hours, it will transition to the enqueued state and then to the in-progress state. If the processing of the task is successful then the task data is removed from Redis. If the processing of the task was NOT successful (i.e. handler returned non-nil error OR panicked), then the task will transition to retry state to be retried later. After some delay, the task will transition to the enqueued state again and then to the in-progress. This loop will continues until either task gets processed successfully OR task exhausts all of its retry count. In the latter case, the task will transition to the dead state.

The only difference between task2 and task1 in the example is that task2 will skip the scheduled state and goes directly to the enqueued state.

The diagram below shows the state transitions.

+-------------+            +--------------+          +--------------+
|             |            |              |          |              |
|  Scheduled  |----------->|   Enqueued   |--------->|  InProgress  |--> Success
|             |            |              |          |              |
+-------------+            +--------------+          +--------------+
                                  ^                       |
                                  |                       |
                                  |                       | Failed
                                  |                       |
                                  |                       |
                                  |                       |
                           +------+-------+               |        +------------+
                           |              |               |        |            |
                           |    Retry     |<--------------+------->|    Dead    |
                           |              |                        |            |
                           +--------------+                        +------------+