-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathsetup_periodic_tasks.py
55 lines (44 loc) · 1.59 KB
/
setup_periodic_tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from django.core.management.base import BaseCommand
from django.db import transaction
from django.utils.timezone import get_default_timezone_name
from django_celery_beat.models import CrontabSchedule, IntervalSchedule, PeriodicTask
class Command(BaseCommand):
help = """
Setup celery beat periodic tasks.
Following tasks will be created:
- ....
"""
@transaction.atomic
def handle(self, *args, **kwargs):
print("Deleting all periodic tasks and schedules...\n")
IntervalSchedule.objects.all().delete()
CrontabSchedule.objects.all().delete()
PeriodicTask.objects.all().delete()
"""
Example:
{
'task': periodic_task_name,
'name': 'Periodic task description',
# Everyday at 15:45
# https://crontab.guru/#45_15_*_*_*
'cron': {
'minute': '45',
'hour': '15',
'day_of_week': '*',
'day_of_month': '*',
'month_of_year': '*',
},
'enabled': True
},
"""
periodic_tasks_data = []
timezone = get_default_timezone_name()
for periodic_task in periodic_tasks_data:
print(f'Setting up {periodic_task["task"].name}')
cron = CrontabSchedule.objects.create(timezone=timezone, **periodic_task["cron"])
PeriodicTask.objects.create(
name=periodic_task["name"],
task=periodic_task["task"].name,
crontab=cron,
enabled=periodic_task["enabled"],
)