Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复周期扫描发送周期任务消息通知 #7364 (#7425) #7426

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading