Skip to main content

A SQLAlchemy-based scheduler for celery-beat

Project description

Python Version Code style: black Build: just code

RDBBeat

rdbbeat is a SQLAlchemy-based scheduler for celery-beat that persists periodic tasks in a relational database.

Table Of Contents

NOTE: We assume prior knowledge of session management in SQLAlchemy and migrations in Alembic. If you are not familiar with these concepts, please read the documentation for SQLAlchemy and Alembic.

Installation

The library is available for download from the official pypi website:

pip install rdbbeat

Or you can build the library for the source code hosted publicly on GitHub.

Celery Configuration

The library makes use of the parent service's database and scope management mechanism. You can configure SQLAlchemy session_scope when you configure celery, for example as:

from celery import Celery

# this comes from parent service
from server.db import session_scope

celery = Celery('tasks')

celery.conf.update(
    {'session_scope': session_scope}
)

Usage

Creating crontab-based periodic task

A crontab schedule has the fields: minute, hour, day_of_week, day_of_month and month_of_year, so if you want the equivalent of a 30 * * * * (execute every 30 minutes) crontab entry, you specify:

from rdbbeat.controller import schedule_task
from rdbbeat.data_models import ScheduledTask

scheduled_task = {
    "name": "task_1",
    "task": "echo",
    "schedule": {
        "minute": "30",
        "hour": "*",
        "day_of_week": "*",
        "day_of_month": "*",
        "month_of_year": "*",
        "timezone": "UTC",
    },
}

with session_scope() as session:
    task = schedule_task(session, ScheduledTask.parse_ob(scheduled_task))

The crontab schedule is linked to a specific timezone using the timezone input parameter.

Run the migrations

rdbbeat includes a migration script that is required to create the database tables in the scheduler schema. Run the following command to run the migrations:

python -m alembic -n scheduler upgrade head

Deployment

The periodic tasks still need 'workers' to execute them. So make sure the default Celery package is installed. (If not installed, please follow the installation instructions here: https://github.com/celery/celery)

Both the worker and beat services need to be running at the same time.

  1. Start a Celery worker service (specify your project name):

    $ celery -A [project-name] worker --loglevel=info
    
  2. As a separate process, start the beat service (specify the scheduler):

    $ celery -A [project-name] beat -l info --scheduler rdbbeat.schedulers:DatabaseScheduler
    

Flask Service Example

Check out the rdbbeat-flask-example repo for a simple Flask service that uses rdbbeat to schedule periodic tasks.

Contribution

Setup

This project is setup to use editorconfig. Most editors work but some require a plugin like VSCode

It's advisable to create a virtual environment for this project to keep packages separate.

NOTE: Using pyenv, you can run pyenv virtualenv 3.10.<latest> celery-sqlalchemy-scheduler

After creating a virtual environment, install the required dependencies.

just setup-dev

Version Control

This repo follows the SemVer 2 version format.

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards compatible manner, and
  • PATCH version when you make backwards compatible bug fixes.

Workflows

The repository has a number of github workflows defined in the the .github/workflows folder.

Lint & Test Code

  • Tests the code for linting issues
  • Tests the requirements file for any changes
  • Runs the provided unit tests to ensure code quality

Release

  • Pushes the library to Pypi

License

MIT licensed. See the bundled LICENSE file for more details.

Acknowledgements

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

rdbbeat-0.2.1.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

rdbbeat-0.2.1-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file rdbbeat-0.2.1.tar.gz.

File metadata

  • Download URL: rdbbeat-0.2.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for rdbbeat-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3768fd0be13c54e6912a81f6a3ab085ac4d0225cb9025194ee34991b03d1163d
MD5 7c923a30ee9055a45a2d5c685de6b4b9
BLAKE2b-256 01ec6cf27d4f0b6aa1a491af95fddf1b5599d13c01870daf1a5e8a56279908e3

See more details on using hashes here.

File details

Details for the file rdbbeat-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: rdbbeat-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for rdbbeat-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 90fb27b88070866ecb1b30a4b0d4c1b3a7bf323affa4620efdd8cd6a4daf6c88
MD5 f2a5d7cf22b0fe3da61bf2a512693e1f
BLAKE2b-256 74d0db6ef0b831243eef49fc0e71b3eceb446cd44b41c93a9161aafefab88e5a

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