refactor: 拆分 fusion 服务
This commit is contained in:
parent
b0086a984f
commit
0b5b26d5b0
50
cmd/service-fusion/main.go
Normal file
50
cmd/service-fusion/main.go
Normal 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")
|
||||
}
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user