Quick Start Guide
This guide will walk you through the minimal steps to get a Celery Beat instance running with RedBeat.
Step 1: Install RedBeat
If you haven't already, install the package via pip:
pip install celery-redbeat
Step 2: Configure Celery
Create a Celery configuration file (e.g., celeryconfig.py). The key is to set CELERYBEAT_SCHEDULER to redbeat.RedBeatScheduler.
Here is a minimal configuration:
# celeryconfig.py
# Use Redis as the message broker
BROKER_URL = 'redis://localhost:6379/0'
# Set RedBeat as the scheduler
CELERYBEAT_SCHEDULER = 'redbeat.RedBeatScheduler'
# Optional: A fast loop interval is recommended for RedBeat
CELERYBEAT_MAX_LOOP_INTERVAL = 5 # seconds
# Optional: RedBeat settings (see Configuration for more)
# REDBEAT_REDIS_URL = 'redis://localhost:6379/1' # Defaults to BROKER_URL
Step 3: Define a Celery App and a Task
Create a file for your Celery application (e.g., tasks.py):
# tasks.py
from celery import Celery
from datetime import timedelta
app = Celery('my_app')
app.config_from_object('celeryconfig')
@app.task
def add(x, y):
print(f"Adding {x} + {y}")
return x + y
# Optional: Define a static schedule in your config
app.conf.beat_schedule = {
'add-every-30-seconds': {
'task': 'tasks.add',
'schedule': timedelta(seconds=30),
'args': (16, 16)
},
}
Step 4: Run Celery Beat
Start the Celery Beat service, pointing it to your Celery app and specifying the RedBeat scheduler.
celery -A tasks.app beat -S redbeat.RedBeatScheduler --loglevel=INFO
You should see logs indicating that RedBeat has acquired a lock and is sending your scheduled task.
Step 5: Run a Celery Worker
In a separate terminal, start a Celery worker to execute the tasks sent by the beat scheduler:
celery -A tasks.app worker --loglevel=INFO
The worker will pick up the add task every 30 seconds.
Running Beat Embedded in a Worker
For development, you can run Beat as part of a worker process using the --beat or -B flag. You must still specify the scheduler.
celery -A tasks.app worker --beat --scheduler redbeat.RedBeatScheduler --loglevel=INFO