Celery beat scheduler using SQLAlchemy, patched for SQLAlchemy 2.x
Project description
Celery SQLAlchemy V2 Scheduler
A Celery Beat scheduler that stores the schedule in a database via SQLAlchemy.
This scheduler allows you to store periodic task schedules in a database, enabling you to add, edit, and remove tasks dynamically without restarting the Celery beat service.
Features
- Dynamic Schedule Management: Add, edit, and disable tasks on the fly by manipulating the database.
- SQLAlchemy Backend: Works with any database supported by SQLAlchemy (e.g., PostgreSQL, MySQL, SQLite).
- Full-Featured Schedules: Natively supports
interval,crontab, andsolarschedules. - Timezone-Aware Crontabs: Define cron jobs that run in specific timezones.
- Declarative Setup: Define an initial schedule directly in your Celery configuration, which will be synchronized to the database on startup.
Installation
Install the package from PyPI:
pip install celery-sqlalchemy-v2-scheduler
Setup
To use this scheduler, you need to set the beat_scheduler and beat_dburi in your Celery application configuration. The scheduler will automatically create the necessary tables in your database when it first starts.
# in your_app/celery.py
from celery import Celery
from celery.schedules import crontab
app = Celery('your_app')
# Configure the scheduler
app.conf.beat_scheduler = 'celery_sqlalchemy_v2_scheduler.schedulers.DatabaseScheduler'
# The database URI for the scheduler.
# This can be any database supported by SQLAlchemy.
app.conf.beat_dburi = 'sqlite:///schedule.db'
# (Optional) Define a static schedule in your config.
# These tasks will be added to the database when the scheduler starts.
# This is useful for defining a default set of tasks.
app.conf.beat_schedule = {
'cleanup-every-morning': {
'task': 'your_app.tasks.backend_cleanup',
'schedule': crontab(hour=4, minute=0),
},
'add-every-30-seconds': {
'task': 'your_app.tasks.add',
'schedule': 30.0,
'args': (16, 16)
},
}
# Load task modules
app.autodiscover_tasks(['your_app.tasks'])
Usage
Running Celery Beat
Start the beat service. If you have configured the scheduler in your Celery app as shown above, you don't need to specify it on the command line.
celery -A your_app beat -l info
The scheduler will connect to the database specified in beat_dburi and create the necessary tables if they don't exist.
Managing Tasks Programmatically
You can add, modify, or delete tasks by directly interacting with the SQLAlchemy models. This is the primary benefit of using a database-backed scheduler.
Here's an example of how to add a new periodic task that runs every 10 seconds.
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from celery.schedules import schedule
from celery_sqlalchemy_v2_scheduler.session import SessionManager
from celery_sqlalchemy_v2_scheduler.models import PeriodicTask, IntervalSchedule
# 1. Setup the database session
db_uri = 'sqlite:///schedule.db'
session_manager = SessionManager()
engine, Session = session_manager.create_session(db_uri)
session = Session()
# 2. Create an interval schedule
# The scheduler will look for an existing schedule with the same properties
# or create a new one if it doesn't exist.
interval = IntervalSchedule.from_schedule(session, schedule(run_every=10.0))
session.flush() # Ensure the interval gets an ID
# 3. Create the periodic task
task = PeriodicTask(
name='My Programmatic Task',
task='your_app.tasks.some_task',
interval=interval,
args='[1, 2]',
kwargs='{"foo": "bar"}',
enabled=True
)
session.add(task)
session.commit()
print(f"Task '{task.name}' with id {task.id} created.")
session.close()
Disabling a Task
To disable a task, simply query it and set its enabled flag to False. The beat service will automatically detect the change and stop scheduling the task.
# ... (session setup from previous example) ...
task_to_disable = session.query(PeriodicTask).filter_by(name='My Programmatic Task').first()
if task_to_disable:
task_to_disable.enabled = False
session.commit()
print(f"Task '{task_to_disable.name}' has been disabled.")
session.close()
Database Models
The scheduler uses the following core models:
PeriodicTask: The main model representing a single periodic task. It holds the task name, arguments, execution options, and a foreign key to one of the schedule types.IntervalSchedule: Stores interval-based schedules (e.g., "run every 30 seconds").CrontabSchedule: Stores cron-style schedules (e.g., "run every day at 5 AM"). This model is timezone-aware.SolarSchedule: Stores schedules based on solar events like sunrise, sunset, dawn, and dusk for a given geographic location.PeriodicTaskChanged: A helper table used internally to efficiently detect when the schedule has been updated, prompting the scheduler to reload its tasks.
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 celery_sqlalchemy_v2_scheduler-0.1.2.tar.gz.
File metadata
- Download URL: celery_sqlalchemy_v2_scheduler-0.1.2.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba8654307476e02409e83b00c3aecd101c784322d424289f3e948875f56070a5
|
|
| MD5 |
876b59a94d14564be39857acfc06bbee
|
|
| BLAKE2b-256 |
4bbe11bfdab8a354e8a5c780f73de29850d4f36c28f038a53d5d2a5704329e99
|
File details
Details for the file celery_sqlalchemy_v2_scheduler-0.1.2-py3-none-any.whl.
File metadata
- Download URL: celery_sqlalchemy_v2_scheduler-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
baf7174161351b7e6c8514d990afec8f998e8a3b5297e2ea18847b5e21c27dd9
|
|
| MD5 |
35106c051475cd796dc12b3f08462a4f
|
|
| BLAKE2b-256 |
e87ea600d58c31b96e472ac5bfea5e3d516718e0435533ab9fca8d32e65836ee
|