Creating & Managing Tasks
RedBeat offers multiple ways to define and manage your scheduled tasks, from static configuration to fully dynamic runtime management.
Static Tasks (via beat_schedule)
You can define tasks directly in your Celery configuration file using the standard beat_schedule setting. RedBeat will automatically load these tasks into Redis on startup.
# In your celery config
from datetime import timedelta
from celery.schedules import crontab
app.conf.beat_schedule = {
'add-every-30-seconds': {
'task': 'tasks.add',
'schedule': timedelta(seconds=30),
'args': (16, 16)
},
'multiply-at-midnight': {
'task': 'tasks.multiply',
'schedule': crontab(hour=0, minute=0),
'args': (4, 4)
}
}
These tasks are considered "static" because changing them requires a code change and a restart of the Beat process. RedBeat keeps a record of these static tasks to clean them up if they are removed from the configuration in a future deployment.
Dynamic Tasks (via Python)
The most powerful feature of RedBeat is the ability to manage tasks programmatically. The RedBeatSchedulerEntry class provides an easy-to-use API for this.
from redbeat import RedBeatSchedulerEntry
from celery.schedules import schedule, crontab
from your_celery_app import app # Import your Celery app instance
# Create an interval schedule to run every 60 seconds
interval = schedule(run_every=60)
entry = RedBeatSchedulerEntry('my-interval-task', 'tasks.add', interval, args=[10, 20], app=app)
entry.save()
# Create a crontab schedule
cron = crontab(minute='*/15') # Every 15 minutes
entry2 = RedBeatSchedulerEntry('my-cron-task', 'tasks.multiply', cron, args=[5, 5], app=app)
entry2.save()
# To delete a task
entry_to_delete = RedBeatSchedulerEntry.from_key('redbeat:my-interval-task', app=app)
entry_to_delete.delete()
Dynamic Tasks (via Direct Redis Insertion)
For management from other services or languages, you can insert task definitions directly into Redis.
A task consists of a Redis Hash with a key following the pattern <redbeat_key_prefix>:<task_name>.
This hash must contain a field named definition, which is a JSON string describing the task.
Interval Task Example
To create a task named interval-example that runs tasks.every_5_seconds every 5 seconds:
{
"name": "interval-example",
"task": "tasks.every_5_seconds",
"schedule": {
"__type__": "interval",
"every": 5,
"relative": false
},
"args": ["param1", "param2"],
"kwargs": {"max_targets": 100},
"enabled": true
}
Crontab Task Example
To create a task named crontab-example that runs tasks.daily at 5 minutes past the hour:
{
"name": "crontab-example",
"task": "tasks.daily",
"schedule": {
"__type__": "crontab",
"minute": "5",
"hour": "*",
"day_of_week": "*",
"day_of_month": "*",
"month_of_year": "*"
},
"enabled": true
}
Recurrence Rule (rrule) Example
RedBeat also supports rrule schedules for more complex recurring tasks.
{
"name": "rrule-example",
"task": "tasks.report",
"schedule": {
"__type__": "rrule",
"freq": "WEEKLY",
"dtstart": 1672531200,
"byweekday": ["MO", "FR"]
},
"enabled": true
}
Once the definition hash is created, you must add the task key to the main schedule, which is a Redis Sorted Set.
# Add the task to the schedule to be run immediately (score 0)
zadd redbeat:schedule 0 redbeat:interval-example