Skip to main content

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.py has its tasks autodiscovered (celery.autodiscover_tasks imports <package>.tasks for every installed module). No per-module registration hook.
  • Admin UI at /admin/background-tasks — list recent runs, retry failed, inspect tracebacks (gated by the background_tasks.view permission).
  • build_celery(settings) factory in background_tasks.worker.celery_app, plus bind_task_context / get_log_context / install_log_filter exported from the package root (import name background_tasks, distribution name simple_module_background_tasks).
  • enqueue_after_commit(db, task, *args, **kwargs) — publish a task only once the session's transaction commits, never on rollback.
  • heartbeat() — called from a long task body, keeps the stuck sweep from flagging it; @shared_task(stuck_after=seconds) sets a per-task stuck threshold. See the module docs for both.

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

Routing, tuning and per-worker module subsets

A module can ship a light task_config.py (TASK_ROUTES, QUEUES, optional BEAT_SCHEDULE) that is read without importing tasks.py. The task_routes_override setting re-routes tasks at runtime, and the Workers page lists the effective routes. Tuning settings (visibility_timeout_seconds, worker_max_tasks_per_child, task_time_limit, worker_concurrency, ...) and a raw SM_BG_TASKS_CELERY_OVERRIDES (env only, unsafe keys refused) default to Celery's own defaults. SM_BG_TASKS_WORKER_QUEUES=gpu,default make worker consumes only those queues and imports only the modules whose QUEUES overlap (see SM_BG_TASKS_INCLUDE_MODULES / SM_BG_TASKS_EXCLUDE_MODULES; env only, not DB settings). Full details in docs/modules/background_tasks.md.

Durable events

This module is the transport behind bus.publish_durable(event, db): the event is written to an outbox table in the caller's transaction and delivered by a worker to every bus.subscribe_durable(...) handler, at least once, with backoff, after the commit. Writes a handler makes through ctx.db land exactly once. Undelivered and dead rows are on /admin/background-tasks/outbox. Full details in docs/modules/background_tasks.md § Durable events.

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_settings
  • celery[redis]>=5.4, redis>=5

License

MIT — see LICENSE.

Metadata

Release files for simple-module-background-tasks 0.0.46

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-module-background-tasks 0.0.46
File Size Uploaded Provenance Actions
simple_module_background_tasks-0.0.46.tar.gz 236.5 kB With provenance Details for simple_module_background_tasks-0.0.46.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simple-module-background-tasks 0.0.46
File Interpreter ABI Platform Actions
simple_module_background_tasks-0.0.46-py3-none-any.whl Python 3 none any Details for simple_module_background_tasks-0.0.46-py3-none-any.whl

Total release size: 425.5 kB

Release files / simple_module_background_tasks-0.0.46.tar.gz

Download URL simple_module_background_tasks-0.0.46.tar.gz
Size 236.5 kB
Tags Source
SHA-256 checksum
How to use checksums
96550098fd4b89a2af54e8208915e7c24d7e45d9c331d873b6de89f6790b1de7
BLAKE2b-256 checksum
How to use checksums
4754ee55a79b33af64a7b37c273f42f3b931b8d3f28f3771cb392307bb319e1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Release files / simple_module_background_tasks-0.0.46-py3-none-any.whl

Download URL simple_module_background_tasks-0.0.46-py3-none-any.whl
Size 189.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e99217873c125d456dea61b8abbda0b7cfa5bae28aa2c6a53db7d819fda2776
BLAKE2b-256 checksum
How to use checksums
de0e6e7895270fde1ceb3b8e6cd576e0e831d3133419a7c96f962cf9ee587048
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

Security for simple-module-background-tasks 0.0.46

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for simple_module_background_tasks-0.0.46.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 13, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.46 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page