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.6.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

celery_sqlmodel_beat-0.1.6-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: celery_sqlmodel_beat-0.1.6.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.2.0

File hashes

Hashes for celery_sqlmodel_beat-0.1.6.tar.gz
Algorithm Hash digest
SHA256 a85f65d6402aa601e7d609b280787b9218504f9cf5ee8d87494857f25b340469
MD5 4599c3b77bcb893cd66a100f86983518
BLAKE2b-256 f0ae90caee6750af5eaa449358ce562fa8059ff35973c87cf2f0a4a28da738a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for celery_sqlmodel_beat-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0620f69f4d5872cec76125b4f5a8e911428562d57fdc85a364a9e43d1444f495
MD5 bae18aa941625ec7b8ba61446bbc710d
BLAKE2b-256 bf5d171c733f85cab889ed319d5c0d60b517b3dabf48d5563ffe0e04230b7017

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