Files
pig-farm-controller/design/provide-logger-with-mothed/task-domain.md
2025-11-03 23:17:06 +08:00

22 KiB
Raw Blame History

  • internal/domain/audit/service.go (audit.Service)
    • 结构体改造:
      • 移除 service 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewService):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 audit.Service 创建其专属的 selfCtxselfCtx := logs.AddCompName(ctx, "AuditService")
      • 将这个 selfCtx 赋值给 service 结构体的 selfCtx 成员。
    • 公共方法改造 (LogAction):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "LogAction") 获取新的 context.Contextlogger 实例。
      • 将所有对 s.logger.Warnws.logger.Errorw 的调用替换为 logger.Warnwlogger.Errorw
      • 确保所有对 s.userActionLogRepository.Create 的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/device/general_device_service.go (device.Service)
    • 结构体改造:
      • 移除 GeneralDeviceService 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewGeneralDeviceService):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 GeneralDeviceService 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "GeneralDeviceService")
      • 将这个 selfCtx 赋值给 GeneralDeviceService 结构体的 selfCtx 成员。
    • 公共方法改造 (Switch, Collect):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, g.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 g.logger.Errorf, g.logger.Infof, g.logger.Warnf, g.logger.Debugf, g.logger.DPanicf 的调用替换为 logger.Errorf, logger.Infof, logger.Warnf, logger.Debugf, logger.DPanicf
      • 确保所有对 g.deviceRepo, g.deviceCommandLogRepo, g.pendingCollectionRepo, g.comm 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/notify/notify.go (domain_notify.Service - failoverService 实现)
    • 结构体改造:
      • 移除 failoverService 结构体中的 log *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewFailoverService):
      • 修改函数签名,移除 log *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 failoverService 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "FailoverService")
      • 将这个 selfCtx 赋值给 failoverService 结构体的 selfCtx 成员。
    • 公共方法改造 (SendBatchAlarm, BroadcastAlarm, SendTestMessage):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 s.log.Infow, s.log.Errorw, s.log.Warnw 的调用替换为 logger.Infow, logger.Errorw, logger.Warnw
      • 确保所有对 s.userRepo, s.primaryNotifier.Send, s.notifiers, s.notificationRepo 等依赖的调用都将 newCtx 作为第一个参数传递。
    • 内部辅助方法改造 (sendAlarmToUser, recordNotificationAttempt):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 s.log.Errorw, s.log.Infow, s.log.Warnw 的调用替换为 logger.Errorw, logger.Infow, logger.Warnw
      • 确保所有对 s.userRepo, s.primaryNotifier.Send, s.notifiers, s.notificationRepo 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/pig/pen_transfer_manager.go (pig.PigPenTransferManager)
    • 结构体改造:
      • 移除 pigPenTransferManager 结构体中可能存在的 logger *logs.Logger 成员(如果未来添加)。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewPigPenTransferManager):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 pigPenTransferManager 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "PigPenTransferManager")
      • 将这个 selfCtx 赋值给 pigPenTransferManager 结构体的 selfCtx 成员。
    • 公共方法改造 ( 所有方法,例如 LogTransfer, GetPenByID, GetPensByBatchID, UpdatePenFields, GetCurrentPigsInPen, GetTotalPigsInPensForBatchTx, ReleasePen):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数(在 tx *gorm.DB 之前)。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 s.logger.Errorfs.logger.Infof 的调用替换为 logger.Errorflogger.Infof
      • 确保所有对 s.penRepo, s.logRepo, s.pigBatchRepo 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/pig/pig_trade_manager.go (pig.PigTradeManager)
    • 结构体改造:
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewPigTradeManager):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 pigTradeManager 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "PigTradeManager")
      • 将这个 selfCtx 赋值给 pigTradeManager 结构体的 selfCtx 成员。
    • 公共方法改造 (SellPig, BuyPig):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数(在 tx *gorm.DB 之前)。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 确保所有对 s.tradeRepo 的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/pig/pig_sick_manager.go (pig.SickPigManager)
    • 结构体改造:
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewSickPigManager):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 sickPigManager 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "SickPigManager")
      • 将这个 selfCtx 赋值给 sickPigManager 结构体的 selfCtx 成员。
    • 公共方法改造 (ProcessSickPigLog, GetCurrentSickPigCount):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数(在 tx *gorm.DB 之前)。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 确保所有对 s.sickLogRepo, s.medicationLogRepo 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/pig/pig_batch_service.go (pig.PigBatchService)
    • 结构体改造:
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewPigBatchService):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 pigBatchService 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "PigBatchService")
      • 将这个 selfCtx 赋值给 pigBatchService 结构体的 selfCtx 成员。
    • 公共方法改造 ( 所有方法,例如 CreatePigBatch, GetPigBatch, UpdatePigBatch, DeletePigBatch, ListPigBatches, AssignEmptyPensToBatch, MovePigsIntoPen, ReclassifyPenToNewBatch, RemoveEmptyPenFromBatch, GetCurrentPigQuantity, GetCurrentPigsInPen, GetTotalPigsInPensForBatch, UpdatePigBatchQuantity, SellPigs, BuyPigs, TransferPigsAcrossBatches, TransferPigsWithinBatch, RecordSickPigs, RecordSickPigRecovery, RecordSickPigDeath, RecordSickPigCull, RecordDeath, RecordCull):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 确保所有对 s.pigBatchRepo, s.pigBatchLogRepo, s.uow, s.transferSvc, s.tradeSvc, s.sickSvc 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/plan/analysis_plan_task_manager.go (plan.AnalysisPlanTaskManager)
    • 结构体改造:
      • 移除 analysisPlanTaskManagerImpl 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewAnalysisPlanTaskManager):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 analysisPlanTaskManagerImpl 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "AnalysisPlanTaskManager")
      • 将这个 selfCtx 赋值给 analysisPlanTaskManagerImpl 结构体的 selfCtx 成员。
    • 公共方法改造 (Refresh, CreateOrUpdateTrigger, EnsureAnalysisTaskDefinition):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, m.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 m.logger.Info, m.logger.Errorf, m.logger.Warnf 的调用替换为 logger.Info, logger.Errorf, logger.Warnf
      • 确保所有对 m.planRepo, m.pendingTaskRepo, m.executionLogRepo 等依赖的调用都将 newCtx 作为第一个参数传递。
    • 内部辅助方法改造 (getRefreshData, cleanupInvalidTasks, addOrUpdateTriggers, createTriggerTask):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, m.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 m.logger.Info, m.logger.Errorf, m.logger.Warnf 的调用替换为 logger.Info, logger.Errorf, logger.Warnf
      • 确保所有对 m.planRepo, m.pendingTaskRepo, m.executionLogRepo 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/plan/plan_execution_manager.go (plan.ExecutionManager)
    • 结构体改造:
      • 移除 planExecutionManagerImpl 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewPlanExecutionManager):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 planExecutionManagerImpl 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "PlanExecutionManager")
      • 将这个 selfCtx 赋值给 planExecutionManagerImpl 结构体的 selfCtx 成员。
    • 公共方法改造 (Start, Stop):
      • Start 方法中,使用 newCtx, logger := logs.Trace(context.Background(), s.selfCtx, "Start") 获取 logger 实例进行日志记录。
      • Stop 方法中,使用 newCtx, logger := logs.Trace(context.Background(), s.selfCtx, "Stop") 获取 logger 实例进行日志记录。
    • 内部辅助方法改造 (run, claimAndSubmit, handleRequeue, processTask, runTask, analysisPlan, updateTaskExecutionLogStatus, handlePlanTermination, handlePlanCompletion):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数(如果方法内部需要传递上下文)。
      • 在每个方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 s.logger.Errorf, s.logger.Warnf, s.logger.Infof, s.logger.DPanicf 的调用替换为 logger.Errorf, logger.Warnf, logger.Infof, logger.DPanicf
      • 确保所有对 s.pendingTaskRepo, s.executionLogRepo, s.deviceRepo, s.sensorDataRepo, s.planRepo, s.analysisPlanTaskManager, s.taskFactory, s.deviceService 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/plan/plan_service.go (plan.Service)
    • 结构体改造:
      • 移除 planServiceImpl 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewPlanService):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 planServiceImpl 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "PlanService")
      • 将这个 selfCtx 赋值给 planServiceImpl 结构体的 selfCtx 成员。
    • 公共方法改造 (Start, Stop, RefreshPlanTriggers, CreatePlan, GetPlanByID, ListPlans, UpdatePlan, DeletePlan, StartPlan, StopPlan):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 s.logger.Errorf, s.logger.Infof, s.logger.Warnf 的调用替换为 logger.Errorf, logger.Infof, logger.Warnf
      • 确保所有对 s.executionManager, s.taskManager, s.planRepo, s.deviceRepo, s.unitOfWork, s.taskFactory 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/task/task.go (task.TaskFactory)
    • 结构体改造:
      • 移除 taskFactory 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewTaskFactory):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 taskFactory 创建其专属的 selfCtxselfCtx := logs.AddCompName(ctx, "TaskFactory")
      • 将这个 selfCtx 赋值给 taskFactory 结构体的 selfCtx 成员。
    • 公共方法改造 (Production, CreateTaskFromModel):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法内部,使用 newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 t.logger.Panicf 的调用替换为 logger.Panicf
      • 将所有对 NewDelayTask, NewReleaseFeedWeightTask, NewFullCollectionTask 的调用,传递 newCtx
  • internal/domain/task/release_feed_weight_task.go
    • 结构体改造:
      • 移除 ReleaseFeedWeightTask 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewReleaseFeedWeightTask):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 ReleaseFeedWeightTask 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "ReleaseFeedWeightTask")
      • 将这个 selfCtx 赋值给 ReleaseFeedWeightTask 结构体的 selfCtx 成员。
    • 公共方法改造 (Execute, OnFailure, ResolveDeviceIDs):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, r.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 r.logger.Infof, r.logger.Errorf, r.logger.Warnf, r.logger.Debugf 的调用替换为 logger.Infof, logger.Errorf, logger.Warnf, logger.Debugf
      • 确保所有对 r.deviceRepo, r.sensorDataRepo, r.feedPort 等依赖的调用都将 newCtx 作为第一个参数传递。
    • 内部辅助方法改造 (getNowWeight, parseParameters):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, r.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 r.logger.Errorf, r.logger.Warnf 的调用替换为 logger.Errorf, logger.Warnf
      • 确保所有对 r.sensorDataRepo, r.deviceRepo 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/task/full_collection_task.go
    • 结构体改造:
      • 移除 FullCollectionTask 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewFullCollectionTask):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 FullCollectionTask 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "FullCollectionTask")
      • 将这个 selfCtx 赋值给 FullCollectionTask 结构体的 selfCtx 成员。
    • 公共方法改造 (Execute, OnFailure, ResolveDeviceIDs):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 t.logger.Infow, t.logger.Errorw 的调用替换为 logger.Infow, logger.Errorw
      • 确保所有对 t.deviceRepo, t.deviceService 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/domain/task/delay_task.go
    • 结构体改造:
      • 移除 DelayTask 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewDelayTask):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 DelayTask 创建其专属的 selfCtxselfCtx := logs.AddCompName(ctx, "DelayTask")
      • 将这个 selfCtx 赋值给 DelayTask 结构体的 selfCtx 成员。
    • 公共方法改造 (Execute, OnFailure, ResolveDeviceIDs):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, d.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 d.logger.Infofd.logger.Errorf 的调用替换为 logger.Infoflogger.Errorf
    • 内部辅助方法改造 (parseParameters):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, d.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 d.logger.Errorf 的调用替换为 logger.Errorf
  • internal/domain/token/token_service.go (token.Service)
    • 结构体改造:
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewTokenService):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 tokenService 创建其专属的 selfCtxselfCtx := logs.AddCompName(ctx, "TokenService")
      • 将这个 selfCtx 赋值给 tokenService 结构体的 selfCtx 成员。
    • 公共方法改造 (GenerateToken, ParseToken):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 s.logger.* 的调用替换为 logger.* (如果存在)。