refactor: 拆分 fusion 服务

This commit is contained in:
yarnom 2025-10-14 11:14:26 +08:00
parent b0086a984f
commit 0b5b26d5b0
2 changed files with 53 additions and 62 deletions

View File

@ -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")
}

View File

@ -15,8 +15,6 @@ import (
"time" "time"
"unicode/utf8" "unicode/utf8"
"weatherstation/internal/config" "weatherstation/internal/config"
"weatherstation/internal/forecast"
"weatherstation/internal/fusion"
"weatherstation/internal/tools" "weatherstation/internal/tools"
"weatherstation/model" "weatherstation/model"
) )
@ -146,67 +144,10 @@ func StartUDPServer() error {
} }
}() }()
// 后台定时每小时拉取open-meteo全站 // 说明:原有的 open-meteo/彩云/CMA 定时抓取已移除,避免与独立的 service-forecast 重复调度。
go func() { // 若需要启用预报抓取,请运行 `cmd/service-forecast` 服务。
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 定时拉取完成")
}
}
}()
// 后台定时:每小时拉取彩云(全站) // 说明融合任务已迁移至独立服务service-fusion
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"))
}
}
}()
for { for {
n, addr, err := conn.ReadFrom(buffer) n, addr, err := conn.ReadFrom(buffer)