Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-Background-Tasks: Initialize Task at midnight and repeat every midnight

Tags:

python

django

Good day SO,

I am currently using Django, and the Django-Background-Tasks package. I have a periodic task that I need to run at midnight, and it is to be repeated every midnight.

I am a beginner at using the package, and am confused by the following:

  • How do I set the repeat parameter during initialization?

Here is my code:

from background_task import background
from datetime import datetime, date

today_date = datetime.datetime.today()
today_midnight = today_date.replace(hour=23, minute=59, second=59)

@background(schedule=today_midnight)
def send_reminders():...
send_reminders(repeat=Task.DAILY)

I wanted to set the parameter 'repeat' to task.DAILY, as stated in the documentation. However, I have encountered the following:

NameError: name 'Task' is not defined

I know I have to import something to define Task, but I couldn't find it. Can anyone help me?

like image 854
Benji Tan Avatar asked Sep 17 '25 04:09

Benji Tan


1 Answers

This is defined in the background_task.models module [GitHub]. So you should import this with:

from background_task.models import Task

DAILY itself just specifies the number of seconds, so 24×60×60=86'400:

class Task(models.Model):

    # ...

    HOURLY = 3600
    DAILY = 24 * HOURLY
like image 127
Willem Van Onsem Avatar answered Sep 19 '25 00:09

Willem Van Onsem