Skip to main content

A Celery Beat scheduler that stores the schedule in MongoDB.

Project description

Celery-MongoBeat

A modern, drop-in replacement for celerybeat-mongo. This project provides a Celery Beat scheduler that stores and retrieves task schedules from a MongoDB collection, allowing for dynamic management of periodic tasks without restarting the Celery Beat service.

Features

  • Dynamic Task Scheduling: Add, modify, and remove periodic tasks on the fly.
  • MongoDB Backend: Leverages MongoDB for a robust and scalable schedule store.
  • Backwards Compatible: Designed as a drop-in replacement for the deprecated celerybeat-mongo. It supports the legacy mongodb_backend_settings configuration.
  • Modern Tooling: Built with a modern Python packaging structure (pyproject.toml).

Installation

Install the package from PyPI:

pip install celery-mongobeat

Configuration

To use this scheduler, set the beat_scheduler option in your Celery configuration.

Recommended Configuration

# celeryconfig.py

mongodb_scheduler_url = "mongodb://localhost:27017/"
mongodb_scheduler_db = "celery"
mongodb_scheduler_collection = "schedules"

beat_scheduler = "celery_mongobeat.schedulers:MongoScheduler"

Legacy (Backwards-Compatible) Configuration

If you are migrating from celerybeat-mongo, you can use your existing configuration.

# celeryconfig.py

mongodb_backend_settings = {
    "host": "mongodb://localhost:27017/",
    "database": "celery",
    "collection": "schedules"
}

beat_scheduler = "celery_mongobeat.schedulers:MongoScheduler"

Usage

Once configured, start Celery Beat as you normally would:

celery -A your_app beat -l info

You can now manage your schedules by adding, updating, or removing documents in the configured MongoDB collection.

Programmatic Usage Example

While you can manage schedules by inserting raw documents into MongoDB, it's often cleaner to use a helper class within your application. Here is an example of a ScheduleManager class that you could use in your project to programmatically create, update, and find tasks.

This example is framework-agnostic and uses pymongo directly.

from pymongo.collection import Collection

class ScheduleManager:
    """A helper class to manage schedule entries in MongoDB."""

    def __init__(self, collection: Collection):
        self.collection = collection

    def create_interval_task(self, name: str, task: str, every: int, period: str = 'seconds', args=None, kwargs=None):
        """Creates a task that runs on a fixed interval."""
        args = args or []
        kwargs = kwargs or {}
        schedule_doc = {
            'name': name,
            'task': task,
            'enabled': True,
            'interval': {'every': every, 'period': period},
            'args': args,
            'kwargs': kwargs,
        }
        self.collection.update_one(
            {'name': name},
            {'$set': schedule_doc},
            upsert=True
        )
        print(f"Upserted interval task: '{name}'")

    def create_crontab_task(self, name: str, task: str, minute='*', hour='*', day_of_week='*', args=None, kwargs=None):
        """Creates a task that runs on a crontab schedule."""
        args = args or []
        kwargs = kwargs or {}
        schedule_doc = {
            'name': name,
            'task': task,
            'enabled': True,
            'crontab': {'minute': minute, 'hour': hour, 'day_of_week': day_of_week},
            'args': args,
            'kwargs': kwargs,
        }
        self.collection.update_one(
            {'name': name},
            {'$set': schedule_doc},
            upsert=True
        )
        print(f"Upserted crontab task: '{name}'")

    def disable_task(self, name: str):
        """Disables a task by its unique name."""
        self.collection.update_one(
            {'name': name},
            {'$set': {'enabled': False}}
        )
        print(f"Disabled task: '{name}'")

You could then use this manager in your application like so:

# In your application's setup code
from pymongo import MongoClient

client = MongoClient("mongodb://localhost:27017/")
db = client["celery"]
schedules_collection = db["schedules"] # Must match your celery-mongobeat config

manager = ScheduleManager(schedules_collection)

# Example: Create a task to run every 30 seconds
manager.create_interval_task(
    name='my-periodic-task',
    task='your_project.tasks.some_task',
    every=30,
    period='seconds',
    args=[1, 2, 3]
)

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_mongobeat-0.1.1.tar.gz (18.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_mongobeat-0.1.1-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file celery_mongobeat-0.1.1.tar.gz.

File metadata

  • Download URL: celery_mongobeat-0.1.1.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for celery_mongobeat-0.1.1.tar.gz
Algorithm Hash digest
SHA256 469747c173c0bbead7b6809bc9555d9d3229aec0a81f6c05464e4e222cb61561
MD5 a5483b7307049ff62af28ccc6dcb4083
BLAKE2b-256 5edaf26c6e085f6dcfd88d3d04fdb5bb7995d1a2f0d432ad42420cb832bbc1ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for celery_mongobeat-0.1.1.tar.gz:

Publisher: publish.yml on sockmysox/celery-mongobeat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file celery_mongobeat-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for celery_mongobeat-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0ba6f99fe509f04f6fa612c67f1e5b9212f99f1be3a4673ebe2dcff3be9b035
MD5 756fafebfd1be9e7c3151f429919bcf4
BLAKE2b-256 0628db30d839595ac60bb6f340a44a35e2d6314df2d90467bc222bb29ca63369

See more details on using hashes here.

Provenance

The following attestation bundles were made for celery_mongobeat-0.1.1-py3-none-any.whl:

Publisher: publish.yml on sockmysox/celery-mongobeat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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