99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
|
|
"""
|
||
|
|
Celery Beat schedule configuration for periodic tasks.
|
||
|
|
This defines when scheduled tasks should run.
|
||
|
|
"""
|
||
|
|
from celery.schedules import crontab
|
||
|
|
|
||
|
|
|
||
|
|
# Celery Beat schedule configuration
|
||
|
|
beat_schedule = {
|
||
|
|
# Run every minute (for testing/demo)
|
||
|
|
"print-hello-every-minute": {
|
||
|
|
"task": "tasks.print_hello",
|
||
|
|
"schedule": crontab(minute="*"), # Every minute
|
||
|
|
"args": ("Celery Beat",),
|
||
|
|
"options": {"queue": "default"},
|
||
|
|
},
|
||
|
|
|
||
|
|
# Run daily at 9:00 AM
|
||
|
|
"send-daily-report": {
|
||
|
|
"task": "tasks.send_daily_report",
|
||
|
|
"schedule": crontab(hour=9, minute=0), # 9:00 AM daily
|
||
|
|
"options": {"queue": "reports"},
|
||
|
|
},
|
||
|
|
|
||
|
|
# Run every hour at minute 0
|
||
|
|
"update-product-stats-hourly": {
|
||
|
|
"task": "tasks.update_product_statistics",
|
||
|
|
"schedule": crontab(minute=0), # Every hour at minute 0
|
||
|
|
"args": (None,), # Update all products
|
||
|
|
"options": {"queue": "stats"},
|
||
|
|
},
|
||
|
|
|
||
|
|
# Run every Monday at 8:00 AM
|
||
|
|
"weekly-maintenance": {
|
||
|
|
"task": "tasks.long_running_task",
|
||
|
|
"schedule": crontab(hour=8, minute=0, day_of_week=1), # Monday 8:00 AM
|
||
|
|
"args": (5,), # 5 iterations
|
||
|
|
"options": {"queue": "maintenance"},
|
||
|
|
},
|
||
|
|
|
||
|
|
# Run every 5 minutes (for monitoring/heartbeat)
|
||
|
|
"heartbeat-check": {
|
||
|
|
"task": "tasks.print_hello",
|
||
|
|
"schedule": 300.0, # Every 300 seconds (5 minutes)
|
||
|
|
"args": ("Heartbeat",),
|
||
|
|
"options": {"queue": "monitoring"},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def configure_beat_schedule(celery_app):
|
||
|
|
"""
|
||
|
|
Configure Celery Beat schedule on the Celery app.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
celery_app: Celery application instance
|
||
|
|
"""
|
||
|
|
celery_app.conf.beat_schedule = beat_schedule
|
||
|
|
|
||
|
|
# Configure timezone
|
||
|
|
celery_app.conf.timezone = "UTC"
|
||
|
|
celery_app.conf.enable_utc = True
|
||
|
|
|
||
|
|
# Configure task routes for scheduled tasks
|
||
|
|
celery_app.conf.task_routes = {
|
||
|
|
"tasks.print_hello": {"queue": "default"},
|
||
|
|
"tasks.send_daily_report": {"queue": "reports"},
|
||
|
|
"tasks.update_product_statistics": {"queue": "stats"},
|
||
|
|
"tasks.long_running_task": {"queue": "maintenance"},
|
||
|
|
}
|
||
|
|
|
||
|
|
# Configure queues
|
||
|
|
celery_app.conf.task_queues = {
|
||
|
|
"default": {
|
||
|
|
"exchange": "default",
|
||
|
|
"exchange_type": "direct",
|
||
|
|
"routing_key": "default",
|
||
|
|
},
|
||
|
|
"reports": {
|
||
|
|
"exchange": "reports",
|
||
|
|
"exchange_type": "direct",
|
||
|
|
"routing_key": "reports",
|
||
|
|
},
|
||
|
|
"stats": {
|
||
|
|
"exchange": "stats",
|
||
|
|
"exchange_type": "direct",
|
||
|
|
"routing_key": "stats",
|
||
|
|
},
|
||
|
|
"maintenance": {
|
||
|
|
"exchange": "maintenance",
|
||
|
|
"exchange_type": "direct",
|
||
|
|
"routing_key": "maintenance",
|
||
|
|
},
|
||
|
|
"monitoring": {
|
||
|
|
"exchange": "monitoring",
|
||
|
|
"exchange_type": "direct",
|
||
|
|
"routing_key": "monitoring",
|
||
|
|
},
|
||
|
|
}
|