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.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.41

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.41
File Size Uploaded Provenance Actions
simple_module_background_tasks-0.0.41.tar.gz 211.6 kB With provenance Details for simple_module_background_tasks-0.0.41.tar.gz

Built distribution (wheel)

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

Total release size: 389.5 kB

Release files / simple_module_background_tasks-0.0.41.tar.gz

Download URL simple_module_background_tasks-0.0.41.tar.gz
Size 211.6 kB
Tags Source
SHA-256 checksum
How to use checksums
867b542ef15339cf65ec9a7dff5ff51e668b9ff0849162c0b13266ce7440f55a
BLAKE2b-256 checksum
How to use checksums
2fc84602187660a7d744fcb917ccf7e5472376b2073172a9e7d9c35b1f7cab72
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 Oct 25, 2026.

Transparency log

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

Download URL simple_module_background_tasks-0.0.41-py3-none-any.whl
Size 177.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bd33ed510c78df0c20c0a4b7bf041f316bc75d68291efed90758448e5ebebdfb
BLAKE2b-256 checksum
How to use checksums
cfab82c69f68afbaa56be22898e0e809c3ea5914bf59ba59da8d9f4700127bf8
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 Oct 25, 2026.

Transparency log

Security for simple-module-background-tasks 0.0.41

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.41.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 Oct 25, 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.41 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