Count the failures, and stop at max_attempts
A task that fails every time would bounce between waiting and reserved forever. A bad input, such as a corrupt file, never gets better on a retry. So the queue counts each task's failed attempts and gives up at a limit, max_attempts.
Say exactly what the limit means before you code it. In the practice problem, max_attempts=3 means a task may be tried three times. The third failure is the last, so it sends the task to the dead-letter queue.
| Failure | Count | With max_attempts=3 |
|---|---|---|
| first | 1 | back to waiting |
| second | 2 | back to waiting |
| third | 3 | dead |
The test is failures >= max_attempts. With >, the task gets a fourth try. That off-by-one is easy to write and easy to miss, because every test where the task succeeds still passes.
Two more details:
- Each task has its own count. A dict from task id to failures does it.
max_attemptsbelow 1 makes no sense, since a task must be tried at least once. RaiseValueErrorin the constructor.
The dead-letter queue, or DLQ, is just a list of the dead task ids, in the order they died. The task isn't deleted. It's set aside, so a person can look at why it failed.
max_attempts=2. Task a fails, task b fails, then task a fails again. Which tasks are dead?