Lightweight production-ready background task worker with cron, rate limiting and JSON observability
Project description
PyBgWorker
A lightweight, production-ready background task framework for Python.
PyBgWorker provides a durable SQLite-backed task queue, cron scheduling, rate limiting, retries, and structured observability — all without external infrastructure.
It is designed to be simple, reliable, and easy to deploy.
✨ Features
- Persistent SQLite task queue
- Multi-worker safe execution
- Retry + failure handling
- Crash isolation via subprocess
- Cron scheduler for recurring jobs
- JSON structured logging
- Task duration tracking
- Rate limiting (overload protection)
- Heartbeat monitoring
- CLI inspect / retry / purge / cancel
- Production-safe worker loop
🚀 Installation
pip install pybgworker
🧠 Basic Usage
Define a task
from pybgworker.task import task
@task(name="add")
def add(a, b):
return a + b
Enqueue a task
add.delay(1, 2)
▶ Run worker
python -m pybgworker.cli run --app example
⏰ Cron Scheduler
Run recurring tasks:
from pybgworker.scheduler import cron
from pybgworker.task import task
@task(name="heartbeat_task")
@cron("*/1 * * * *")
def heartbeat():
print("alive")
Cron runs automatically inside the worker.
📊 JSON Logging
All worker events are structured JSON:
{"event":"task_start","task_id":"..."}
{"event":"task_success","duration":0.12}
This enables:
- monitoring
- analytics
- alerting
- observability pipelines
🚦 Rate Limiting
Protect infrastructure from overload:
RATE_LIMIT = 5 # tasks per second
Ensures predictable execution under heavy load.
🔍 CLI Commands
Inspect queue:
python -m pybgworker.cli inspect
Retry failed task:
python -m pybgworker.cli retry <task_id>
Cancel task:
python -m pybgworker.cli cancel <task_id>
Purge queued tasks:
python -m pybgworker.cli purge
🧪 Observability
PyBgWorker logs:
- worker start
- cron events
- task start
- success
- retry
- failure
- timeout
- crash
- heartbeat errors
All machine-readable.
🎯 Design Goals
- zero external dependencies
- SQLite durability
- safe multiprocessing
- operator-friendly CLI
- production observability
- infrastructure protection
📌 Roadmap
Future upgrades may include:
- dashboard web UI
- metrics endpoint
- Redis backend
- workflow pipelines
- cluster coordination
📄 License
MIT 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 pybgworker-0.2.1.tar.gz.
File metadata
- Download URL: pybgworker-0.2.1.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
928357768a85d020f6219c3036c76a33c3459037ed0eafde54204cb3f6965a87
|
|
| MD5 |
e1eecdd9d4400ff043d7eca7fba0775a
|
|
| BLAKE2b-256 |
e57b19a9b55e5eeb09978358566e6e77165efb9e3ca64c177ad64942fd84b030
|
File details
Details for the file pybgworker-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pybgworker-0.2.1-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e88483e6dd48b979006ed9e8249a0130888d076664da9bd94fa4e757b2c80c8
|
|
| MD5 |
eeb63c1dc3ff6150e2117314250db836
|
|
| BLAKE2b-256 |
9d34203a384ff64582bece0487ce65c5d410094421a68604ae8c4e9d9d605229
|