From 0b5b26d5b0fa34fbfa3b2d515ca2a944ac011cf6 Mon Sep 17 00:00:00 2001 From: yarnom Date: Tue, 14 Oct 2025 11:14:26 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8B=86=E5=88=86=20fusion=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/service-fusion/main.go | 50 +++++++++++++++++++++++++++++ internal/server/udp.go | 65 ++------------------------------------ 2 files changed, 53 insertions(+), 62 deletions(-) create mode 100644 cmd/service-fusion/main.go diff --git a/cmd/service-fusion/main.go b/cmd/service-fusion/main.go new file mode 100644 index 0000000..af18d34 --- /dev/null +++ b/cmd/service-fusion/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "context" + "log" + "os" + "os/signal" + "syscall" + "time" + "weatherstation/internal/fusion" + "weatherstation/internal/server" +) + +func main() { + server.SetupLogger() + + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + go func() { + for { + if ctx.Err() != nil { + return + } + now := time.Now() + next := now.Truncate(time.Hour).Add(time.Hour).Add(5 * time.Minute) + sleep := time.Until(next) + if sleep < 0 { + sleep = 0 + } + t := time.NewTimer(sleep) + select { + case <-ctx.Done(): + t.Stop() + return + case <-t.C: + } + + issued := next.Truncate(time.Hour) + if err := fusion.RunForIssued(context.Background(), issued); err != nil { + log.Printf("[service-fusion] run failed: %v", err) + } else { + log.Printf("[service-fusion] completed issued=%s", issued.Format("2006-01-02 15:04:05")) + } + } + }() + + <-ctx.Done() + log.Println("service-fusion shutting down") +} diff --git a/internal/server/udp.go b/internal/server/udp.go index a6c930d..d73ae2b 100644 --- a/internal/server/udp.go +++ b/internal/server/udp.go @@ -15,8 +15,6 @@ import ( "time" "unicode/utf8" "weatherstation/internal/config" - "weatherstation/internal/forecast" - "weatherstation/internal/fusion" "weatherstation/internal/tools" "weatherstation/model" ) @@ -146,67 +144,10 @@ func StartUDPServer() error { } }() - // 后台定时:每小时拉取open-meteo(全站) - go func() { - for { - now := time.Now() - next := now.Truncate(time.Hour).Add(time.Hour) - time.Sleep(time.Until(next)) - if err := forecast.RunOpenMeteoFetch(context.Background()); err != nil { - log.Printf("open-meteo 定时拉取失败: %v", err) - } else { - log.Printf("open-meteo 定时拉取完成") - } - } - }() + // 说明:原有的 open-meteo/彩云/CMA 定时抓取已移除,避免与独立的 service-forecast 重复调度。 + // 若需要启用预报抓取,请运行 `cmd/service-forecast` 服务。 - // 后台定时:每小时拉取彩云(全站) - go func() { - token := config.GetConfig().Forecast.CaiyunToken - if token == "" { - log.Printf("caiyun token 未配置,跳过彩云定时拉取(配置 forecast.caiyun_token 可启用)") - return - } - for { - now := time.Now() - next := now.Truncate(time.Hour).Add(time.Hour) - time.Sleep(time.Until(next)) - if err := forecast.RunCaiyunFetch(context.Background(), token); err != nil { - log.Printf("caiyun 定时拉取失败: %v", err) - } else { - log.Printf("caiyun 定时拉取完成") - } - } - }() - - // 后台定时:每小时拉取CMA(全站,共用一份数据,缺失填0) - go func() { - for { - now := time.Now() - next := now.Truncate(time.Hour).Add(time.Hour) - time.Sleep(time.Until(next)) - if err := forecast.RunCMAFetch(context.Background()); err != nil { - log.Printf("cma 定时拉取失败: %v", err) - } else { - log.Printf("cma 定时拉取完成") - } - } - }() - - // 后台定时:每小时整点+5分 执行融合任务(全站),发布 imdroid_mix - go func() { - for { - now := time.Now() - next := now.Truncate(time.Hour).Add(time.Hour).Add(5 * time.Minute) - time.Sleep(time.Until(next)) - issued := next.Truncate(time.Hour) - if err := fusion.RunForIssued(context.Background(), issued); err != nil { - log.Printf("fusion 定时执行失败: %v", err) - } else { - log.Printf("fusion 定时执行完成 issued=%s", issued.Format("2006-01-02 15:04:05")) - } - } - }() + // 说明:融合任务已迁移至独立服务(service-fusion)。 for { n, addr, err := conn.ReadFrom(buffer)