From 2c81091b94fa0b399281a02d45b59d7455496f3e Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Sun, 9 Apr 2023 13:05:52 +0800 Subject: [PATCH] syncInterval --- conf/haha.json | 3 ++- config/haha.go | 7 ++++--- worker/haha/haha_sync_order.go | 24 ++++++++++++++---------- worker/worker.go | 7 ++++--- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/conf/haha.json b/conf/haha.json index 969c017..8389f7b 100644 --- a/conf/haha.json +++ b/conf/haha.json @@ -1,3 +1,4 @@ { - "token": "d6847dfbd7b79bc18709e0311fd7813b" + "token": "d6847dfbd7b79bc18709e0311fd7813b", + "syncInterval": 10 } \ No newline at end of file diff --git a/config/haha.go b/config/haha.go index c383346..2120c4d 100644 --- a/config/haha.go +++ b/config/haha.go @@ -6,7 +6,8 @@ import ( ) type Haha struct { - Token string `json:"token"` + Token string `json:"token"` + SyncInterval int `json:"syncInterval"` } var ( @@ -18,8 +19,8 @@ func LoadHahaConfig() { if err != nil { panic(err) } - dadiConfig = new(Dadi) - if err := json.Unmarshal([]byte(body), dadiConfig); err != nil { + hahaConfig = new(Haha) + if err := json.Unmarshal([]byte(body), hahaConfig); err != nil { panic(err) } } diff --git a/worker/haha/haha_sync_order.go b/worker/haha/haha_sync_order.go index 1eabbeb..0b2f42e 100644 --- a/worker/haha/haha_sync_order.go +++ b/worker/haha/haha_sync_order.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/go-co-op/gocron" log "github.com/sirupsen/logrus" + "github.com/smbrave/goutil" "github.com/spf13/cast" "io" "net/http" @@ -15,22 +16,25 @@ import ( ) type SyncOrderConfig struct { - Token string - NewOrder chan *model.Order - UpdateOrder chan *model.Order + Token string + NewOrder chan *model.Order + UpdateOrder chan *model.Order + SyncInterval int } type SyncOrder struct { - token string - newOrder chan *model.Order - updateOrder chan *model.Order + token string + syncInterval int + newOrder chan *model.Order + updateOrder chan *model.Order } func NewSyncOrder(cfg *SyncOrderConfig) *SyncOrder { return &SyncOrder{ - token: cfg.Token, - newOrder: cfg.NewOrder, - updateOrder: cfg.UpdateOrder, + token: cfg.Token, + newOrder: cfg.NewOrder, + 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") cron := gocron.NewScheduler(timezone) - cron.Every(30).Seconds().Do(func() { + cron.Every(s.syncInterval).Seconds().Do(func() { s.run() }) diff --git a/worker/worker.go b/worker/worker.go index 2788f5e..76f55bd 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -112,9 +112,10 @@ func (w *Worker) Run() { newOrderChan := make(chan *model.Order, 100000) updateOrderChan := make(chan *model.Order, 100000) hahaSyncer := haha.NewSyncOrder(&haha.SyncOrderConfig{ - Token: config.GetHahaConfig().Token, - NewOrder: newOrderChan, - UpdateOrder: updateOrderChan, + Token: config.GetHahaConfig().Token, + NewOrder: newOrderChan, + UpdateOrder: updateOrderChan, + SyncInterval: config.GetHahaConfig().SyncInterval, }) hahaSyncer.Sync()