celery-db-scheduler
A SQLAlchemy-backed Celery Beat scheduler that stores task schedules in a PostgreSQL database instead of a static config file. Schedules can be created, updated, and deleted at runtime with no Beat restart required.
Features
- Dynamic scheduling — add or remove tasks at runtime via database rows; Beat picks up changes within 60 seconds.
- One-time tasks — schedule a task to fire once at a specific UTC datetime; the library marks it disabled automatically after execution.
- Recurring tasks — standard interval-based schedules (
interval_seconds). - Cron tasks — wall-clock schedules via a 5-field cron expression, evaluated in each row's
timezone. - Time-window support — restrict when a task may run using
start_datetime,end_datetime,daily_start_time,daily_end_time, and a per-scheduletimezone. - Async-first workers — built-in async SQLAlchemy session factory for FastAPI / asyncio workers.
- Sync Beat process — the Beat scheduler uses a standard synchronous SQLAlchemy session, compatible with all Celery Beat deployments.
- Pydantic v2 schemas — ready-made request/response models for exposing schedules via a REST API.
Installation
# Core library + async PostgreSQL driver (asyncpg)
pip install celery-db-scheduler
# Also install a synchronous PostgreSQL driver for the Beat process.
# Pre-built binary (recommended for most environments):
pip install "celery-db-scheduler[sync]"
# Compile from source (recommended for Alpine / musl builds):
pip install "celery-db-scheduler[sync-src]"
Requirements: Python 3.12+, PostgreSQL (tested with 14+).
Quickstart
1. Set environment variables
The Beat process and the async workers use separate database connections:
# Synchronous connection — used by celery beat
export DATABASE_URL="postgresql://user:pass@localhost/mydb"
# Asynchronous connection — used by celery workers
export ASYNC_DATABASE_URL="postgresql+asyncpg://user:pass@localhost/mydb"
2. Create the task_schedules table
Import the library's Base alongside your own models so a single
metadata.create_all() (or Alembic env.py) covers everything:
# myapp/db.py
from sqlalchemy import create_engine
from celery_db_scheduler import Base
# Import your own models here so their tables are also created
import myapp.models # noqa: F401
engine = create_engine("postgresql://user:pass@localhost/mydb")
Base.metadata.create_all(engine) # creates the task_schedules table
With Alembic, add the library's metadata to your env.py:
# alembic/env.py
from celery_db_scheduler import Base as SchedulerBase
from myapp.db import Base as AppBase
target_metadata = [AppBase.metadata, SchedulerBase.metadata]
3. Configure your Celery app
# myapp/celery_app.py
import os
from celery import Celery
celery_app = Celery("myapp", broker="redis://localhost:6379/0")
celery_app.conf.update(
result_backend="redis://localhost:6379/0",
beat_scheduler="celery_db_scheduler.beat.scheduler.DynamicDatabaseScheduler",
# Optional: skip cron runs missed by more than this many seconds (default 3600)
beat_cron_starting_deadline=int(os.getenv("BEAT_CRON_STARTING_DEADLINE", "3600")),
)
4. Write a scheduled task
Use @auto_disable_one_time_task() to have the library automatically mark a
one-time task as disabled after it runs. The decorated function must accept
task_id as its first argument.
# myapp/tasks.py
from celery_db_scheduler import auto_disable_one_time_task, validate_and_get_schedule
from celery_db_scheduler.utils.session import get_session, run_async_safely
from myapp.celery_app import celery_app
async def _send_report_async(task_id: int, recipient: str) -> None:
async for session in get_session():
schedule = await validate_and_get_schedule(session, task_id)
if not schedule:
return
# Your business logic here
print(f"Sending report to {recipient}")
@celery_app.task(name="myapp.tasks.send_report")
@auto_disable_one_time_task()
def send_report(task_id: int, recipient: str) -> None:
run_async_safely(_send_report_async, task_id, recipient)
5. Create schedules in the database
Use the CRUD helpers directly, or expose them through your API:
# Run once at a specific time
from datetime import datetime, timezone
from sqlalchemy.ext.asyncio import AsyncSession
from celery_db_scheduler.crud.task_schedule import create_schedule
from celery_db_scheduler.schemas.task_schedule import TaskScheduleCreate
async def schedule_one_time_report(db: AsyncSession) -> None:
await create_schedule(
db,
TaskScheduleCreate(
task_name="myapp.tasks.send_report",
task_kwargs={"recipient": "admin@example.com"},
start_datetime=datetime(2026, 7, 1, 9, 0, tzinfo=timezone.utc),
interval_seconds=None, # None = one-time
enabled=True,
description="Monthly report — July 2026",
),
)
async def schedule_recurring_cleanup(db: AsyncSession) -> None:
await create_schedule(
db,
TaskScheduleCreate(
task_name="myapp.tasks.cleanup_old_records",
interval_seconds=3600, # every hour
# Only run between 01:00 and 05:00 UTC
daily_start_time="01:00:00",
daily_end_time="05:00:00",
timezone="UTC",
enabled=True,
),
)
6. Start Beat and a worker
# Terminal 1 — worker
celery -A myapp.celery_app worker --loglevel=info
# Terminal 2 — Beat scheduler
celery -A myapp.celery_app beat \
--scheduler celery_db_scheduler.beat.scheduler.DynamicDatabaseScheduler \
--loglevel=info
Beat will poll the database every 60 seconds. Any schedule row you add, update, or delete is reflected in the next sync cycle — no restart needed.
TaskSchedule field reference
| Field | Type | Description |
|---|---|---|
task_name |
str |
Dotted Celery task name, e.g. "myapp.tasks.send_report" |
interval_seconds |
int | None |
Recurrence interval. None = one-time or cron task |
cron_expression |
str | None |
Standard 5-field cron (minute hour day month weekday). When set, fires on wall-clock times in timezone instead of using interval_seconds |
task_args |
list |
Positional arguments passed to the task |
task_kwargs |
dict |
Keyword arguments passed to the task |
enabled |
bool |
Set to False to pause the task |
start_datetime |
datetime |
Earliest time the task may run (required for one-time tasks) |
end_datetime |
datetime |
Task is skipped after this time |
daily_start_time |
time |
Daily window start (evaluated in timezone) |
daily_end_time |
time |
Daily window end (evaluated in timezone) |
timezone |
str |
IANA timezone name, default "UTC" |
last_run_at |
datetime |
Set automatically by the scheduler |
Contributing
Contributions are welcome! This section covers everything you need to get a development environment running.
Prerequisites
- Python 3.12+
- PostgreSQL (for integration tests) or Docker
- hatch (
pip install hatch)
Setup
git clone https://github.com/c05m0ch405/celery-db-scheduler.git
cd celery-db-scheduler
# Install the package and all dev dependencies in an isolated environment
pip install -e ".[sync]"
pip install pytest pytest-cov pytest-asyncio
Running the tests
# All tests with coverage report
pytest tests/ -v --tb=short --cov=celery_db_scheduler --cov-report=term-missing
# A single test module
pytest tests/test_scheduler.py -v
Code style
This project uses Ruff for linting and formatting:
pip install ruff
ruff check . # lint
ruff format . # auto-format
Building a distribution locally
hatch build # produces dist/celery_db_scheduler-*.whl and *.tar.gz
twine check dist/* # verify the package metadata before uploading
Submitting a pull request
- Fork the repository and create a feature branch from
main. - Add or update tests to cover your change — aim for full coverage of new logic.
- Ensure
pytestandruff check .both pass with no errors. - Open a pull request with a clear description of what was changed and why.
For significant changes, please open an issue first to discuss the approach.
License
MIT © c05m0ch405
Metadata
Release files for celery-db-scheduler 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| celery_db_scheduler-1.1.0.tar.gz | 15.9 kB | With provenance | Details for celery_db_scheduler-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| celery_db_scheduler-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for celery_db_scheduler-1.1.0-py3-none-any.whl |
Total release size: 36.3 kB
Release files / celery_db_scheduler-1.1.0.tar.gz
| Download URL | celery_db_scheduler-1.1.0.tar.gz |
|---|---|
| Size | 15.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e35db72e5dcca0a537db413500daec8aaeccbd0f0a9fb089e90ed7fc1fd4bed6
|
|
BLAKE2b-256 checksum How to use checksums |
419173f46705ea0f302d40d9f1a0c1bcd27794344abfea80ef2fdca6960c2901
|
| 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 29, 2026.
Transparency logRelease files / celery_db_scheduler-1.1.0-py3-none-any.whl
| Download URL | celery_db_scheduler-1.1.0-py3-none-any.whl |
|---|---|
| Size | 20.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0209a038ea1de734872d178fadf5630664ad915ab6a945ddd927726360ad0f8c
|
|
BLAKE2b-256 checksum How to use checksums |
ca824f33c702a58bf7cd49bbb6a6823922d9592efc6958580435f69a89299290
|
| 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 29, 2026.
Transparency logSecurity for celery-db-scheduler 1.1.0
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.
Attestation for celery_db_scheduler-1.1.0.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 29, 2026.
Transparency logA 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.