sidharth
sidharth

Reputation: 101

Django Schedule/run tasks dynamically at particular time periodically from user's order in app (without celery)

I am creating a Django App where the user can schedule some tasks to happen at a particular time.

for example. in google calendar we tell google what we will be doing tomorrow and then at right time it sends us a notification.
similarly here I want it to be flexible, for instance, the user can tell Django the time and function to run. Django will wait for the time and then run the function.

like he said turn off lights at 12 pm then Django will do it.
or for example:-
user says remind me to go to the gym in 30 minutes
And then after 30 minutes, he gets a notification.

Actually the tasks are added dynamically so we can't hardcode them at first.

code:-

skills.py # all the functions defined by user are imported in it
# for example take this task
def turn_off_light(room, id, *args, **kwargs):
    # turned off light using python (your logic)
    print(f"light no {id} is turned off!")

there's a Django model known as Function in which this function is stored and the user can access it easily.

I want users to select this function within the application and also give parameters and time and Django will run it at that time!

In short what I need is that the user from within the application is able to set the time of a function(or task) to run at a particular time(maybe periodically or once or maybe in the situation) and Django runs it on that particular time.

Note: user will also give args and kwargs(I am taking care of that). All I need is Django run the function with those args and kwargs.

(only Django method without celery or something will be appreciated)

Thanks!

Upvotes: 1

Views: 2750

Answers (1)

Gaëtan GR
Gaëtan GR

Reputation: 1398

Without Celery and using Django the best way to do is to create custom django-admin commands with Cron

For example :

  • Create customer command called calendar_routine.py
  • Create a cron schedule to call your function from your server at a given time

Otherwise there is no way to do it in pure Python/Django

Upvotes: 0

Related Questions