Skip to main content

Celery Periodic Tasks backed by the sqlmodel

Project description

Database backed Celery Beat Scheduler

Description

This is an sqlmodel based implementation of the celery beat scheduler. It persists periodic celery tasks in a SQLAlchemy-compatible database.

Usage

You can install this package using pip. After installation, specify the database connection string in the Celery config, using the name beat_dburi. Modified from sqlmodel-celery-beat to support pydantic v2. and optimize some functions

You can run the beat instance using:

install

pip install celery-sqlmodel-beat

init_models.py

from sqlmodel import SQLModel, create_engine

from app.ext.sqlmodel_celery_beat.models import (
    ClockedSchedule,
    CrontabSchedule,
    IntervalSchedule,
    PeriodicTask,
    PeriodicTasksChanged,
    SolarSchedule,
)

if __name__ == "__main__":
    engine = create_engine('sqlite:///schedule.db')
    def create_db_and_tables() -> None:
        """
        create table
        """
        SQLModel.metadata.create_all(engine)

    create_db_and_tables()

tasks.py

from celery import Celery
from sqlmodel_celery_beat.schedulers import (  # pylint: disable=unused-import
    DatabaseScheduler,
)

beat_scheduler = "sqlmodel_celery_beat.schedulers:DatabaseScheduler"

beat_dburi = 'sqlite:///schedule.db'
# beat_dburi = 'mysql+mysqlconnector://root:root@127.0.0.1/celery-schedule'

config = {
    "broker_url": "redis://:127.0.0.1:6379/1",
    "result_backend": "redis://:127.0.0.1:6379/2",
    "beat_dburi": beat_dburi,
}

celery = Celery(__name__)

celery.config_from_object(config)

celery.autodiscover_tasks()

@celery.task(name='tasks.add')
def add(x, y):
    return x + y

Run Celery

celery -A tasks:celery worker -l info
celery -A tasks:celery beat -S tasks:DatabaseScheduler -l info

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

celery_sqlmodel_beat-0.1.8.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

celery_sqlmodel_beat-0.1.8-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file celery_sqlmodel_beat-0.1.8.tar.gz.

File metadata

  • Download URL: celery_sqlmodel_beat-0.1.8.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.4 Darwin/23.6.0

File hashes

Hashes for celery_sqlmodel_beat-0.1.8.tar.gz
Algorithm Hash digest
SHA256 c3ca0c6eaa65f567157033a9a318e46ef6a6b66b6c6828ba0e56aeee8fbdbacc
MD5 05abb8602ccad79b391016080c43a8cf
BLAKE2b-256 2b26db6c12ef7dac79585337590943fc4031eed203fc3a94f1c5e573e2a68d4c

See more details on using hashes here.

File details

Details for the file celery_sqlmodel_beat-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for celery_sqlmodel_beat-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 7f9092426d81d6ab94ae5d56e9964d2cf49ea96c945de0aafcaef997a951bee7
MD5 a709c8c76055ec830273261b91b85a97
BLAKE2b-256 cfdbe84f777df16fae208fce127b3e5e2a1d602b39180ef5848795a130f53774

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page