-
Notifications
You must be signed in to change notification settings - Fork 382
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
周期任务运营优化 #7364
Labels
Comments
@normal-wls 发送频率和延期时长,需要做成可配置 |
参考内置通知执行人逻辑,纯后台实现,不使用前台的通知配置 |
开发方案:1.级联关闭开启的周期任务在原有定期扫描cc项目上,将已经的归档的项目对应的周期任务关闭 def sync_projects_from_cmdb(username, use_cache=True):
......
Project.objects.update_business_project_status(
archived_cc_ids=archived_biz_cc_ids | deleted_biz_cc_ids, active_cc_ids=active_biz_cc_ids
)
try:
archived_cc_ids = archived_biz_cc_ids | deleted_biz_cc_ids
period_tasks = PeriodicTask.objects.filter(project__id__in=archived_cc_ids).all()
# 把cc归档的业务下的周期任务关闭
for task in period_tasks:
task.set_enabled(False)
except Exception as e:
logger.warning("[shutdown period_task] shutdown peroid_tash failed due to: {}".format(e)) 2.定期通知@periodic_task(run_every=TzAwareCrontab(**settings.EXPIRED_SESSION_PERIOD_TASK_SCAN))
def scan_period_task():
tasks = PeriodicTask.objects.filter(task__celery_task__enabled=True).all()
title = "周期任务关闭通知"
for task in tasks:
# 根据更新人/创建人 、更新时间/创建时间聚合
notify_time = task.edit_time if task.editor else task.create_time
notifier = task.editor if task.editor else task.creator
curr_time = datetime.datetime.now()
last_month_time = curr_time + dateutil.relativedelta.relativedelta(months=-int(settings.PERIOD_TASK_TIMES))
record = NoticeHistoryRecord.objects.filter(task=task,notifier=notifier).first()
# 把通知确认链接放在邮件内容里
content = settings.BK_SOPS_HOST + f"/periodictask/api/shutdown/confirm/{task.project.id}/{task.id}/"
receiver = task.editor if task.editor else task.creator
try:
if not record:
# 超过一个月的周期任务给相关人发送通知
if notify_time.timestamp() < last_month_time.timestamp():
send_message("admin",["email"],receiver,title,content)
else:
record_time = record.notify_time
# 距上次确认时间超过一个月
if record_time.timestamp() < last_month_time.timestamp():
send_message("admin",["email"],receiver,title,content)
except Exception as e:
logger.exception(f"send period task notify error: {e}")
# 确认接口
@require_GET
@iam_intercept(ShutdownConfirmInterceptor())
def periodtask_shutdown_confirm(request, project_id, task_id):
task = PeriodicTask.objects.get(id=task_id)
defaults = {
"notifier": task.editor if task.editor else task.creator,
"notify_time": datetime.datetime.now()
}
try:
# 更新通知人和通知时间
NoticeHistoryRecord.objects.update_or_create(task=task,defaults=defaults)
except Exception as e:
return JsonResponse(
{"result": False, "message": str(e), "data": None, "code": err_code.REQUEST_PARAM_INVALID.code}
)
return JsonResponse({"result": True, "message": "success", "data": None, "code": err_code.SUCCESS.code})
# 通知历史记录表
class NoticeHistoryRecord(models.Model):
task = models.ForeignKey(PeriodicTask, verbose_name=_("周期任务"), on_delete=models.CASCADE)
notify_time = models.DateTimeField(verbose_name=_("通知周期"), auto_now_add=True)
notifier = models.CharField(max_length=64, verbose_name=_("通知人")) |
period_tasks = PeriodicTask.objects.filter(project__id__in=archived_cc_ids).all()
# 把cc归档的业务下的周期任务关闭
for task in period_tasks:
task.set_enabled(False)
|
@lTimej 通知模板参考 |
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Mar 26, 2024
Merged
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 9, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 15, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 15, 2024
normal-wls
pushed a commit
that referenced
this issue
Apr 15, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 17, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 17, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 17, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 17, 2024
normal-wls
added a commit
that referenced
this issue
Apr 17, 2024
…on_master perf: 周期任务消息通知模版信息优化 #7364
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 18, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 19, 2024
normal-wls
pushed a commit
that referenced
this issue
Apr 19, 2024
normal-wls
pushed a commit
that referenced
this issue
Apr 19, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 19, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
Apr 22, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
May 7, 2024
normal-wls
pushed a commit
that referenced
this issue
May 9, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
May 9, 2024
lTimej
added a commit
to lTimej/bk-sops
that referenced
this issue
May 9, 2024
normal-wls
pushed a commit
that referenced
this issue
May 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
背景
目前的周期任务增长迅速,通过对环境数据的获取和分析,以及与用户沟通,发现有部分已经废弃的周期任务其实已经不需要了,但是用户不会去主动关闭,这造成了执行资源的浪费。
方案
为了解决这个问题,本方案主要设计两个场景:
实现
The text was updated successfully, but these errors were encountered: