From 39b2370f1194b09a4cad57f837e0194e0a0ce8e9 Mon Sep 17 00:00:00 2001 From: lTimej <65690352+lTimej@users.noreply.github.com> Date: Fri, 19 Apr 2024 09:44:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E5=8F=91=E9=80=81=E5=91=A8=E6=9C=9F=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5=20#7364=20(#7425)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复周期扫描发送周期任务消息通知 #7364 * fix: 修复周期扫描发送周期任务消息通知 #7364 --- env.py | 6 ++---- gcloud/core/tasks.py | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/env.py b/env.py index f7b8426663..4a3f5cffb9 100644 --- a/env.py +++ b/env.py @@ -134,10 +134,8 @@ # 发送周期任务消息通知开关 PERIODIC_TASK_REMINDER_SWITCH = int(os.getenv("BKAPP_PERIODIC_TASK_REMINDER_SWITCH", 0)) -# 周期任务自动关闭扫描周期 -PERIODIC_TASK_REMINDER_SCAN_CRON = json.loads( - os.getenv("BKAPP_PERIODIC_TASK_REMINDER_SCAN_CRON", '{"hour": "10", "minute": "20"}') -) +# 周期任务自动关闭扫描周期(每月1号10点20) +PERIODIC_TASK_REMINDER_SCAN_CRON = tuple(os.getenv("BKAPP_PERIODIC_TASK_REMINDER_SCAN_CRON", "20 10 1 * *").split()) # 周期任务消息通知类型 PERIODIC_TASK_REMINDER_NOTIFY_TYPE = json.loads(os.getenv("PERIODIC_TASK_REMINDER_NOTIFY_TYPE", '["email"]')) diff --git a/gcloud/core/tasks.py b/gcloud/core/tasks.py index 68544774b7..81caaa96a4 100644 --- a/gcloud/core/tasks.py +++ b/gcloud/core/tasks.py @@ -20,6 +20,7 @@ import dateutil.relativedelta from celery import task from celery.five import monotonic +from celery.schedules import crontab from celery.task import periodic_task from django.contrib.sessions.models import Session from django.core.cache import cache @@ -128,26 +129,24 @@ def migrate_pipeline_parent_data_task(): logger.info("[migrate_pipeline_parent_data] migrate done!") -@periodic_task(run_every=TzAwareCrontab(minute=0, hour="*/2")) -def cmdb_business_sync_shutdown_period_task(): - task_id = cmdb_business_sync_shutdown_period_task.request.id +@periodic_task(run_every=TzAwareCrontab(minute=1, hour="*/4")) +def cmdb_business_sync_shutdown_periodic_task(): + task_id = cmdb_business_sync_shutdown_periodic_task.request.id with redis_lock(LOCK_ID, task_id) as acquired: if acquired: - logger.info("Start sync business from cmdb...") try: task_ids = [ item["task__celery_task__id"] for item in PeriodicTask.objects.filter(project__is_disable=True).values("task__celery_task__id") ] + logger.info("[shutdown_periodic_task] disabled the deleted periodic task: {}".format(task_ids)) DjangoCeleryBeatPeriodicTask.objects.filter(id__in=task_ids).update(enabled=False) - except exceptions.APIError as e: - logger.error( - "An error occurred when sync cmdb business, message: {msg}, trace: {trace}".format( - msg=str(e), trace=traceback.format_exc() - ) + except Exception as e: + logger.exception( + "[shutdown_periodic_task] closing periodic task from cmdb, message: {msg}".format(msg=str(e)) ) else: - logger.info("Can not get sync_business lock, sync operation abandon") + logger.info("[shutdown_periodic_task] Can not get sync_business lock, sync operation abandon") @task @@ -158,7 +157,7 @@ def send_periodic_task_notify(executor, notify_type, receivers, title, content): logger.exception(f"send periodic task notify error: {e}") -@periodic_task(run_every=TzAwareCrontab(**settings.PERIODIC_TASK_REMINDER_SCAN_CRON)) +@periodic_task(run_every=(crontab(*settings.PERIODIC_TASK_REMINDER_SCAN_CRON))) def scan_periodic_task(is_send_notify: bool = True): if not settings.PERIODIC_TASK_REMINDER_SWITCH: return