# Add Retries with Backoff to RQ Jobs

Yes. An agent adds RQ's Retry with intervals, turns on the worker's scheduler so retried jobs actually run again, and proves it with a job that fails.

[Run this on Rush](https://cloud.getrush.ai/run/add-task-retries/rq/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

RQ has had retries since 1.5, so the agent's first finding is that no upgrade is needed. In an app like mjhea0/flask-redis-queue, `requirements.txt` pins `rq==1.10.1`, and `run_task` in `project/server/main/views.py` enqueues with a bare `q.enqueue(create_task, task_type)`. The agent changes that call to pass `retry=Retry(max=3, interval=[10, 30, 60])`, the form RQ's documentation gives for a growing delay, and imports `Retry` beside `Queue`.

That one line is where most attempts stop, and it is not enough. A job that fails with an `interval` set is not put back on the queue. It is parked with the status `scheduled`, and only a worker that runs RQ's scheduler moves it back when its time comes. `run_worker` in `manage.py` calls `worker.work()` with no arguments, so in this app a retried job would sit in `scheduled` until someone noticed. The agent changes the call to `worker.work(with_scheduler=True)`, the code form of the `--with-scheduler` flag the RQ docs tell you not to forget, and says in the pull request that every place a worker is started needs the same change, including the `worker` service in `docker-compose.yml`, which runs that command.

Then it decides what should not be retried, because RQ 1.10's `Retry` reruns the job on any exception. `create_task` in `project/server/main/tasks.py` calls `int(task_type)`, and the route passes `request.form["type"]` straight through, so a request with a type that is not a number fails the same way on every attempt and would burn all three retries with their delays. The agent validates the type in the route and answers 400 before anything is enqueued, which keeps retries for failures that might pass the next time. It also adds the attempts left to the JSON from `/tasks/<task_id>`, since the script in `project/client/static/main.js` stops polling only on `finished` or `failed` and will now see `scheduled` in between.

To prove it, the agent needs a job that fails on purpose, since the only task in the app sleeps and returns true. It adds a small test task that counts its attempts in Redis and raises until the count reaches a number passed as an argument. The sandbox has no Docker, so it installs Redis with `sudo apt-get install -y redis-server` and runs the web app and `python manage.py run_worker` as processes, with the `redis` hostname from `project/server/config.py` mapped to localhost. It enqueues the failing task twice: once against a worker started the old way, to show the job stuck in `scheduled`, and once with the scheduler on, to show each attempt arrive after its interval and the job end `finished`. A third run exhausts the retries and ends `failed`.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "codex",
  "input": "A Linear issue labelled retries asks for failed background jobs in this Flask and RQ app to be retried with a growing delay. Read requirements.txt, manage.py, project/server/config.py, project/server/main/views.py, project/server/main/tasks.py, project/client/static/main.js and docker-compose.yml first. Do not upgrade rq; say which release added Retry and confirm the pinned one has it. Enqueue with retry=Retry(max=3, interval=[10, 30, 60]). Find out what state a failed job with an interval is left in and what a worker needs for it to run again, then change every place a worker is started accordingly and list any you could not change. Decide which failures should not be retried: look at what create_task does with its argument, and reject bad input in the route with a 400 before enqueueing. Add the number of attempts left to the status JSON without removing a field, and say what main.js does when it sees a status other than finished or failed. Do not use Docker: install redis-server with apt, run Redis, the web app and python manage.py run_worker as processes, and make the redis hostname resolve locally without editing config.py. Add a test-only task that fails a set number of times. Show three cases with the status responses and worker log for each: a retried job left unscheduled by a worker without the scheduler, a job that succeeds on its third attempt with the scheduler on, and a job that exhausts its retries. If pip install of the pinned requirements fails for an unrelated reason, fix only that in a separate commit. Open a pull request that links the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `views.py` and `manage.py`, adds the test task, and touches `requirements.txt` only if the pinned install needed a fix to run at all. The description states the retry policy in one line, then the part readers usually miss: with an interval, a failed job waits as `scheduled`, and the worker must run with the scheduler for it to come back.

The evidence is three short transcripts. In the first, a worker started without the scheduler runs the job once, the status route reports `scheduled`, and it still reports `scheduled` after the first interval has passed. In the second, with `with_scheduler=True`, the worker log shows the same job id on each attempt, the attempts-left number in the status JSON falls, and the job ends `finished`. In the third the job fails every time and ends `failed` once nothing is left. Beside them is the 400 for a non-numeric type, with a note that before the change the same request was accepted with a 202 and failed inside the worker.

A reviewer checks the things retries make worse. A retried job runs again from its first line, so any task that sends an email or charges a card must be safe to repeat before it gets this policy. The intervals add up to well over a minute before a job is reported `failed`, which changes how long a user watches the status table. And the worker change has to reach production: a deployment that starts workers with the `rq worker` command needs `--with-scheduler` there too, or the new policy turns failures into jobs that never finish.

## Run it on every Linear issue labelled retries

An automation in the Project that holds the repository, with a `linear` trigger on `linear.issue.labeled` and `retries` in its `labels`, starts one run each time someone adds that label. The issue is placed after the saved prompt and fills in nothing, so the prompt tells the agent to read which job and which failure the issue describes from the text that follows it. Add `teams` with your team's key if only one team's issues should fire it.

The automation runs as the member who last changed it and uses that member's Linear connection, made in the console under Settings → Connections. A label added by an app integration does not fire it, and neither does one added by a person who is not in your Rush org. If a triage session labels several issues at once, the automation's max runs per hour decides how many start straight away; the rest are created `queued` and start when the hour has room.

## Why run it on Rush?

The bug this page is about cannot be seen in a diff. `retry=Retry(...)` reads as complete, the app starts, jobs still succeed, and the missing scheduler only shows when a job fails and then does not come back. In a Rush sandbox the agent runs a real Redis and a real worker, makes a job fail, waits out the interval and looks at the status, first the wrong way and then the right way. Both transcripts are kept in the run's record, so the pull request shows the failure it prevents.

Waiting is also cheap there. Proving a backoff of ten, thirty and sixty seconds means sitting through those delays three times over, which nobody enjoys doing at a desk. The run does it on its own machine while the person who labelled the issue works on something else, and the result arrives as a pull request linked from the issue.

## Frequently asked questions

### Why are my retried RQ jobs stuck in scheduled?

Because the worker is not running the scheduler. A failed job whose `Retry` has an `interval` is stored with the status `scheduled`, and a worker moves it back to the queue only when it was started with `--with-scheduler`, or `work(with_scheduler=True)` in code. A `Retry` with no interval is requeued at once and does not need it.

### Do I need rq-scheduler for this?

No. The scheduler that handles retry intervals is built into RQ and runs inside the worker process when you ask for it. `rq-scheduler` is a separate package for other scheduling work and is not required for retries.

### Can I retry only on some exceptions?

Not with `Retry` alone in RQ 1.10, which reruns the job whatever was raised. The practical approach is the one the agent takes: reject input that can never succeed before it is enqueued, and inside the task catch errors that are permanent and return a failure value. The pull request says which failures still reach the retry policy.

### Does it change the tasks that already work?

Only how they are enqueued. A job that succeeds on its first attempt behaves as before, and the status JSON keeps its existing fields with one added. The agent runs one ordinary task after the change and includes its responses to show that.

## More RQ jobs

- [Upgrade RQ 1.x to RQ 3 in a Flask App](https://getrush.ai/run/upgrade-rq/rq/)
- [Add Job Timeouts to a Flask RQ App](https://getrush.ai/run/add-job-timeouts/rq/)
- [Add a Scheduled Job to a Flask App That Uses RQ](https://getrush.ai/run/add-periodic-tasks/rq/)

## The same job on other stacks

- [Add Retries with Backoff to Celery Tasks](https://getrush.ai/run/add-task-retries/celery/)

## More jobs like this: Turn a Linear label into a PR

- [Add Task Routing to a Celery App](https://getrush.ai/run/add-task-routing/celery/)
- [Add Task Time Limits to a Flask Celery App](https://getrush.ai/run/add-task-timeouts/celery/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
