Skip to content

Commit

Permalink
fix: 修复周期扫描发送周期任务消息通知 #7364 (#7425)
Browse files Browse the repository at this point in the history
* fix: 修复周期扫描发送周期任务消息通知 #7364

* fix: 修复周期扫描发送周期任务消息通知 #7364
  • Loading branch information
lTimej authored and normal-wls committed Apr 19, 2024
1 parent 053ed3c commit 39b2370
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
6 changes: 2 additions & 4 deletions env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]'))
21 changes: 10 additions & 11 deletions gcloud/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 39b2370

Please sign in to comment.