修改程序入口
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -14,19 +16,20 @@ const (
|
||||
|
||||
// initializeState 在应用启动时准备其初始数据状态。
|
||||
// 这包括清理任何因上次异常关闭而留下的悬空任务或请求。
|
||||
func (app *Application) initializeState() error {
|
||||
func (app *Application) initializeState(ctx context.Context) error {
|
||||
newCtx, logger := logs.Trace(ctx, app.Ctx, "InitializeState")
|
||||
// 初始化预定义系统计划 (致命错误)
|
||||
if err := app.initializeSystemPlans(); err != nil {
|
||||
if err := app.initializeSystemPlans(ctx); err != nil {
|
||||
return fmt.Errorf("初始化预定义系统计划失败: %w", err)
|
||||
}
|
||||
|
||||
// 清理待采集任务 (非致命错误)
|
||||
if err := app.initializePendingCollections(); err != nil {
|
||||
app.Logger.Errorw("清理待采集任务时发生非致命错误", "error", err)
|
||||
if err := app.initializePendingCollections(newCtx); err != nil {
|
||||
logger.Errorw("清理待采集任务时发生非致命错误", "error", err)
|
||||
}
|
||||
|
||||
// 初始化待执行任务列表 (致命错误)
|
||||
if err := app.initializePendingTasks(); err != nil {
|
||||
if err := app.initializePendingTasks(newCtx); err != nil {
|
||||
return fmt.Errorf("初始化待执行任务列表失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -34,8 +37,9 @@ func (app *Application) initializeState() error {
|
||||
}
|
||||
|
||||
// initializeSystemPlans 确保预定义的系统计划在数据库中存在并保持最新。
|
||||
func (app *Application) initializeSystemPlans() error {
|
||||
app.Logger.Info("开始检查并更新预定义的系统计划...")
|
||||
func (app *Application) initializeSystemPlans(ctx context.Context) error {
|
||||
logger := logs.TraceLogger(ctx, app.Ctx, "InitializeSystemPlans")
|
||||
logger.Info("开始检查并更新预定义的系统计划...")
|
||||
|
||||
// 动态构建预定义计划列表
|
||||
predefinedSystemPlans := app.getPredefinedSystemPlans()
|
||||
@@ -60,7 +64,7 @@ func (app *Application) initializeSystemPlans() error {
|
||||
|
||||
if foundExistingPlan, ok := existingPlanMap[predefinedPlan.Name]; ok {
|
||||
// 如果计划存在,则进行无差别更新
|
||||
app.Logger.Infof("预定义计划 '%s' 已存在,正在进行无差别更新...", predefinedPlan.Name)
|
||||
logger.Infof("预定义计划 '%s' 已存在,正在进行无差别更新...", predefinedPlan.Name)
|
||||
|
||||
// 将数据库中已存在的计划的ID和运行时状态字段赋值给预定义计划
|
||||
predefinedPlan.ID = foundExistingPlan.ID
|
||||
@@ -69,20 +73,20 @@ func (app *Application) initializeSystemPlans() error {
|
||||
if err := app.Infra.repos.planRepo.UpdatePlan(predefinedPlan); err != nil {
|
||||
return fmt.Errorf("更新预定义计划 '%s' 失败: %w", predefinedPlan.Name, err)
|
||||
} else {
|
||||
app.Logger.Infof("成功更新预定义计划 '%s'。", predefinedPlan.Name)
|
||||
logger.Infof("成功更新预定义计划 '%s'。", predefinedPlan.Name)
|
||||
}
|
||||
} else {
|
||||
// 如果计划不存在, 则创建
|
||||
app.Logger.Infof("预定义计划 '%s' 不存在,正在创建...", predefinedPlan.Name)
|
||||
logger.Infof("预定义计划 '%s' 不存在,正在创建...", predefinedPlan.Name)
|
||||
if err := app.Infra.repos.planRepo.CreatePlan(predefinedPlan); err != nil {
|
||||
return fmt.Errorf("创建预定义计划 '%s' 失败: %w", predefinedPlan.Name, err)
|
||||
} else {
|
||||
app.Logger.Infof("成功创建预定义计划 '%s'。", predefinedPlan.Name)
|
||||
logger.Infof("成功创建预定义计划 '%s'。", predefinedPlan.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
app.Logger.Info("预定义系统计划检查完成。")
|
||||
logger.Info("预定义系统计划检查完成。")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -119,25 +123,26 @@ func (app *Application) getPredefinedSystemPlans() []models.Plan {
|
||||
// initializePendingCollections 在应用启动时处理所有未完成的采集请求。
|
||||
// 我们的策略是:任何在程序重启前仍处于“待处理”状态的请求,都应被视为已失败。
|
||||
// 这保证了系统在每次启动时都处于一个干净、确定的状态。
|
||||
func (app *Application) initializePendingCollections() error {
|
||||
app.Logger.Info("开始清理所有未完成的采集请求...")
|
||||
func (app *Application) initializePendingCollections(ctx context.Context) error {
|
||||
logger := logs.TraceLogger(ctx, app.Ctx, "InitializePendingCollections")
|
||||
logger.Info("开始清理所有未完成的采集请求...")
|
||||
|
||||
// 直接将所有 'pending' 状态的请求更新为 'timed_out'。
|
||||
count, err := app.Infra.repos.pendingCollectionRepo.MarkAllPendingAsTimedOut()
|
||||
if err != nil {
|
||||
return fmt.Errorf("清理未完成的采集请求失败: %v", err)
|
||||
} else if count > 0 {
|
||||
app.Logger.Infof("成功将 %d 个未完成的采集请求标记为超时。", count)
|
||||
logger.Infof("成功将 %d 个未完成的采集请求标记为超时。", count)
|
||||
} else {
|
||||
app.Logger.Info("没有需要清理的采集请求。")
|
||||
logger.Info("没有需要清理的采集请求。")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// initializePendingTasks 在应用启动时清理并刷新待执行任务列表。
|
||||
func (app *Application) initializePendingTasks() error {
|
||||
logger := app.Logger
|
||||
func (app *Application) initializePendingTasks(ctx context.Context) error {
|
||||
logger := logs.TraceLogger(ctx, app.Ctx, "InitializePendingTasks")
|
||||
planRepo := app.Infra.repos.planRepo
|
||||
pendingTaskRepo := app.Infra.repos.pendingTaskRepo
|
||||
executionLogRepo := app.Infra.repos.executionLogRepo
|
||||
|
||||
Reference in New Issue
Block a user