Skip to main content

Database-backed celery beat schedule for SQLAlchemy with predefined views for sqladmin package.

Project description

About

This package enables you to store crontab periodic celery tasks in an SQLAlchemy compatible database. It was specifically developed to manage the periodic tasks from sqladmin admin interface for SQLAlchemy, but in theory any other solution can be used.

NOTE: the package was developed and tested with a PostgreSQL backend.

Inspired by django-celery-beat

Installation

Install from PyPi:

$ pip install celery-beat-sqlalchemy

Configuration

  1. Set beat_db_scheduler_dsn conf variable in your celery application:
    from celery import Celery
    
    celery_app = Celery(__name__)
    celery_app.conf.beat_db_scheduler_dsn = <DB_DSN>  # must be synchronous
    
  2. In case you use sqladmin, add CeleryTasksAdmin and CeleryTasksScheduleAdmin models to admin panel:
    from celery_beat_sqlalchemy.admin import CeleryTasksAdmin, CeleryTasksScheduleAdmin
    
    <YOUR_ADMIN_APP>.add_view(CeleryTasksAdmin)
    <YOUR_ADMIN_APP>.add_view(CeleryTasksScheduleAdmin)
    
  3. Start celery worker:
    $ celery -A <YOUR_APP> worker -l info
    
  4. Start celery beat with DatabaseScheduler as scheduler:
    $ celery -A <YOUR_APP> beat -S celery_beat_sqlalchemy.scheduler:DatabaseScheduler -l info
    

Usage

The following examples are for sqladmin interface.

View application available tasks

Your application up-to-date list of tasks can be seen in Celery Tasks Models view. New tasks are downloaded on the application start automatically, as well as the removed ones are deleted: List of tasks

Add new periodic task

To add a new scheduled task you select it from the dropdown list: Add from list and fill in all necessary arguments and the desired schedule: Fill params

Implementation details

Models

  • CeleryTasksModel
    This model represents a task in your application. When downloaded during startup, tasks' arguments and docstrings are parsed for more detailed view in an admin interface.
  • CeleryTasksScheduleModel
    This model defines a single periodic task to be run. The same task can be run with different args/kwargs, and they are different entries for celery beat. To uniquely identify those entries the combination of task_name-args-kwargs is used as a unique constraint in the table.
  • CeleryTasksScheduleMetaModel
    Keeps time when celery_tasks_schedule table was last updated and serves as a signal to reload the schedule from the database.

Schedule

To change the schedule of a task, just update the corresponding db entry. The next time the DatabaseScheduler synchronizes, it will acknowledge the new schedule.

NOTE: synchronization is done in the middle of a minute (between 20th and 50th seconds) to avoid overriding the current heap. The main reason of this implementation is to "save" rare tasks from sudden elimination from the schedule because of the reload.

Other synchronization rules:

  • On each reload the scheduler is filled with enabled db entries
  • Reload is done after any update in CeleryTasksScheduleModel or every 5 minutes if there are no update events
  • New scheduled task in code, that is not in db: new db entry is created automatically
  • Scheduled task in code as well as in db: schedule in db is used to run task
  • Tasks that are deleted in code will be removed from db after redeploy

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_beat_sqlalchemy-0.0.4.tar.gz (125.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

celery_beat_sqlalchemy-0.0.4-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file celery_beat_sqlalchemy-0.0.4.tar.gz.

File metadata

  • Download URL: celery_beat_sqlalchemy-0.0.4.tar.gz
  • Upload date:
  • Size: 125.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for celery_beat_sqlalchemy-0.0.4.tar.gz
Algorithm Hash digest
SHA256 60aa27d46c468e517e0313df7b74b7c71cef597dba979bd183b59eec77f10c4e
MD5 2a0f728a7e77fa2e4326731024369287
BLAKE2b-256 7d474d228bf6b359a1fc2b91753ca6955eadbb765fd04560859e2dfb860f3712

See more details on using hashes here.

File details

Details for the file celery_beat_sqlalchemy-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for celery_beat_sqlalchemy-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5bde7c9310b25a4e989b931108d521ea03b9b6864171ce738b20ac588679b0ba
MD5 21b3faf46ef2e6d7691fac05610324a4
BLAKE2b-256 af8f2508fc5ab0e0fac431936c5dba36f96a26f18fe40281d11f6314a7552171

See more details on using hashes here.

Supported by

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