系统初始化时健康计划调整(包括增加延时任务)

This commit is contained in:
2025-11-10 14:11:39 +08:00
parent ca8e5ff867
commit 19d55eb09b
4 changed files with 60 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"git.huangwc.com/pig/pig-farm-controller/internal/domain/task"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
@@ -93,6 +94,47 @@ func (app *Application) initializePeriodicSystemHealthCheckPlan(ctx context.Cont
interval = 1 // 确保间隔至少为1分钟
}
cronExpression := fmt.Sprintf("*/%d * * * *", interval)
// 定义预设的全量采集任务
fullCollectionTask := models.Task{
Name: "全量采集",
Description: "触发一次全量数据采集",
ExecutionOrder: 1,
Type: models.TaskTypeFullCollection,
}
// 定义预设的延时任务
delayParams := task.DelayTaskParams{DelayDuration: 10} // 延时10秒
delayTask := models.Task{
Name: "延时任务",
Description: "系统预设延时任务,用于错峰处理",
ExecutionOrder: 2,
Type: models.TaskTypeWaiting,
}
err := delayTask.SaveParameters(delayParams)
if err != nil {
return fmt.Errorf("序列化延时任务参数失败: %w", err)
}
// 构建新的任务列表
var newTasks []models.Task
newTasks = append(newTasks, fullCollectionTask, delayTask)
// 如果计划已存在,则获取其现有任务并追加到新任务列表后(排除预设任务)
if foundExistingPlan, ok := existingPlanMap[PlanNamePeriodicSystemHealthCheck]; ok {
for _, existingTask := range foundExistingPlan.Tasks {
// 排除已预设的全量采集和延时任务
if existingTask.Type != models.TaskTypeFullCollection && existingTask.Type != models.TaskTypeWaiting {
newTasks = append(newTasks, existingTask)
}
}
}
// 重新设置所有任务的 ExecutionOrder
for i := range newTasks {
newTasks[i].ExecutionOrder = i + 1
}
predefinedPlan := &models.Plan{
Name: PlanNamePeriodicSystemHealthCheck,
Description: fmt.Sprintf("这是一个系统预定义的计划, 每 %d 分钟自动触发一次全量数据采集, 并进行阈值校验告警。", app.Config.Collection.Interval),
@@ -101,14 +143,7 @@ func (app *Application) initializePeriodicSystemHealthCheckPlan(ctx context.Cont
CronExpression: cronExpression,
Status: models.PlanStatusEnabled,
ContentType: models.PlanContentTypeTasks,
Tasks: []models.Task{
{
Name: "全量采集",
Description: "触发一次全量数据采集",
ExecutionOrder: 1,
Type: models.TaskTypeFullCollection,
},
},
Tasks: newTasks,
}
if foundExistingPlan, ok := existingPlanMap[predefinedPlan.Name]; ok {