修改domain包
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/device"
|
||||
@@ -12,38 +13,39 @@ import (
|
||||
|
||||
// FullCollectionTask 实现了 plan.Task 接口,用于执行一次全量的设备数据采集
|
||||
type FullCollectionTask struct {
|
||||
ctx context.Context
|
||||
log *models.TaskExecutionLog
|
||||
deviceRepo repository.DeviceRepository
|
||||
deviceService device.Service
|
||||
logger *logs.Logger
|
||||
}
|
||||
|
||||
// NewFullCollectionTask 创建一个全量采集任务实例
|
||||
func NewFullCollectionTask(
|
||||
ctx context.Context,
|
||||
log *models.TaskExecutionLog,
|
||||
deviceRepo repository.DeviceRepository,
|
||||
deviceService device.Service,
|
||||
logger *logs.Logger,
|
||||
) plan.Task {
|
||||
return &FullCollectionTask{
|
||||
ctx: ctx,
|
||||
log: log,
|
||||
deviceRepo: deviceRepo,
|
||||
deviceService: deviceService,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Execute 是任务的核心执行逻辑
|
||||
func (t *FullCollectionTask) Execute() error {
|
||||
t.logger.Infow("开始执行全量采集任务", "task_id", t.log.TaskID, "task_type", t.log.Task.Type, "log_id", t.log.ID)
|
||||
func (t *FullCollectionTask) Execute(ctx context.Context) error {
|
||||
taskCtx, logger := logs.Trace(ctx, t.ctx, "Execute")
|
||||
logger.Infow("开始执行全量采集任务", "task_id", t.log.TaskID, "task_type", t.log.Task.Type, "log_id", t.log.ID)
|
||||
|
||||
sensors, err := t.deviceRepo.ListAllSensors()
|
||||
sensors, err := t.deviceRepo.ListAllSensors(taskCtx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("全量采集任务: 从数据库获取所有传感器失败: %w", err)
|
||||
}
|
||||
|
||||
if len(sensors) == 0 {
|
||||
t.logger.Infow("全量采集任务: 未发现任何传感器设备,跳过本次采集", "task_id", t.log.TaskID, "task_type", t.log.Task.Type, "log_id", t.log.ID)
|
||||
logger.Infow("全量采集任务: 未发现任何传感器设备,跳过本次采集", "task_id", t.log.TaskID, "task_type", t.log.Task.Type, "log_id", t.log.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -54,15 +56,15 @@ func (t *FullCollectionTask) Execute() error {
|
||||
|
||||
var firstError error
|
||||
for controllerID, controllerSensors := range sensorsByController {
|
||||
t.logger.Infow("全量采集任务: 准备为区域主控下的传感器下发采集指令",
|
||||
logger.Infow("全量采集任务: 准备为区域主控下的传感器下发采集指令",
|
||||
"task_id", t.log.TaskID,
|
||||
"task_type", t.log.Task.Type,
|
||||
"log_id", t.log.ID,
|
||||
"controller_id", controllerID,
|
||||
"sensor_count", len(controllerSensors),
|
||||
)
|
||||
if err := t.deviceService.Collect(controllerID, controllerSensors); err != nil {
|
||||
t.logger.Errorw("全量采集任务: 为区域主控下发采集指令失败",
|
||||
if err := t.deviceService.Collect(taskCtx, controllerID, controllerSensors); err != nil {
|
||||
logger.Errorw("全量采集任务: 为区域主控下发采集指令失败",
|
||||
"task_id", t.log.TaskID,
|
||||
"task_type", t.log.Task.Type,
|
||||
"log_id", t.log.ID,
|
||||
@@ -79,13 +81,14 @@ func (t *FullCollectionTask) Execute() error {
|
||||
return fmt.Errorf("全量采集任务执行期间发生错误: %w", firstError)
|
||||
}
|
||||
|
||||
t.logger.Infow("全量采集任务执行完成", "task_id", t.log.TaskID, "task_type", t.log.Task.Type, "log_id", t.log.ID)
|
||||
logger.Infow("全量采集任务执行完成", "task_id", t.log.TaskID, "task_type", t.log.Task.Type, "log_id", t.log.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnFailure 定义了当 Execute 方法返回错误时,需要执行的回滚或清理逻辑
|
||||
func (t *FullCollectionTask) OnFailure(executeErr error) {
|
||||
t.logger.Errorw("全量采集任务执行失败",
|
||||
func (t *FullCollectionTask) OnFailure(ctx context.Context, executeErr error) {
|
||||
logger := logs.TraceLogger(ctx, t.ctx, "OnFailure")
|
||||
logger.Errorw("全量采集任务执行失败",
|
||||
"task_id", t.log.TaskID,
|
||||
"task_type", t.log.Task.Type,
|
||||
"log_id", t.log.ID,
|
||||
@@ -94,7 +97,7 @@ func (t *FullCollectionTask) OnFailure(executeErr error) {
|
||||
}
|
||||
|
||||
// ResolveDeviceIDs 获取当前任务需要使用的设备ID列表
|
||||
func (t *FullCollectionTask) ResolveDeviceIDs() ([]uint, error) {
|
||||
func (t *FullCollectionTask) ResolveDeviceIDs(ctx context.Context) ([]uint, error) {
|
||||
// 全量采集任务不和任何设备绑定, 每轮采集都会重新获取全量传感器
|
||||
return []uint{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user