Auto-populate django-celery-beat PeriodicTask objects with a simple @periodic_task decorator.
Project description
django-beat-periodic
Auto-populate django-celery-beat PeriodicTask objects with a simple decorator — no manual admin setup required.
Installation
pip install django-beat-periodic
Why Use This?
1. Periodic Tasks as Code — No Admin Required
Define schedules directly in your codebase. No need to manually create or update periodic tasks through the Django admin — everything is synced automatically on startup.
2. Dynamic, Environment-Aware Scheduling
Use environment variables or Django settings to change task intervals per environment — without touching the database:
import os
from django_beat_periodic import periodic_task
CLEANUP_INTERVAL = int(os.getenv("CLEANUP_INTERVAL", "3600")) # 1h default
@periodic_task(interval=CLEANUP_INTERVAL)
def cleanup_old_records():
...
Run every 10 seconds in dev, every hour in production — same code, different env vars.
3. Version-Controlled Schedules
Since schedules live in code, every change is tracked in Git — you get full history, code review, and easy rollbacks. No more wondering who changed a task's interval in the admin.
4. Consistent Across Team & Deployments
New team members or fresh deployments get the correct periodic tasks automatically — no manual setup, no "did you remember to add the periodic task?" checklist.
5. Single Source of Truth
The decorator is the authoritative definition. If someone changes a task's settings in the admin, the next deployment will reset it to match the code — preventing configuration drift.
Quick Start
1. Add to INSTALLED_APPS
INSTALLED_APPS = [
# ...
"django_celery_beat",
"django_beat_periodic", # must come AFTER django_celery_beat
# ...
]
2. Decorate Your Tasks
# myapp/tasks.py
from django_beat_periodic import periodic_task
@periodic_task(interval=60)
def heartbeat():
"""Runs every 60 seconds."""
print("alive!")
@periodic_task(crontab="*/5 * * * *")
def generate_report():
"""Runs every 5 minutes (cron-style)."""
build_report()
@periodic_task(interval=300, enabled=False)
def expensive_cleanup():
"""Registered but disabled by default."""
cleanup_old_records()
3. Done!
On Django startup, django_beat_periodic will automatically create or update the
corresponding PeriodicTask, IntervalSchedule, and CrontabSchedule rows in your
database. The Celery Beat scheduler picks them up immediately.
Decorator Options
| Parameter | Type | Description |
|---|---|---|
interval |
int or timedelta |
Run every N seconds (or a timedelta) |
crontab |
str or dict |
Cron expression ("* * * * *") or dict of fields |
enabled |
bool (default True) |
Whether the task is enabled |
name |
str |
Custom task name (defaults to module path) |
queue |
str |
Celery queue to route to |
priority |
int |
Task priority |
args |
list |
Positional arguments for the task |
kwargs |
dict |
Keyword arguments for the task |
How It Works
@periodic_taskregisters metadata (schedule, enabled, etc.) in an internal registry and wraps the function with Celery's@app.task.- When Django starts,
DjangoBeatPeriodicConfig.ready()callssync_periodic_tasks(). sync_periodic_tasks()iterates the registry and creates or updatesdjango_celery_beatdatabase objects, only writing when something actually changed.
Requirements
- Python ≥ 3.9
- Django ≥ 3.2
- Celery ≥ 5.0
- django-celery-beat ≥ 2.0
License
MIT
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
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 django_beat_periodic-0.1.0.tar.gz.
File metadata
- Download URL: django_beat_periodic-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cc7eaf2fe9512b7ebd344928c6619cff845a40f54762d683b1e97d6c667be66
|
|
| MD5 |
f62d7295cdd0b14c1530e006408e9250
|
|
| BLAKE2b-256 |
2d17c191a837ae1b6f1a21a5f42efda1e9293396fad76764ca79407edd7d996b
|
File details
Details for the file django_beat_periodic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_beat_periodic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30720aee10229af1b5d69e86c139a042b9fc99402ec394b59e55391b6df7f42c
|
|
| MD5 |
f4b81258c6dc712add5a73c36262c43b
|
|
| BLAKE2b-256 |
d9bce4550a5e477a2cc849fe28ddf43236a393369e9ceef400da3a17389fec59
|