0

I have django-project with some tasks, which are saved in db. I need that tasks are executed in certain time. I thiink about cron or celery, but I see only function like repeated actions, but I need to do in time which is saved in my db. How can I do this?

  • Possible duplicate of [How do I get a Cron like scheduler in Python?](https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python) – andreygold May 21 '19 at 11:54

2 Answers2

1

A canonical solution would be to have a cronjob running every X minutes that looks up your db for tasks to be executed and launch a celery task for each (so the task execution is async). You'll have to be careful about race conditions though so the same tasks is not executed twice concurrently (the celery task should check and update the db task status, or you could use redis as a tasklock).

Also, celery already provides an ETA feature to program future tasks executions - which may or not be enough for your needs, depending on the context.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1

I think you are looking for celery beat_schedule.

app.conf.beat_schedule = {
    # Starts to run task on specific time.
    'your-task-name': {
        'task': 'your.tasks.path.name',
        'schedule': crontab(
            minute=[get it from database],
            hour=[get it from database]
        ),
    },
}

Here is the documentation from celery: https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html