issue-18 优化代码(只保证编译通过没检查)

This commit is contained in:
2025-09-26 22:50:08 +08:00
parent d9fe1683d2
commit 23b7f66d74
17 changed files with 767 additions and 251 deletions

View File

@@ -31,6 +31,7 @@ type Application struct {
planRepo repository.PlanRepository
pendingTaskRepo repository.PendingTaskRepository
executionLogRepo repository.ExecutionLogRepository
pendingCollectionRepo repository.PendingCollectionRepository
analysisPlanTaskManager *task.AnalysisPlanTaskManager
}
@@ -76,17 +77,33 @@ func NewApplication(configPath string) (*Application, error) {
// 初始化命令下发历史仓库
deviceCommandLogRepo := repository.NewGormDeviceCommandLogRepository(storage.GetDB())
// 初始化待采集请求仓库
pendingCollectionRepo := repository.NewGormPendingCollectionRepository(storage.GetDB())
// 初始化设备上行监听器
listenHandler := transport.NewChirpStackListener(logger, sensorDataRepo, deviceRepo, deviceCommandLogRepo)
listenHandler := transport.NewChirpStackListener(logger, sensorDataRepo, deviceRepo, deviceCommandLogRepo, pendingCollectionRepo)
// 初始化计划触发器管理器
analysisPlanTaskManager := task.NewAnalysisPlanTaskManager(planRepo, pendingTaskRepo, executionLogRepo, logger)
// 初始化设备通信器
comm := lora.NewChirpStackTransport(cfg.ChirpStack, logger, deviceCommandLogRepo, deviceRepo)
// 初始化设备通信器 (纯粹的通信客户端)
comm := lora.NewChirpStackTransport(cfg.ChirpStack, logger)
// 初始化任务执行器
executor := task.NewScheduler(pendingTaskRepo, executionLogRepo, deviceRepo, sensorDataRepo, planRepo, comm, analysisPlanTaskManager, logger, time.Duration(cfg.Task.Interval)*time.Second, cfg.Task.NumWorkers)
executor := task.NewScheduler(
pendingTaskRepo,
executionLogRepo,
deviceRepo,
sensorDataRepo,
planRepo,
comm,
analysisPlanTaskManager,
logger,
deviceCommandLogRepo,
pendingCollectionRepo,
time.Duration(cfg.Task.Interval)*time.Second,
cfg.Task.NumWorkers,
)
// 初始化 API 服务器
apiServer := api.NewAPI(cfg.Server, logger, userRepo, deviceRepo, planRepo, sensorDataRepo, executionLogRepo, tokenService, listenHandler, analysisPlanTaskManager)
@@ -102,6 +119,7 @@ func NewApplication(configPath string) (*Application, error) {
planRepo: planRepo,
pendingTaskRepo: pendingTaskRepo,
executionLogRepo: executionLogRepo,
pendingCollectionRepo: pendingCollectionRepo,
analysisPlanTaskManager: analysisPlanTaskManager,
}
@@ -112,7 +130,13 @@ func NewApplication(configPath string) (*Application, error) {
func (app *Application) Start() error {
app.Logger.Info("应用启动中...")
// --- 新增逻辑:初始化待执行任务列表 ---
// --- 清理待采集任务 ---
if err := app.initializePendingCollections(); err != nil {
// 这是一个非致命错误,记录它,但应用应继续启动
app.Logger.Error(err)
}
// --- 初始化待执行任务列表 ---
if err := app.initializePendingTasks(
app.planRepo, // 传入 planRepo
app.pendingTaskRepo, // 传入 pendingTaskRepo
@@ -160,6 +184,25 @@ func (app *Application) Stop() error {
return nil
}
// initializePendingCollections 在应用启动时处理所有未完成的采集请求。
// 我们的策略是:任何在程序重启前仍处于“待处理”状态的请求,都应被视为已失败。
// 这保证了系统在每次启动时都处于一个干净、确定的状态。
func (app *Application) initializePendingCollections() error {
app.Logger.Info("开始清理所有未完成的采集请求...")
// 直接将所有 'pending' 状态的请求更新为 'timed_out'。
count, err := app.pendingCollectionRepo.MarkAllPendingAsTimedOut()
if err != nil {
return fmt.Errorf("清理未完成的采集请求失败: %v", err)
} else if count > 0 {
app.Logger.Infof("成功将 %d 个未完成的采集请求标记为超时。", count)
} else {
app.Logger.Info("没有需要清理的采集请求。")
}
return nil
}
// initializePendingTasks 在应用启动时清理并刷新待执行任务列表。
func (app *Application) initializePendingTasks(
planRepo repository.PlanRepository,