Search code examples
djangopython-3.xcrondjango-rest-frameworkapscheduler

django-apscheduler schedule a job to run at a specific time of the day


There isn't much available in the https://github.com/jarekwg/django-apscheduler . I want to set a job which will run exactly at 12:00 AM each day.

How to set that up in django-apscheduler?

What I have tried so far is this:

@register_job(scheduler, "interval", days=1)
def pending_block_activity_notification():
    print("Job started")

How to specify it to run once a day at exactly 12:00 am?

My configuration will run at an interval of 1 day but the interval is counted from when the django server is being started.


Solution

  • Finally I found the solution.

    The syntaxes are same as that of APScheduler.

    @register_job(scheduler, "cron", hour=0)
    def pending_block_activity_notification():
        print("pending_block_activity_notification Job started")
    

    Similarly we can run the job at 12:00 am, 6:00 am, 12:00 pm and 6:00 pm in the following way:-

    @register_job(scheduler, "cron", hour='0,6,12,18')
    def pending_block_activity_notification():
        print("pending_block_activity_notification Job started")
    

    We can find the valid expressions which we can use in the apscheduler docs https://apscheduler.readthedocs.io/en/latest/modules/triggers/cron.html