syncInterval

This commit is contained in:
jiangyong27 2023-04-09 13:05:52 +08:00
parent 8996e27b70
commit 2c81091b94
4 changed files with 24 additions and 17 deletions

View File

@ -1,3 +1,4 @@
{ {
"token": "d6847dfbd7b79bc18709e0311fd7813b" "token": "d6847dfbd7b79bc18709e0311fd7813b",
"syncInterval": 10
} }

View File

@ -6,7 +6,8 @@ import (
) )
type Haha struct { type Haha struct {
Token string `json:"token"` Token string `json:"token"`
SyncInterval int `json:"syncInterval"`
} }
var ( var (
@ -18,8 +19,8 @@ func LoadHahaConfig() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
dadiConfig = new(Dadi) hahaConfig = new(Haha)
if err := json.Unmarshal([]byte(body), dadiConfig); err != nil { if err := json.Unmarshal([]byte(body), hahaConfig); err != nil {
panic(err) panic(err)
} }
} }

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"github.com/go-co-op/gocron" "github.com/go-co-op/gocron"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast" "github.com/spf13/cast"
"io" "io"
"net/http" "net/http"
@ -15,22 +16,25 @@ import (
) )
type SyncOrderConfig struct { type SyncOrderConfig struct {
Token string Token string
NewOrder chan *model.Order NewOrder chan *model.Order
UpdateOrder chan *model.Order UpdateOrder chan *model.Order
SyncInterval int
} }
type SyncOrder struct { type SyncOrder struct {
token string token string
newOrder chan *model.Order syncInterval int
updateOrder chan *model.Order newOrder chan *model.Order
updateOrder chan *model.Order
} }
func NewSyncOrder(cfg *SyncOrderConfig) *SyncOrder { func NewSyncOrder(cfg *SyncOrderConfig) *SyncOrder {
return &SyncOrder{ return &SyncOrder{
token: cfg.Token, token: cfg.Token,
newOrder: cfg.NewOrder, newOrder: cfg.NewOrder,
updateOrder: cfg.UpdateOrder, updateOrder: cfg.UpdateOrder,
syncInterval: goutil.If(cfg.SyncInterval == 0, 30, cfg.SyncInterval),
} }
} }
@ -38,7 +42,7 @@ func (s *SyncOrder) Sync() {
timezone, _ := time.LoadLocation("Asia/Shanghai") timezone, _ := time.LoadLocation("Asia/Shanghai")
cron := gocron.NewScheduler(timezone) cron := gocron.NewScheduler(timezone)
cron.Every(30).Seconds().Do(func() { cron.Every(s.syncInterval).Seconds().Do(func() {
s.run() s.run()
}) })

View File

@ -112,9 +112,10 @@ func (w *Worker) Run() {
newOrderChan := make(chan *model.Order, 100000) newOrderChan := make(chan *model.Order, 100000)
updateOrderChan := make(chan *model.Order, 100000) updateOrderChan := make(chan *model.Order, 100000)
hahaSyncer := haha.NewSyncOrder(&haha.SyncOrderConfig{ hahaSyncer := haha.NewSyncOrder(&haha.SyncOrderConfig{
Token: config.GetHahaConfig().Token, Token: config.GetHahaConfig().Token,
NewOrder: newOrderChan, NewOrder: newOrderChan,
UpdateOrder: updateOrderChan, UpdateOrder: updateOrderChan,
SyncInterval: config.GetHahaConfig().SyncInterval,
}) })
hahaSyncer.Sync() hahaSyncer.Sync()