-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnooverlapping.go
50 lines (43 loc) · 925 Bytes
/
nooverlapping.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
package nooverlapping
import (
"context"
"github.com/flc1125/go-cron/v4"
)
type options struct {
logger cron.Logger
}
type Option func(*options)
func WithLogger(logger cron.Logger) Option {
return func(o *options) {
o.logger = logger
}
}
func newOptions(opts ...Option) options {
opt := options{
logger: cron.DefaultLogger,
}
for _, o := range opts {
o(&opt)
}
return opt
}
// New returns a without Overlapping middleware.
// if the job is running, skip the job.
// Based on the old version of SkipIfStillRunning
func New(opts ...Option) cron.Middleware {
o := newOptions(opts...)
return func(job cron.Job) cron.Job {
ch := make(chan struct{}, 1)
ch <- struct{}{}
return cron.JobFunc(func(ctx context.Context) error {
select {
case v := <-ch:
defer func() { ch <- v }()
return job.Run(ctx)
default:
o.logger.Info("job is still running, skip")
return nil
}
})
}
}