feat: 接入 CoinGlass 清算热力图隔离链路
Some checks failed
ci / build + vet + guard + race (push) Has been cancelled

This commit is contained in:
dela
2026-05-25 16:57:22 +08:00
parent e5fb54cb72
commit 1b61541ff5
32 changed files with 2550 additions and 70 deletions

View File

@@ -3,6 +3,8 @@ package app
import (
"context"
"log/slog"
"math/rand"
"time"
"github.com/robfig/cron/v3"
@@ -12,8 +14,12 @@ import (
type Scheduler struct {
cron *cron.Cron
collector *worker.Collector
coinglass *worker.CoinglassCollector
log *slog.Logger
rootCtx context.Context
coinglassLiqHeatMapCron string
coinglassStartupJitter time.Duration
}
func NewScheduler(c *worker.Collector, log *slog.Logger) *Scheduler {
@@ -24,19 +30,37 @@ func NewScheduler(c *worker.Collector, log *slog.Logger) *Scheduler {
}
}
func (s *Scheduler) WithCoinglass(c *worker.CoinglassCollector, liqHeatMapCron string, startupJitter time.Duration) *Scheduler {
s.coinglass = c
s.coinglassLiqHeatMapCron = liqHeatMapCron
s.coinglassStartupJitter = startupJitter
return s
}
func (s *Scheduler) Start(ctx context.Context) {
s.rootCtx = ctx
s.add("*/15 * * * *", "klines_15m", func(c context.Context) { _ = s.collector.CollectKlines(c, "15m") })
s.add("0 * * * *", "klines_1h", func(c context.Context) { _ = s.collector.CollectKlines(c, "1h") })
s.add("0 */4 * * *", "klines_4h", func(c context.Context) { _ = s.collector.CollectKlines(c, "4h") })
s.add("5 0 * * *", "klines_1d", func(c context.Context) { _ = s.collector.CollectKlines(c, "1d") })
s.add("10 0 * * 1", "klines_1w", func(c context.Context) { _ = s.collector.CollectKlines(c, "1w") })
if s.collector != nil {
s.add("*/15 * * * *", "klines_15m", func(c context.Context) { _ = s.collector.CollectKlines(c, "15m") })
s.add("0 * * * *", "klines_1h", func(c context.Context) { _ = s.collector.CollectKlines(c, "1h") })
s.add("0 */4 * * *", "klines_4h", func(c context.Context) { _ = s.collector.CollectKlines(c, "4h") })
s.add("5 0 * * *", "klines_1d", func(c context.Context) { _ = s.collector.CollectKlines(c, "1d") })
s.add("10 0 * * 1", "klines_1w", func(c context.Context) { _ = s.collector.CollectKlines(c, "1w") })
s.add("*/15 * * * *", "funding", func(c context.Context) { _ = s.collector.CollectFunding(c) })
s.add("*/15 * * * *", "oi", func(c context.Context) { _ = s.collector.CollectOpenInterest(c) })
s.add("*/15 * * * *", "ls", func(c context.Context) { _ = s.collector.CollectLongShortRatio(c) })
s.add("*/15 * * * *", "taker", func(c context.Context) { _ = s.collector.CollectTakerVolume(c) })
s.add("*/15 * * * *", "funding", func(c context.Context) { _ = s.collector.CollectFunding(c) })
s.add("*/15 * * * *", "oi", func(c context.Context) { _ = s.collector.CollectOpenInterest(c) })
s.add("*/15 * * * *", "ls", func(c context.Context) { _ = s.collector.CollectLongShortRatio(c) })
s.add("*/15 * * * *", "taker", func(c context.Context) { _ = s.collector.CollectTakerVolume(c) })
}
if s.coinglass != nil {
spec := s.coinglassLiqHeatMapCron
if spec == "" {
spec = "*/5 * * * *"
}
s.add(spec, "coinglass_liq_heatmap", func(c context.Context) { _ = s.coinglass.CollectLiqHeatMap(c) })
s.startCoinglassWarmup(ctx)
}
s.cron.Start()
s.log.Info("scheduler_started")
@@ -52,6 +76,21 @@ func (s *Scheduler) add(spec, name string, fn func(context.Context)) {
}
}
func (s *Scheduler) startCoinglassWarmup(ctx context.Context) {
go func() {
if s.coinglassStartupJitter > 0 {
jitter := time.Duration(rand.Int63n(int64(s.coinglassStartupJitter)))
select {
case <-ctx.Done():
return
case <-time.After(jitter):
}
}
s.log.Info("cron_tick", "job", "coinglass_liq_heatmap_warmup")
_ = s.coinglass.CollectLiqHeatMap(ctx)
}()
}
func (s *Scheduler) Stop() {
s.log.Info("scheduler_stopping")
<-s.cron.Stop().Done()