Search code examples
pythondjangocelerydjango-celerycelerybeat

Running periodic task at time stored in database


Currently, I have periodic tasks set up with a Job Scheduler on my Azure instance. These are triggering API (Django) endpoints at fixed times.

I want to make these times dynamic (which will not work with this solution). The plan is to fire these tasks directly from Django. The schedule times will be stored in my database (MySQL) and be retrieved to create the scheduled job. When these values are changed the scheduler should also change accordingly.

After looking at Celery it seems that using periodic tasks crontab schedules could work. Using this, is it possible to set my scheduled time based on values from my database?

It looks like I will also need a Redis instance as well. Since I would only be using Celery for periodic tasks is it still the right approach?


Solution

  • Cron is thought to run tasks that happen periodically, as it has easy configuration options for everyday, every hour, each 15 min...
    Adding cron jobs is not really a good way to configure dynamic tasks to run at a concrete date (or datetime)

    You could use schedule module as explained in this answer.

    There is also another library apscheduler, but check if the last version works well with python3 (if you use it )

    from datetime import date
    from apscheduler.scheduler import Scheduler
    
    # Start the scheduler
    sched = Scheduler()
    sched.start()
    
    # Define the function that is to be executed
    def my_job(text):
        print text
    
    # The job will be executed on November 6th, 2009
    exec_date = date(2009, 11, 6)
    
    # Store the job in a variable in case we want to cancel it
    job = sched.add_date_job(my_job, exec_date, ['hello'])