Home › Python › Celery background jobs AGENTS.md

Celery background jobs AGENTS.md

AGENTS.md for Celery 5 task queues in Python/Django: idempotent tasks, retries, time limits, and safe enqueueing.

AGENTS.mdPython

How to use

Save this as AGENTS.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

AGENTS.mdDownload  
# AGENTS.md: Celery tasks

## Commands
- Worker: `celery -A config worker -l info` · Beat: `celery -A config beat -l info` · Tests: `pytest -q`

## Writing tasks
- Tasks live in `<app>/tasks.py`, decorated `@shared_task(bind=True)`. Name them explicitly if they're referenced by string.
- Pass ids and primitives, never ORM objects or large payloads. Re-fetch the record inside the task.
- Tasks must be idempotent: running twice gives the same result (check state before acting, use unique constraints, or an idempotency key).
- Retries for transient errors only: `autoretry_for=(httpx.TransportError,)`, `retry_backoff=True`, `retry_jitter=True`, `max_retries=5`. Don't retry on validation errors.
- Set `soft_time_limit`/`time_limit` on anything that calls the network.
- For tasks that must not be lost on worker crash, use `acks_late=True` (and make them idempotent).
- Enqueue after the DB transaction commits: `transaction.on_commit(lambda: send_receipt.delay(order.id))` (or `delay_on_commit` where available), so workers don't read uncommitted rows.
- Long workflows: chains/groups/chords, each step small.
- Periodic tasks defined in code (beat schedule) or django-celery-beat, not hard-coded crons on servers.
- Log with the task id; never log secrets or full payloads with PII.

## Testing
Call the task function directly (`send_receipt.run(order_id)`) or use `task_always_eager` in tests only; mock external calls.

## Don't
Don't purge queues or revoke tasks in shared environments.

More: all Python rules · all AGENTS.md rules

Related rules

Python Copilot path instructions

Path-specific Copilot instructions for Python 3.12+ files: typing, error handling, uv/ruff tooling, and pytest.

GitHub CopilotPython

Modern Python project (uv + ruff) rules

Cursor rules for Python 3.12+ projects managed with uv, ruff, and pyright or mypy: typing, layout, and tooling.

CursorPython

REST API design AGENTS.md

AGENTS.md for designing consistent HTTP/JSON APIs: resource naming, status codes, errors, pagination, and versioning.

AGENTS.mdAPI design

Blazor (.NET 10) AGENTS.md

AGENTS.md for Blazor Web Apps on .NET 10: render modes, components, forms and validation, state, and bUnit tests.

AGENTS.md.NET / ASP.NET Core

Deno 2 AGENTS.md

AGENTS.md for Deno 2 projects: deno.json tasks, JSR and npm imports, permissions, and built-in tooling.

AGENTS.mdDeno

Express 5 API AGENTS.md

AGENTS.md for Express 5 REST APIs in TypeScript with zod validation, error middleware, and supertest.

AGENTS.mdNode.js / TypeScript

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.