Skip to main content

Celery + Redis task queue with admin UI for monitoring and retrying failed/stuck tasks

Project description

simple_module_background_tasks

Celery + Redis background-task module for simple_module apps. Provides a pre-configured Celery instance, a task registration hook, and an admin UI for monitoring + retrying failed/stuck tasks.

Install

pip install simple_module_background_tasks

Requires a Redis broker — set SM_CELERY_BROKER_URL (default redis://localhost:6379/0).

What it provides

  • register_background_tasks() module hook — modules declare tasks here; the registry wires them into the Celery app at boot.
  • Admin UI at /background-tasks/admin — list recent runs, retry failed, inspect tracebacks.
  • Shared Celery app accessible via from background_tasks import celery_app (import name background_tasks, distribution name simple_module_background_tasks).

Usage

Declare a task in a module:

# modules/reports/reports/tasks.py
from background_tasks import celery_app   # type: ignore[import-not-found]


@celery_app.task(name="reports.generate")
def generate_report(report_id: int) -> None:
    ...

Register it:

class ReportsModule(ModuleBase):
    meta = ModuleMeta(name="reports", depends_on=["background_tasks"])

    def register_background_tasks(self):
        from . import tasks  # noqa: F401 — side-effect: registers tasks

Enqueue from an endpoint:

generate_report.delay(report_id=42)

Run a worker locally:

uv run celery -A background_tasks.celery_app worker --loglevel=info

Worker log context

Every worker log line automatically carries the Celery task identifiers that fired it. A LogContextFilter is attached when the Celery app is built (build_celery) and the task_prerun / task_postrun signals bind task_id + task_name into contextvars for the task's duration:

{"level": "INFO", "logger": "reports.tasks", "message": "ingest done",
 "task_id": "9c2a…", "task_name": "reports.generate"}

Use bind_task_context(...) to attach app-level identifiers (the domain job_id that named a Celery task is the canonical example):

from background_tasks import bind_task_context

@celery_app.task
def process_dataset(job_id: int) -> None:
    with bind_task_context(job_id=job_id):
        logger.info("starting ingest")   # now carries job_id too

Bindings nest cleanly and restore on exit. structlog users can mount the same contextvars directly via structlog.contextvars.merge_contextvars.

Depends on

  • simple_module_core, simple_module_db, simple_module_hosting
  • celery[redis]>=5.4, redis>=5

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_module_background_tasks-0.0.17.tar.gz (38.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_module_background_tasks-0.0.17-py3-none-any.whl (42.4 kB view details)

Uploaded Python 3

File details

Details for the file simple_module_background_tasks-0.0.17.tar.gz.

File metadata

File hashes

Hashes for simple_module_background_tasks-0.0.17.tar.gz
Algorithm Hash digest
SHA256 46dbe5d2377d267d5df9af95d5a43ea8e09715992b36b33be06c3b96ef74f5a8
MD5 ce335877da4c9a854f49a457e2208a06
BLAKE2b-256 d4a45c518c39b6c4d06b95e84ae2ecacd54cb884627deae8e8c758989f95bb40

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_module_background_tasks-0.0.17.tar.gz:

Publisher: release.yml on antosubash/simple_module_python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simple_module_background_tasks-0.0.17-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_module_background_tasks-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 2b1ba96dcafabe1d25c4d644d3c5a5f06b21b19bf7b3a26791bdc06c685b6d54
MD5 50d0565e48548870ca2185ee391f8a52
BLAKE2b-256 4bed9b571e52453cf6fa82833883af4744bf0404d1a6b3adf5d8b36abd7ccf1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for simple_module_background_tasks-0.0.17-py3-none-any.whl:

Publisher: release.yml on antosubash/simple_module_python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page