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. The broker / result-backend URLs are module settings (broker_url, default redis://localhost:6379/0; result_backend, default redis://localhost:6379/1) configured from the DB settings store via the admin UI — they are not read from environment variables at runtime. These fields are requires_restart (changing them needs a worker/web restart).
What it provides
- Zero-config task discovery — any installed module that ships a
tasks.pyhas its tasks autodiscovered (celery.autodiscover_tasksimports<package>.tasksfor every installed module). No per-module registration hook. - Admin UI at
/admin/background-tasks— list recent runs, retry failed, inspect tracebacks (gated by thebackground_tasks.viewpermission). build_celery(settings)factory inbackground_tasks.celery_app, plusbind_task_context/get_log_context/install_log_filterexported from the package root (import namebackground_tasks, distribution namesimple_module_background_tasks).
Usage
Declare a task in a module's tasks.py with Celery's @shared_task — it's autodiscovered, no registration hook needed:
# modules/reports/reports/tasks.py
from celery import shared_task
@shared_task(name="reports.generate")
def generate_report(report_id: int) -> None:
...
Declaring background_tasks as a depends_on ensures the Celery app is built before your tasks run:
class ReportsModule(ModuleBase):
meta = ModuleMeta(name="reports", depends_on=["background_tasks"])
Enqueue from an endpoint:
generate_report.delay(report_id=42)
Run a worker locally (the scaffolded host ships a scripts/run_worker.py that builds the app via build_celery):
uv run celery -A scripts.run_worker:celery worker -l info
uv run celery -A scripts.run_worker:celery beat -l 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
from celery import shared_task
@shared_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,simple_module_settingscelery[redis]>=5.4,redis>=5
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_module_background_tasks-0.0.24.tar.gz.
File metadata
- Download URL: simple_module_background_tasks-0.0.24.tar.gz
- Upload date:
- Size: 40.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58b57cd3db09d39981192013ca58a565a9dc6d7fe79acd37498f755f40fd5ffc
|
|
| MD5 |
43ad9178681d7d12def52123e2171ea3
|
|
| BLAKE2b-256 |
27574972acafd7b9f57b09ffa8ba4150886d8eeab0a7ffd61b6346d5b986b8b9
|
Provenance
The following attestation bundles were made for simple_module_background_tasks-0.0.24.tar.gz:
Publisher:
release.yml on antosubash/simple_module_python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_module_background_tasks-0.0.24.tar.gz -
Subject digest:
58b57cd3db09d39981192013ca58a565a9dc6d7fe79acd37498f755f40fd5ffc - Sigstore transparency entry: 2026221173
- Sigstore integration time:
-
Permalink:
antosubash/simple_module_python@35bb8df75943c017d7a7d581859027157488f1d9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/antosubash
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@35bb8df75943c017d7a7d581859027157488f1d9 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file simple_module_background_tasks-0.0.24-py3-none-any.whl.
File metadata
- Download URL: simple_module_background_tasks-0.0.24-py3-none-any.whl
- Upload date:
- Size: 43.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13bc47f6d74549726927572f91fd4e616f785f15bc0037c901bb056b13a73b89
|
|
| MD5 |
fbe4e7af724b642ba56a03d93c5f203d
|
|
| BLAKE2b-256 |
f74d3a7187e57c6f380cbaf5e5501e8fa7ff87013ffdfb2c6c4aa213439ec73d
|
Provenance
The following attestation bundles were made for simple_module_background_tasks-0.0.24-py3-none-any.whl:
Publisher:
release.yml on antosubash/simple_module_python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_module_background_tasks-0.0.24-py3-none-any.whl -
Subject digest:
13bc47f6d74549726927572f91fd4e616f785f15bc0037c901bb056b13a73b89 - Sigstore transparency entry: 2026221281
- Sigstore integration time:
-
Permalink:
antosubash/simple_module_python@35bb8df75943c017d7a7d581859027157488f1d9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/antosubash
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@35bb8df75943c017d7a7d581859027157488f1d9 -
Trigger Event:
workflow_dispatch
-
Statement type: