# Add Task Time Limits to a Flask Celery App

Yes. An agent sets soft and hard time limits on a Flask app's Celery tasks, handles the soft limit so task state is kept, and proves it with a worker run.

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

## What the agent does

The agent starts from how the tasks are declared. In an app laid out like miguelgrinberg/flask-celery-example, everything is in `app.py`: the Celery app is built with `Celery(app.name, broker=app.config['CELERY_BROKER_URL'])` and `celery.conf.update(app.config)`, so configuration comes from the Flask config under the old upper-case names `CELERY_BROKER_URL` and `CELERY_RESULT_BACKEND`, both `redis://localhost:6379/0`. There are two tasks. `send_async_email` sends through Flask-Mail to `smtp.googlemail.com` on port 587 with TLS and credentials from `MAIL_USERNAME` and `MAIL_PASSWORD`, inside `app.app_context()`. `long_task`, declared with `bind=True`, loops over `random.randint(10, 50)` iterations, sleeps one second each and calls `self.update_state(state='PROGRESS', meta={'current', 'total', 'status'})`. Neither task sets `time_limit` or `soft_time_limit`, and the config sets no global default, so a stalled SMTP connection holds a worker process until the socket gives up on its own.

Then it reads the routes that watch those tasks. `/longtask` enqueues with `long_task.apply_async()` and answers 202 with a `Location` header to `/status/<task_id>`; `/status/<task_id>` reads `long_task.AsyncResult(task_id)`, reports `current`, `total` and `status` from `task.info` while the state is `PENDING` or `PROGRESS`, and on `FAILURE` returns `str(task.info)`, the exception. `templates/index.html` polls that route with jQuery and draws a nanobar. `/` enqueues the email with `.delay()` or with `apply_async(countdown=60)`. `requirements.txt` pins `celery==4.4.1`, `kombu==4.6.8`, `redis==3.4.1` and `Flask==1.1.1`, and `run-redis.sh` builds Redis from source.

The change is two limits and one handler. `celery.conf.update` gains `task_soft_time_limit` and `task_time_limit` as defaults for every task, with the hard limit above the soft one, which Celery 4 reads under these lower-case names beside the old upper-case keys. `long_task` gets its own `soft_time_limit` sized for its longest loop, and its loop is wrapped so that `SoftTimeLimitExceeded` is caught, the task writes a final `update_state` with the iteration it reached and a status that says it timed out, and then re-raises so the result is `FAILURE` and `/status` shows the exception text as it does today. `send_async_email` gets a short `soft_time_limit`, since a mail send that has not finished in seconds will not finish, and `autoretry_for=(SoftTimeLimitExceeded,)` with `retry_backoff` and a `max_retries` so one slow SMTP exchange is tried again rather than lost. The agent notes in the pull request that the hard limit is enforced by the prefork pool only, which is what `celery worker` uses by default here.

To prove it, the agent installs Redis with `apt-get` in its sandbox and an interpreter the early-2020 pins run on with `uv python install`, installs `requirements.txt` unchanged, starts `celery -A app.celery worker --concurrency 1`, and from a short script enqueues `long_task` with a soft limit of a couple of seconds through `apply_async(soft_time_limit=…)`. It pastes the worker log lines showing `SoftTimeLimitExceeded`, the task's last `PROGRESS` state and the `/status` response reading `FAILURE`, then the same for the email task with no SMTP reachable, showing the retries and the final failure.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add time limits to the Celery tasks in this Flask app. Read app.py, requirements.txt, run-redis.sh, templates/index.html and the README first. In celery.conf.update set task_soft_time_limit and task_time_limit as defaults with the hard limit above the soft one. Give long_task its own soft_time_limit sized for its longest loop, catch SoftTimeLimitExceeded inside it, write a final update_state with the iteration reached and a timed-out status, and re-raise so the result is FAILURE and /status reports the exception as it does now. Give send_async_email a short soft_time_limit with autoretry_for on SoftTimeLimitExceeded, retry_backoff and a max_retries. Say in the pull request that the hard limit needs the prefork pool. Install Redis in your sandbox and an interpreter the pinned releases run on, install requirements.txt unchanged, start a worker with concurrency 1, enqueue long_task with a soft limit of a few seconds and paste the worker log, the last PROGRESS state and the /status response, then do the same for the email task with no SMTP reachable. Change no route, no template and no pin.",
  "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 `app.py` only. Its description lists the two global limits and the per-task ones in a table with the reason for each number, shows the worker log for the forced timeout with the `SoftTimeLimitExceeded` line, the last `PROGRESS` state the task wrote and the `/status` JSON reading `FAILURE`, and shows the email task retrying with backoff and then failing when no SMTP server answers.

The routes, the template and the pins are unchanged, so the page still polls the same `Location` and draws the same bar; what changes is that a stuck task now ends, with its last known progress recorded, instead of holding a worker process. A reviewer sees the exact exception text the status route will return, because the run shows it rather than describing it.

Before merging, a reviewer sets the limits for their real workloads, since the ones in the pull request are sized to this app's loop and SMTP exchange, decides whether a timed-out email should retry at all or alert instead, and confirms the worker runs with the prefork pool wherever the hard limit matters.

## Run it on every Linear issue labelled ops

Add an automation to the Project with this prompt and a `linear` trigger on `linear.issue.labeled` with the label your on-call work uses in its `labels`. Each labelled issue starts one run with the issue as its context, on its own branch; the trigger adds the issue after the prompt and fills in no placeholder, and a change made by an app integration never fires it, so the agent's own comment on the issue starts nothing.

The automation's `max_runs_per_hour` holds a burst of labels to that many runs an hour: a fire past it is created `queued` and starts when the hour has room, and one past a second hour's worth is skipped as `rate_limited` until the issue is labelled again. From your own server, the same run starts with this request and the issue URL in `resources`, with a `request_id` that is a UUID v5 of the issue id so a retried start returns the run already begun.

## Why run it on Rush?

A time limit is one keyword argument, but whether it fires, what state the task leaves behind and what the status route shows afterwards can only be seen by running a worker and forcing the timeout. The agent does that in its own sandbox with a real broker and a real worker, so the pull request shows the log line and the status response rather than quoting the Celery documentation.

The transcript keeps the worker output, the enqueue script and the status fetch, so a reviewer reads the evidence from one page. Several services with the same task shape can be given limits at once, each run in its own sandbox with its own Redis, and nothing merges until a person reads the diff.

## Frequently asked questions

### What is the difference between the soft and the hard limit?

The soft limit raises `SoftTimeLimitExceeded` inside the task, so the task can catch it, record where it got to and clean up. The hard limit kills the worker process running the task, which is why it sits above the soft one and why it only works on the prefork pool. `long_task` uses the soft one to write its final state; the hard one is the backstop for a task that ignores the exception.

### Why re-raise after catching the soft limit?

So the result is `FAILURE`, which the existing `/status` route already handles by returning the exception text, and so a retry policy can see the failure. Swallowing the exception would mark the task `SUCCESS` with a partial result and the page would draw a full bar for work that never finished.

### Why retry the email task but not long_task?

A mail send is idempotent enough to try again after a slow SMTP exchange, and `autoretry_for` with `retry_backoff` does that without new code in the task. `long_task` is a progress demo whose work is the waiting; retrying it would start the loop over. The pull request keeps that split explicit so a reviewer can change it.

### Does the countdown on apply_async count against the limit?

No. `countdown=60` delays when the worker picks the task up; the time limits start when the task begins executing. A task scheduled a minute ahead and then limited to a few seconds runs for those seconds after the minute, which the agent confirms in the run rather than assuming.

## More Celery jobs

- [Fix the Celery broker_connection_retry Deprecation Warning](https://getrush.ai/run/fix-broker-connection-retry-deprecation-warning/celery/)
- [Fix "Error: No such option '-A'" when a Celery worker starts after moving to Celery 5](https://getrush.ai/run/fix-celery-no-such-option-a/celery/)
- [Upgrade Celery 4 to Celery 5](https://getrush.ai/run/upgrade-celery/celery/)

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

- [Add Team Invitations to a Filament Panel](https://getrush.ai/run/add-team-invitations/filament/)
- [Shard a Jest Suite Across Parallel CI Jobs for an Express API](https://getrush.ai/run/add-test-sharding/jest/)
- [Shard Playwright Tests Across GitHub Actions Runners](https://getrush.ai/run/add-test-sharding/playwright/)

## 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/)
