Celery Beat Crontab Schedules

Schedule recurring Celery tasks with beat crontabs, mixing timezones, staggered offsets, and task-specific routing to dedicated queues.

PythonResilience

Code

pythonsnippet
50 lines
from celery import Celery
from celery.schedules import crontab

app = Celery('ops', broker='redis://localhost:6379/1')
app.conf.update(
    timezone='UTC',
    beat_schedule={
        'nightly-invoice-close': {
            'task': 'tasks.close_invoices',
            'schedule': crontab(hour=2, minute=0),
            'options': {'queue': 'billing'},
        },
        'cache-warm-every-15min': {
            'task': 'tasks.warm_cache',
            'schedule': crontab(minute='*/15'),
            'options': {'queue': 'maintenance'},
        },
        'staggered-region-reports': {
            'task': 'tasks.region_report',
            'schedule': crontab(hour=6, minute=30, day_of_week='mon'),
            'args': ('eu',),
            'options': {'queue': 'reporting'},
        },
        'monthly-compliance-export': {
            'task': 'tasks.compliance_export',
            'schedule': crontab(day=1, hour=3, minute=0),
            'options': {'queue': 'reporting', 'expires': 7200},
        },
    },
)


@app.task(queue='billing')
def close_invoices() -> dict:
    return {'closed': 42}


@app.task(queue='maintenance')
def warm_cache() -> dict:
    return {'warmed': ['rates', 'catalog']}


@app.task(queue='reporting')
def region_report(region: str) -> dict:
    return {'region': region, 'rows': 128}


@app.task(queue='reporting')
def compliance_export() -> dict:
    return {'archive': 'compliance-2026-10.zip'}

Inside the snippet

Count the functions close_invoices, warm_cache, region_report and compliance_export as the entire surface area of Celery Beat Crontab Schedules.

From celery it pulls Celery, crontab — the imports name exactly which library surface the snippet touches.

Returns a newly built value, leaving the inputs as they were.

Points of care

  • celery must be installed first — the snippet imports it directly.

Putting it to use

The main signature reads def close_invoices() -> dict

Treat close_invoices as the API: import it, and ignore the supporting code.

Core lines

return {'archive': 'compliance-2026-10.zip'}
return {'warmed': ['rates', 'catalog']}
return {'region': region, 'rows': 128}

Interactive Tools for This Topic

FAQ

Does `close_invoices` modify its input?
No. The code builds and returns a new value and leaves the arguments untouched, so it is safe to call on shared or frozen data structures.
Are there third-party dependencies?
`celery` — install it with the package manager before running.