Task Retention

By default, once a task is successfully processed by the Handler (i.e., the Handler.ProcessTask returns nil), the task will be removed from the queue. However, if you want the task to be retained in the queue after completion (e.g., for inspection purposes), you can specify a retention period for the task.

Below is an example of using the Retention option to specify that a task should be retained in the queue for 24 hours after completion.

// Set the option when initializing the task.
task := asynq.NewTask("my_task", payload, asynq.Retention(24 * time.Hour))

// Alternatively, set the option when queuing the task.
info, err := client.Enqueue(task, asynq.Retention(24 * time.Hour))

After setting this option, you should be able to see the completed tasks using the CLI or Web UI.

Task Result

If you need to store some data related to the task during task processing, and if this data is only needed during the task's lifecycle (i.e., until the task is removed from the queue), you can store the data together with the task.

Use ResultWriter to write data to redis and associate the written data with the task.

Note: Please carefully consider the amount of data written to redis. If a large amount of data needs to be stored, it's better to use a disk-based storage system such as a SQL database.

// In the handler code.
func MyHandler(ctx context.Context, task *asynq.Task) error {
    res, err := DoStuff(ctx, task)
    if err != nil {
        return fmt.Errorf("failed to process task: %v", err)
    }
    if _, err := task.ResultWriter().Write(res); err != nil {
        return fmt.Errorf("failed to write task result: %v", err)
    }
    return nil
}

If you use the Retention option as shown above, you will be able to view the result data via the CLI and Web UI. Additionally, you can programmatically access the result data through the use of Inspector.GetTaskInfo and Inspector.ListCompletedTasks.