1. 增加任务调度器配置文件

2. 创建/更新计划会自动处理触发器
This commit is contained in:
2025-09-17 23:01:15 +08:00
parent f7a5e4737d
commit 810049d62e
7 changed files with 90 additions and 42 deletions

View File

@@ -5,8 +5,10 @@ import (
"os"
"os/signal"
"syscall"
"time"
"git.huangwc.com/pig/pig-farm-controller/internal/app/api"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/task"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/token"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/transport"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/config"
@@ -14,7 +16,6 @@ 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/task"
)
// Application 是整个应用的核心,封装了所有组件和生命周期。
@@ -22,7 +23,7 @@ type Application struct {
Config *config.Config
Logger *logs.Logger
Storage database.Storage
Executor *task.Executor
Executor *task.Scheduler
API *api.API // 添加 API 对象
}
@@ -44,9 +45,6 @@ func NewApplication(configPath string) (*Application, error) {
return nil, err // 错误已在 initStorage 中被包装
}
// 初始化任务执行器
executor := task.NewExecutor(cfg.Heartbeat.Concurrency, logger)
// 初始化 Token 服务
tokenService := token.NewTokenService([]byte(cfg.App.JWTSecret))
@@ -59,11 +57,23 @@ func NewApplication(configPath string) (*Application, error) {
// 初始化计划仓库
planRepo := repository.NewGormPlanRepository(storage.GetDB())
// 初始化待执行任务仓库
pendingTaskRepo := repository.NewGormPendingTaskRepository(storage.GetDB())
// 初始化执行日志仓库
executionLogRepo := repository.NewGormExecutionLogRepository(storage.GetDB())
// 初始化设备上行监听器
listenHandler := transport.NewChirpStackListener(logger)
// 初始化计划触发器管理器
analysisPlanTaskManager := task.NewAnalysisPlanTaskManager(planRepo, pendingTaskRepo, executionLogRepo, logger)
// 初始化任务执行器
executor := task.NewScheduler(pendingTaskRepo, executionLogRepo, planRepo, analysisPlanTaskManager, task.TaskFactory, logger, time.Duration(cfg.Task.Interval)*time.Second, cfg.Task.NumWorkers)
// 初始化 API 服务器
apiServer := api.NewAPI(cfg.Server, logger, userRepo, deviceRepo, planRepo, tokenService, listenHandler)
apiServer := api.NewAPI(cfg.Server, logger, userRepo, deviceRepo, planRepo, tokenService, listenHandler, analysisPlanTaskManager)
// 组装 Application 对象
app := &Application{