实现 AlarmNotificationTask
This commit is contained in:
@@ -6,9 +6,12 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
notify_domain "git.huangwc.com/pig/pig-farm-controller/internal/domain/notify"
|
||||
"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"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/notify"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
)
|
||||
|
||||
// AlarmNotificationTaskParams 定义了 AlarmNotificationTask 的参数结构
|
||||
@@ -23,21 +26,22 @@ type AlarmNotificationTask struct {
|
||||
ctx context.Context
|
||||
taskLog *models.TaskExecutionLog
|
||||
|
||||
// notificationIntervals 告警通知的发送间隔时间,键为告警等级,值为 time.Duration
|
||||
notificationIntervals map[models.SeverityLevel]time.Duration
|
||||
// alarmNotificationTaskParams 是任务配置
|
||||
alarmNotificationTaskParams AlarmNotificationTaskParams
|
||||
|
||||
onceParse sync.Once // 保证解析参数只执行一次
|
||||
|
||||
// TODO: 根据实际需求添加告警通知相关的依赖,例如:
|
||||
// notificationService notification.Service
|
||||
// alarmRepository repository.AlarmRepository
|
||||
notificationService notify_domain.Service
|
||||
alarmRepository repository.AlarmRepository
|
||||
}
|
||||
|
||||
// NewAlarmNotificationTask 创建一个新的告警通知发送任务实例
|
||||
func NewAlarmNotificationTask(ctx context.Context, taskLog *models.TaskExecutionLog) plan.Task {
|
||||
func NewAlarmNotificationTask(ctx context.Context, taskLog *models.TaskExecutionLog, service notify_domain.Service, alarmRepository repository.AlarmRepository) plan.Task {
|
||||
return &AlarmNotificationTask{
|
||||
ctx: ctx,
|
||||
taskLog: taskLog,
|
||||
ctx: ctx,
|
||||
taskLog: taskLog,
|
||||
alarmRepository: alarmRepository,
|
||||
notificationService: service,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +54,47 @@ func (t *AlarmNotificationTask) Execute(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: 实现告警通知发送逻辑,可以使用 t.notificationIntervals 来获取不同等级的发送间隔
|
||||
// 获取是否有待发送告警通知, 用于优化性能
|
||||
alarmsCount, err := t.alarmRepository.CountAlarmsForNotification(taskCtx, t.alarmNotificationTaskParams.NotificationIntervals)
|
||||
if err != nil {
|
||||
logger.Errorf("任务 %v: 获取告警数量失败: %v", t.taskLog.TaskID, err)
|
||||
return err
|
||||
}
|
||||
if alarmsCount == 0 {
|
||||
logger.Debugf("没有待发送的告警通知, 跳过任务, 任务ID: %d", t.taskLog.TaskID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取所有待发送的告警通知
|
||||
alarms, err := t.alarmRepository.ListAlarmsForNotification(taskCtx, t.alarmNotificationTaskParams.NotificationIntervals)
|
||||
if err != nil {
|
||||
logger.Errorf("任务 %v: 获取告警列表失败: %v", t.taskLog.TaskID, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// 发送通知
|
||||
for _, alarm := range alarms {
|
||||
// TODO 因为还没做权限管理, 所以暂时通过广播形式发给所有用户
|
||||
err = t.notificationService.BroadcastAlarm(taskCtx, notify.AlarmContent{
|
||||
Title: alarm.AlarmSummary,
|
||||
Message: alarm.AlarmDetails,
|
||||
Level: alarm.Level,
|
||||
Timestamp: time.Now(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
// 非致命错误
|
||||
logger.Errorf("任务 %v: 发送告警通知失败: %v", t.taskLog.TaskID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 能发送通知的告警要么是忽略期已过且到达触发时间, 要么是不忽略且到达触发时间, 二者都应该取消忽略并刷新最后一次发送时间
|
||||
err = t.alarmRepository.UpdateAlarmNotificationStatus(taskCtx, alarm.ID, time.Now(), false, nil)
|
||||
if err != nil {
|
||||
// 非致命错误, 没有必要因为更新失败影响后续消息发送
|
||||
logger.Errorf("任务 %v: 更新告警通知状态失败: %v", t.taskLog.TaskID, err)
|
||||
}
|
||||
}
|
||||
|
||||
logger.Infof("告警通知发送任务执行完成, 任务ID: %d", t.taskLog.TaskID)
|
||||
return nil
|
||||
@@ -60,8 +104,6 @@ func (t *AlarmNotificationTask) Execute(ctx context.Context) error {
|
||||
func (t *AlarmNotificationTask) OnFailure(ctx context.Context, executeErr error) {
|
||||
logger := logs.TraceLogger(ctx, t.ctx, "OnFailure")
|
||||
logger.Errorf("告警通知发送任务执行失败, 任务ID: %d, 错误: %v", t.taskLog.TaskID, executeErr)
|
||||
|
||||
// TODO: 实现告警通知发送失败时的回滚或清理逻辑
|
||||
}
|
||||
|
||||
// ResolveDeviceIDs 从任务配置中解析并返回所有关联的设备ID列表
|
||||
@@ -89,13 +131,7 @@ func (t *AlarmNotificationTask) parseParameters(ctx context.Context) error {
|
||||
return
|
||||
}
|
||||
|
||||
// 初始化 notificationIntervals
|
||||
t.notificationIntervals = make(map[models.SeverityLevel]time.Duration)
|
||||
|
||||
// 将 uint 类型的秒数转换为 time.Duration
|
||||
for level, seconds := range params.NotificationIntervals {
|
||||
t.notificationIntervals[level] = time.Duration(seconds) * time.Minute
|
||||
}
|
||||
t.alarmNotificationTaskParams = params
|
||||
|
||||
})
|
||||
return err
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/device"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/notify"
|
||||
"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"
|
||||
@@ -19,23 +20,31 @@ const (
|
||||
)
|
||||
|
||||
type taskFactory struct {
|
||||
ctx context.Context
|
||||
ctx context.Context
|
||||
|
||||
sensorDataRepo repository.SensorDataRepository
|
||||
deviceRepo repository.DeviceRepository
|
||||
deviceService device.Service
|
||||
alarmRepo repository.AlarmRepository
|
||||
|
||||
deviceService device.Service
|
||||
notificationService notify.Service
|
||||
}
|
||||
|
||||
func NewTaskFactory(
|
||||
ctx context.Context,
|
||||
sensorDataRepo repository.SensorDataRepository,
|
||||
deviceRepo repository.DeviceRepository,
|
||||
alarmRepo repository.AlarmRepository,
|
||||
deviceService device.Service,
|
||||
notifyService notify.Service,
|
||||
) plan.TaskFactory {
|
||||
return &taskFactory{
|
||||
ctx: ctx,
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
deviceRepo: deviceRepo,
|
||||
deviceService: deviceService,
|
||||
ctx: ctx,
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
deviceRepo: deviceRepo,
|
||||
alarmRepo: alarmRepo,
|
||||
deviceService: deviceService,
|
||||
notificationService: notifyService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +59,7 @@ func (t *taskFactory) Production(ctx context.Context, claimedLog *models.TaskExe
|
||||
case models.TaskTypeFullCollection:
|
||||
return NewFullCollectionTask(logs.AddCompName(baseCtx, CompNameFullCollectionTask), claimedLog, t.deviceRepo, t.deviceService)
|
||||
case models.TaskTypeAlarmNotification:
|
||||
return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), claimedLog)
|
||||
return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), claimedLog, t.notificationService, t.alarmRepo)
|
||||
default:
|
||||
// TODO 这里直接panic合适吗? 不过这个场景确实不该出现任何异常的任务类型
|
||||
logger.Panicf("不支持的任务类型: %s", claimedLog.Task.Type)
|
||||
@@ -79,7 +88,7 @@ func (t *taskFactory) CreateTaskFromModel(ctx context.Context, taskModel *models
|
||||
case models.TaskTypeFullCollection:
|
||||
return NewFullCollectionTask(logs.AddCompName(baseCtx, CompNameFullCollectionTask), tempLog, t.deviceRepo, t.deviceService), nil
|
||||
case models.TaskTypeAlarmNotification:
|
||||
return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), tempLog), nil
|
||||
return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), tempLog, t.notificationService, t.alarmRepo), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("不支持为类型 '%s' 的任务创建模型实例", taskModel.Type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user