fix: 修复 interval 周期任务不执行问题

pull/6368/head
xinwen 2021-07-01 10:42:49 +08:00 committed by 老广
parent 8ae6863266
commit 0fd5ab02e9
1 changed files with 6 additions and 2 deletions

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import json import json
import os
import redis_lock import redis_lock
import redis import redis
@ -12,6 +11,7 @@ from django_celery_beat.models import (
PeriodicTask, IntervalSchedule, CrontabSchedule, PeriodicTasks PeriodicTask, IntervalSchedule, CrontabSchedule, PeriodicTasks
) )
from common.utils.timezone import now
from common.utils import get_logger from common.utils import get_logger
logger = get_logger(__name__) logger = get_logger(__name__)
@ -36,6 +36,8 @@ def create_or_update_celery_periodic_tasks(tasks):
for name, detail in tasks.items(): for name, detail in tasks.items():
interval = None interval = None
crontab = None crontab = None
last_run_at = None
try: try:
IntervalSchedule.objects.all().count() IntervalSchedule.objects.all().count()
except (ProgrammingError, OperationalError): except (ProgrammingError, OperationalError):
@ -50,6 +52,7 @@ def create_or_update_celery_periodic_tasks(tasks):
interval = IntervalSchedule.objects.filter(**kwargs).first() interval = IntervalSchedule.objects.filter(**kwargs).first()
if interval is None: if interval is None:
interval = IntervalSchedule.objects.create(**kwargs) interval = IntervalSchedule.objects.create(**kwargs)
last_run_at = now()
elif isinstance(detail.get("crontab"), str): elif isinstance(detail.get("crontab"), str):
try: try:
minute, hour, day, month, week = detail["crontab"].split() minute, hour, day, month, week = detail["crontab"].split()
@ -75,7 +78,8 @@ def create_or_update_celery_periodic_tasks(tasks):
enabled=detail.get('enabled', True), enabled=detail.get('enabled', True),
args=json.dumps(detail.get('args', [])), args=json.dumps(detail.get('args', [])),
kwargs=json.dumps(detail.get('kwargs', {})), kwargs=json.dumps(detail.get('kwargs', {})),
description=detail.get('description') or '' description=detail.get('description') or '',
last_run_at=last_run_at,
) )
task = PeriodicTask.objects.update_or_create( task = PeriodicTask.objects.update_or_create(
defaults=defaults, name=name, defaults=defaults, name=name,