/
scheduler.go
106 lines (88 loc) · 2.51 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cron
import (
"context"
"sync"
"time"
"github.com/go-co-op/gocron/v2"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
)
type scheduler struct {
gocron.Scheduler
jobSyncer Syncer
syncDuration time.Duration
cancel context.CancelFunc
}
type disablePingUniversalClient struct {
redis.UniversalClient
}
func (disablePingUniversalClient) Ping(ctx context.Context) *redis.StatusCmd {
return redis.NewStatusCmd(ctx)
}
func NewSchedulerWithRedisLocker(config Config, jobSyncer Syncer, client redis.UniversalClient, logger *zap.Logger, opts ...gocron.SchedulerOption) (gocron.Scheduler, error) {
o := append(opts, gocron.WithDistributedLocker(NewRedisLocker(*config.Locker, client)))
return NewScheduler(config, jobSyncer, logger, o...)
}
func NewScheduler(config Config, jobSyncer Syncer, logger *zap.Logger, opts ...gocron.SchedulerOption) (gocron.Scheduler, error) {
o := []gocron.SchedulerOption{
gocron.WithLocation(time.UTC),
gocron.WithStopTimeout(config.StopTimeout),
gocron.WithLimitConcurrentJobs(config.Limit, gocron.LimitModeReschedule),
gocron.WithLogger(Logger{l: logger}),
gocron.WithGlobalJobOptions(
gocron.WithSingletonMode(gocron.LimitModeReschedule),
gocron.WithEventListeners(
gocron.BeforeJobRuns(func(_ uuid.UUID, name string) {
logger.Named("job").Info("job start running", zap.String("name", name))
}),
gocron.AfterJobRuns(func(_ uuid.UUID, name string) {
logger.Named("job").Info("job stop running", zap.String("name", name))
}),
gocron.AfterJobRunsWithError(func(_ uuid.UUID, name string, err error) {
logger.Named("job").Error("job stop running with error", zap.String("name", name), zap.Error(err))
}),
),
),
}
s, err := gocron.NewScheduler(append(o, opts...)...)
if err != nil {
return nil, err
}
return &scheduler{
Scheduler: s,
jobSyncer: jobSyncer,
syncDuration: config.Sync,
}, nil
}
func (s *scheduler) Start() {
ctx, cancel := context.WithCancel(context.Background())
s.cancel = sync.OnceFunc(cancel)
s.Scheduler.Start()
go s.run(ctx)
}
func (s *scheduler) StopJobs() error {
defer s.cancel()
return s.Scheduler.StopJobs()
}
func (s *scheduler) Shutdown() error {
defer func() {
if s.cancel != nil {
s.cancel()
s.cancel = nil
}
}()
return s.Scheduler.Shutdown()
}
func (s *scheduler) run(ctx context.Context) {
t := time.NewTicker(s.syncDuration)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
s.jobSyncer.Sync(ctx, s)
}
}
}