Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 531
Matt Avatar asked May 11 '18 16:05

Matt


People also ask

Where are schedule tasks stored?

There are two different folders labeled "tasks". The first folder is relative to the scheduled tasks that would appear in the task scheduler, these are in c:\windows\tasks. The second tasks folder is located in c:\windows\system32\tasks.

What is scheduled job in database?

A schedule specifies when and how many times a job is executed. Jobs can be scheduled for processing at a later time or immediately. For jobs to be executed at a later time, the user can specify a date and time when the job should start.

Which account is used to run a scheduled task?

The task should be scheduled to run under a service user account. Do not use a personal account, other than possibly temporarily for testing, since the schedule will stop working as soon as the password is changed. Usually, a local account on the server can be used.

How do you run a task periodically in Python?

You can run a periodic task in the background using a daemon thread with a while loop and a sleep. A daemon thread is a background thread.


1 Answers

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'])
like image 166
Evhz Avatar answered Sep 26 '22 23:09

Evhz