diff --git a/apps/ops/celery/utils.py b/apps/ops/celery/utils.py index 959e4c907..c35db0f0e 100644 --- a/apps/ops/celery/utils.py +++ b/apps/ops/celery/utils.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- # import json -import os import redis_lock import redis @@ -12,6 +11,7 @@ from django_celery_beat.models import ( PeriodicTask, IntervalSchedule, CrontabSchedule, PeriodicTasks ) +from common.utils.timezone import now from common.utils import get_logger logger = get_logger(__name__) @@ -36,6 +36,8 @@ def create_or_update_celery_periodic_tasks(tasks): for name, detail in tasks.items(): interval = None crontab = None + last_run_at = None + try: IntervalSchedule.objects.all().count() except (ProgrammingError, OperationalError): @@ -50,6 +52,7 @@ def create_or_update_celery_periodic_tasks(tasks): interval = IntervalSchedule.objects.filter(**kwargs).first() if interval is None: interval = IntervalSchedule.objects.create(**kwargs) + last_run_at = now() elif isinstance(detail.get("crontab"), str): try: 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), args=json.dumps(detail.get('args', [])), 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( defaults=defaults, name=name,