Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Schedule Celery Beat task with Flask

I want to be able to let the user of my application start/stop periodic crontab style tasks with Celery beat. Right now I run Celery with

venv/bin/celery worker -A celery_worker.celery --loglevel=info

I got Celery Beat running with this simple example:

@celery.task
def add(x, y):
    return x + y

on my config file:

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'app.email.add',
        'schedule': timedelta(seconds=30),
        'args': (16, 16)
    },
}
CELERY_TIMEZONE = 'UTC'

then I run the Celery beat worker with

celery -A celery_worker.celery beat -s ~/Documents/cesco-automation/power/celerybeat-schedule

And it works perfectly! But I need to have more control over the schedule.

Also on my app shell, I am able to do this.

>>>add.apply_async([80,800],countdown=30)

>>> from datetime import datetime, timedelta     
>>> tomorrow = datetime.now() + timedelta(days=1)
>>> add.apply_async(args=[10, 10], eta=tomorrow)

Which is great, but I am developing a home-automation app, so I also need to stop the tasks. How do I do this??

I have also found this link that mention about a django custom scheduler classes. Which is exactly what I need. On the Celery docs it mentions the -S flag, but I dont know how to properly add the class to my Flask app. How can I use it with Flask??

Do I really need Celery Beat? Are there other option other the crontab? Crontab seems not to be sharp enough.

like image 360
CESCO Avatar asked Jun 21 '15 22:06

CESCO


1 Answers

This functionality will be available in Celery ver.4.0. Dynamic task scheduling is only currently supported in the development version : http://docs.celeryproject.org/en/master/userguide/periodic-tasks.html#beat-entries

like image 118
Jakub Czaplicki Avatar answered Nov 15 '22 04:11

Jakub Czaplicki