A
A
albertalexandrov2018-12-25 15:51:06
Django
albertalexandrov, 2018-12-25 15:51:06

How to organize periodic jobs in a multi-user application?

Hello everyone!)
Please tell me how to implement the ability to create tasks in a multi-user application.
In various tutorials, examples are often given where there is a tasks.py file in which tasks are written. For example:

from celery.task.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(run_every=(crontab(days='*/1')), name="wake_me_up", ignore_result=True)
def wake_me_up(alarm_melody):
    run_alarm_clock(alarm_melody)

I also need users to be able to create tasks themselves with the frequency they need. For example, so that user A can set the parameter days=1, user B - days=2, and so on. That is, so that tasks are created on the fly.
So far the following comes to mind:
periodicity = 1
periodic_task(run_every=(crontab(days=f'*/{periodicity}')), name="wake_me_up", ignore_result=True)(wake_me_up)(alarm_melody)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2018-12-25
@albertalexandrov

Decorators work at the time of import, it is better to store the user schedule in the database:
https://django-celery-beat.readthedocs.io/en/latest/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question