增加通知状态刷新任务
This commit is contained in:
@@ -30,6 +30,11 @@ type AlarmService interface {
|
||||
// CancelAlarmSnooze 取消对一个告警的忽略状态。
|
||||
// 如果告警不存在,或本就未被忽略,不执行任何操作并返回 nil。
|
||||
CancelAlarmSnooze(ctx context.Context, alarmID uint32) error
|
||||
|
||||
// ClearExpiredIgnoredAlarms 清除所有已过期的告警忽略状态。
|
||||
// 将 is_ignored 设置为 false,ignored_until 设置为 NULL。
|
||||
// 返回受影响的行数和错误。
|
||||
ClearExpiredIgnoredAlarms(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
// alarmService 是 AlarmService 接口的具体实现。
|
||||
@@ -173,3 +178,22 @@ func (s *alarmService) CancelAlarmSnooze(ctx context.Context, alarmID uint32) er
|
||||
logger.Infof("告警 %d 的忽略状态已被成功取消。", alarmID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClearExpiredIgnoredAlarms 清除所有已过期的告警忽略状态。
|
||||
func (s *alarmService) ClearExpiredIgnoredAlarms(ctx context.Context) (int64, error) {
|
||||
serviceCtx, logger := logs.Trace(ctx, s.ctx, "ClearExpiredIgnoredAlarms")
|
||||
|
||||
rowsAffected, err := s.alarmRepo.ClearExpiredIgnoredAlarms(serviceCtx)
|
||||
if err != nil {
|
||||
logger.Errorf("清除过期忽略告警状态失败: %v", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if rowsAffected > 0 {
|
||||
logger.Infof("成功清除了 %d 条过期忽略告警的状态。", rowsAffected)
|
||||
} else {
|
||||
logger.Debugf("没有发现需要清除的过期忽略告警。")
|
||||
}
|
||||
|
||||
return rowsAffected, nil
|
||||
}
|
||||
|
||||
48
internal/domain/task/refresh_notification_task.go
Normal file
48
internal/domain/task/refresh_notification_task.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/alarm"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/plan"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
|
||||
type RefreshNotificationTask struct {
|
||||
ctx context.Context
|
||||
taskLog *models.TaskExecutionLog
|
||||
|
||||
alarmService alarm.AlarmService
|
||||
}
|
||||
|
||||
func NewRefreshNotificationTask(ctx context.Context, taskLog *models.TaskExecutionLog, alarmService alarm.AlarmService) plan.Task {
|
||||
return &RefreshNotificationTask{
|
||||
ctx: ctx,
|
||||
taskLog: taskLog,
|
||||
alarmService: alarmService,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RefreshNotificationTask) Execute(ctx context.Context) error {
|
||||
taskCtx, logger := logs.Trace(ctx, r.ctx, "Execute")
|
||||
logger.Infof("开始执行刷新通知任务, 任务ID: %d", r.taskLog.TaskID)
|
||||
|
||||
_, err := r.alarmService.ClearExpiredIgnoredAlarms(taskCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Infof("刷新通知任务执行完成, 任务ID: %d", r.taskLog.TaskID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RefreshNotificationTask) OnFailure(ctx context.Context, executeErr error) {
|
||||
logger := logs.TraceLogger(ctx, r.ctx, "OnFailure")
|
||||
logger.Errorf("刷新通知任务执行失败, 任务ID: %d, 错误: %v", r.taskLog.TaskID, executeErr)
|
||||
}
|
||||
|
||||
func (r *RefreshNotificationTask) ResolveDeviceIDs(ctx context.Context) ([]uint32, error) {
|
||||
// 刷新通知任务不与具体设备绑定
|
||||
return []uint32{}, nil
|
||||
}
|
||||
@@ -68,6 +68,8 @@ func (t *taskFactory) Production(ctx context.Context, claimedLog *models.TaskExe
|
||||
return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, "DeviceThresholdCheckTask"), claimedLog, t.sensorDataRepo, t.alarmService)
|
||||
case models.TaskTypeAreaCollectorThresholdCheck:
|
||||
return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, "AreaCollectorThresholdCheckTask"), claimedLog, t.sensorDataRepo, t.deviceRepo, t.alarmService)
|
||||
case models.TaskTypeNotificationRefresh:
|
||||
return NewRefreshNotificationTask(logs.AddCompName(baseCtx, "NotificationRefreshTask"), claimedLog, t.alarmService)
|
||||
default:
|
||||
// TODO 这里直接panic合适吗? 不过这个场景确实不该出现任何异常的任务类型
|
||||
logger.Panicf("不支持的任务类型: %s", claimedLog.Task.Type)
|
||||
@@ -101,6 +103,8 @@ func (t *taskFactory) CreateTaskFromModel(ctx context.Context, taskModel *models
|
||||
return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, "DeviceThresholdCheckTask"), tempLog, t.sensorDataRepo, t.alarmService), nil
|
||||
case models.TaskTypeAreaCollectorThresholdCheck:
|
||||
return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, "AreaCollectorThresholdCheckTask"), tempLog, t.sensorDataRepo, t.deviceRepo, t.alarmService), nil
|
||||
case models.TaskTypeNotificationRefresh:
|
||||
return NewRefreshNotificationTask(logs.AddCompName(baseCtx, "NotificationRefreshTask"), tempLog, t.alarmService), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("不支持为类型 '%s' 的任务创建模型实例", taskModel.Type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user