Skip to main content

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

Release files for rdbbeat 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rdbbeat 0.2.1
File Size Uploaded
rdbbeat-0.2.1.tar.gz 18.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rdbbeat 0.2.1
File Interpreter ABI Platform
rdbbeat-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size:35.3 kB

Release files / rdbbeat-0.2.1.tar.gz

Download URL rdbbeat-0.2.1.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3768fd0be13c54e6912a81f6a3ab085ac4d0225cb9025194ee34991b03d1163d
BLAKE2b-256 checksum
How to use checksums
01ec6cf27d4f0b6aa1a491af95fddf1b5599d13c01870daf1a5e8a56279908e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / rdbbeat-0.2.1-py3-none-any.whl

Download URL rdbbeat-0.2.1-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90fb27b88070866ecb1b30a4b0d4c1b3a7bf323affa4620efdd8cd6a4daf6c88
BLAKE2b-256 checksum
How to use checksums
74d0db6ef0b831243eef49fc0e71b3eceb446cd44b41c93a9161aafefab88e5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page