实现定时采集
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/webhook"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/audit"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/collection"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/device"
|
||||
domain_notify "git.huangwc.com/pig/pig-farm-controller/internal/domain/notify"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/pig"
|
||||
@@ -28,11 +29,12 @@ import (
|
||||
|
||||
// Application 是整个应用的核心,封装了所有组件和生命周期。
|
||||
type Application struct {
|
||||
Config *config.Config
|
||||
Logger *logs.Logger
|
||||
Storage database.Storage
|
||||
Executor *task.Scheduler
|
||||
API *api.API // 添加 API 对象
|
||||
Config *config.Config
|
||||
Logger *logs.Logger
|
||||
Storage database.Storage
|
||||
Executor *task.Scheduler
|
||||
API *api.API
|
||||
Collector collection.Collector
|
||||
|
||||
// 新增的仓库和管理器字段,以便在 initializePendingTasks 中访问
|
||||
planRepo repository.PlanRepository
|
||||
@@ -177,6 +179,14 @@ func NewApplication(configPath string) (*Application, error) {
|
||||
cfg.Task.NumWorkers,
|
||||
)
|
||||
|
||||
// --- 初始化定时采集器 ---
|
||||
timedCollector := collection.NewTimedCollector(
|
||||
deviceRepo,
|
||||
generalDeviceService,
|
||||
logger,
|
||||
time.Duration(cfg.Collection.Interval)*time.Second,
|
||||
)
|
||||
|
||||
// 初始化 API 服务器
|
||||
apiServer := api.NewAPI(
|
||||
cfg.Server,
|
||||
@@ -204,6 +214,7 @@ func NewApplication(configPath string) (*Application, error) {
|
||||
Storage: storage,
|
||||
Executor: executor,
|
||||
API: apiServer,
|
||||
Collector: timedCollector,
|
||||
planRepo: planRepo,
|
||||
pendingTaskRepo: pendingTaskRepo,
|
||||
executionLogRepo: executionLogRepo,
|
||||
@@ -327,6 +338,9 @@ func (app *Application) Start() error {
|
||||
// 启动任务执行器
|
||||
app.Executor.Start()
|
||||
|
||||
// 启动定时采集器
|
||||
app.Collector.Start()
|
||||
|
||||
// 启动 API 服务器
|
||||
app.API.Start()
|
||||
|
||||
@@ -349,6 +363,9 @@ func (app *Application) Stop() error {
|
||||
// 关闭任务执行器
|
||||
app.Executor.Stop()
|
||||
|
||||
// 关闭定时采集器
|
||||
app.Collector.Stop()
|
||||
|
||||
// 断开数据库连接
|
||||
if err := app.Storage.Disconnect(); err != nil {
|
||||
app.Logger.Errorw("数据库连接断开失败", "error", err)
|
||||
|
||||
Reference in New Issue
Block a user