定义ReleaseFeedWeightTask并注入依赖
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"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"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/transport"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -80,8 +81,11 @@ type Scheduler struct {
|
||||
workers int
|
||||
pendingTaskRepo repository.PendingTaskRepository
|
||||
executionLogRepo repository.ExecutionLogRepository
|
||||
deviceRepo repository.DeviceRepository
|
||||
sensorDataRepo repository.SensorDataRepository
|
||||
planRepo repository.PlanRepository
|
||||
analysisPlanTaskManager *AnalysisPlanTaskManager // <--- 注入共享的 Manager
|
||||
comm transport.Communicator
|
||||
analysisPlanTaskManager *AnalysisPlanTaskManager
|
||||
progressTracker *ProgressTracker
|
||||
|
||||
pool *ants.Pool // 使用 ants 协程池来管理并发
|
||||
@@ -93,16 +97,22 @@ type Scheduler struct {
|
||||
func NewScheduler(
|
||||
pendingTaskRepo repository.PendingTaskRepository,
|
||||
executionLogRepo repository.ExecutionLogRepository,
|
||||
deviceRepo repository.DeviceRepository,
|
||||
sensorDataRepo repository.SensorDataRepository,
|
||||
planRepo repository.PlanRepository,
|
||||
analysisPlanTaskManager *AnalysisPlanTaskManager, // <--- 注入 Manager
|
||||
comm transport.Communicator,
|
||||
analysisPlanTaskManager *AnalysisPlanTaskManager,
|
||||
logger *logs.Logger,
|
||||
interval time.Duration,
|
||||
numWorkers int) *Scheduler {
|
||||
return &Scheduler{
|
||||
pendingTaskRepo: pendingTaskRepo,
|
||||
executionLogRepo: executionLogRepo,
|
||||
deviceRepo: deviceRepo,
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
planRepo: planRepo,
|
||||
analysisPlanTaskManager: analysisPlanTaskManager, // <--- 注入 Manager
|
||||
comm: comm,
|
||||
analysisPlanTaskManager: analysisPlanTaskManager,
|
||||
logger: logger,
|
||||
pollingInterval: interval,
|
||||
workers: numWorkers,
|
||||
@@ -261,11 +271,7 @@ func (s *Scheduler) runTask(claimedLog *models.TaskExecutionLog) error {
|
||||
|
||||
} else {
|
||||
// 执行普通任务
|
||||
task := s.taskFactory(claimedLog.Task.Type)
|
||||
if err := task.ParseParams(s.logger, claimedLog); err != nil {
|
||||
s.logger.Errorf("[严重] 任务参数解析失败, 日志ID: %d, 错误: %v", claimedLog.ID, err)
|
||||
return err
|
||||
}
|
||||
task := s.taskFactory(claimedLog)
|
||||
|
||||
if err := task.Execute(); err != nil {
|
||||
s.logger.Errorf("[严重] 任务执行失败, 日志ID: %d, 错误: %v", claimedLog.ID, err)
|
||||
@@ -278,10 +284,13 @@ func (s *Scheduler) runTask(claimedLog *models.TaskExecutionLog) error {
|
||||
}
|
||||
|
||||
// taskFactory 会根据任务类型初始化对应任务
|
||||
func (s *Scheduler) taskFactory(taskType models.TaskType) Task {
|
||||
switch taskType {
|
||||
func (s *Scheduler) taskFactory(claimedLog *models.TaskExecutionLog) Task {
|
||||
switch claimedLog.Task.Type {
|
||||
case models.TaskTypeWaiting:
|
||||
return NewDelayTask()
|
||||
return NewDelayTask(s.logger, claimedLog)
|
||||
case models.TaskTypeReleaseFeedWeight:
|
||||
return NewReleaseFeedWeightTask(s.deviceRepo, s.sensorDataRepo, s.comm)
|
||||
|
||||
default:
|
||||
// TODO 这里直接panic合适吗? 不过这个场景确实不该出现任何异常的任务类型
|
||||
panic("不支持的任务类型")
|
||||
|
||||
Reference in New Issue
Block a user