-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (140 loc) · 3.28 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"fmt"
"sync"
"time"
"github.com/ahmedalhulaibi/cronalt/job"
"github.com/ahmedalhulaibi/loggy"
"github.com/gorhill/cronexpr"
"go.uber.org/zap"
"github.com/ahmedalhulaibi/cronalt"
)
func main() {
logger, _ := zap.NewProduction(zap.AddCallerSkip(2))
loggylog := localLogger{loggy.New(logger.Sugar())}
defer loggylog.Sync()
ctx := context.Background()
js := job.NewStore()
scheduler, _ := cronalt.NewScheduler(10, cronalt.WithJobStore(js), cronalt.WithLogger(loggylog))
dscheduler := &DynamicJobScheduler{
ctx: ctx,
state: stopped,
scheduler: scheduler,
jobStore: js,
}
dscheduler.Schedule(cronexpr.MustParse("0 * * * * * *"), foo{})
dscheduler.Schedule(cronalt.Every(5*time.Second), panicker{})
go func() {
var flip bool
ticker := time.NewTicker(time.Second * 10)
flipjob := echoJob{}
for range ticker.C {
if flip {
dscheduler.Schedule(cronalt.Every(time.Second), flipjob)
} else {
dscheduler.Remove(flipjob)
}
flip = !flip
}
}()
dscheduler.Wait(ctx)
}
type jobStore interface {
Remove(name string) error
}
type schedulerState byte
const (
stopped schedulerState = iota
started
)
type DynamicJobScheduler struct {
ctx context.Context
cancelFunc context.CancelFunc
scheduler *cronalt.Scheduler
jobStore jobStore
state schedulerState
locker sync.Mutex
}
func (d *DynamicJobScheduler) Schedule(jt job.Timer, j job.Job) error {
d.locker.Lock()
defer d.locker.Unlock()
err := d.scheduler.Schedule(jt, j)
if err != nil {
return err
}
d.restart()
return nil
}
func (d *DynamicJobScheduler) Remove(j job.Job) error {
d.locker.Lock()
defer d.locker.Unlock()
err := d.jobStore.Remove(j.Name())
if err != nil {
return err
}
d.restart()
return nil
}
func (d *DynamicJobScheduler) restart() {
if d.state == started {
d.cancelFunc()
d.state = stopped
}
ctx, cancelFunc := context.WithCancel(d.ctx)
d.cancelFunc = cancelFunc
go d.scheduler.Start(ctx)
d.state = started
}
func (d *DynamicJobScheduler) Wait(ctx context.Context) {
<-ctx.Done()
}
type localLogger struct {
loggy.Logger
}
func (l localLogger) getArgs(args ...cronalt.KeyVal) []loggy.KeyVal {
argsL := make([]loggy.KeyVal, 0, len(args))
for _, arg := range args {
argsL = append(argsL, loggy.KeyVal(arg))
}
return argsL
}
func (l localLogger) Info(ctx context.Context, msg string, args ...cronalt.KeyVal) {
l.Logger.Infow(ctx, msg, l.getArgs(args...)...)
}
func (l localLogger) Error(ctx context.Context, msg string, args ...cronalt.KeyVal) {
l.Logger.Errorw(ctx, msg, l.getArgs(args...)...)
}
func (l localLogger) Warn(ctx context.Context, msg string, args ...cronalt.KeyVal) {
l.Logger.Warnw(ctx, msg, l.getArgs(args...)...)
}
func echo(_ context.Context) error {
fmt.Println("hello")
return nil
}
type echoJob struct{}
func (echoJob) Name() string {
return "echoJob"
}
func (echoJob) Runner() job.JobFn {
return echo
}
type foo struct{}
func (foo) Name() string {
return "foo"
}
func (foo) Runner() job.JobFn {
return func(ctx context.Context) error {
fmt.Println("foo")
return nil
}
}
type panicker struct{}
func (panicker) Name() string {
return "panicker"
}
func (panicker) Runner() job.JobFn {
return func(ctx context.Context) error {
panic(fmt.Errorf("panicker panicking"))
}
}