/
cron.go
148 lines (129 loc) · 2.95 KB
/
cron.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
package ocron
import (
"sync/atomic"
"time"
"github.com/robfig/cron/v3"
"github.com/xqk/ox/pkg/olog"
"github.com/xqk/ox/pkg/util/ostring"
)
var (
// Every ...
Every = cron.Every
// NewParser ...
NewParser = cron.NewParser
// NewChain ...
NewChain = cron.NewChain
// WithSeconds ...
WithSeconds = cron.WithSeconds
// WithParser ...
WithParser = cron.WithParser
// WithLocation ...
WithLocation = cron.WithLocation
)
type (
// JobWrapper ...
JobWrapper = cron.JobWrapper
// EntryID ...
EntryID = cron.EntryID
// Entry ...
Entry = cron.Entry
// Schedule ...
Schedule = cron.Schedule
// Parser ...
Parser = cron.Parser
// Option ...
Option = cron.Option
// Job ...
Job = cron.Job
//NamedJob ..
NamedJob interface {
Run() error
Name() string
}
)
// FuncJob ...
type FuncJob func() error
// Run ...
func (f FuncJob) Run() error { return f() }
// Name ...
func (f FuncJob) Name() string { return ostring.FunctionName(f) }
// Cron ...
type Cron struct {
*Config
*cron.Cron
entries map[string]EntryID
}
func newCron(config *Config) *Cron {
if config.logger == nil {
config.logger = olog.OxLogger
}
config.logger = config.logger.With(olog.FieldMod("worker.cron"))
cron := &Cron{
Config: config,
Cron: cron.New(
cron.WithParser(config.parser),
cron.WithChain(config.wrappers...),
cron.WithLogger(&wrappedLogger{config.logger}),
),
}
return cron
}
// Schedule ...
func (c *Cron) Schedule(schedule Schedule, job NamedJob) EntryID {
if c.ImmediatelyRun {
schedule = &immediatelyScheduler{
Schedule: schedule,
}
}
innnerJob := &wrappedJob{
NamedJob: job,
logger: c.logger,
distributedTask: c.DistributedTask,
waitLockTime: c.WaitLockTime,
leaseTTL: c.Config.TTL,
client: c.client,
}
// xdebug.PrintKVWithPrefix("worker", "add job", job.Name())
c.logger.Info("add job", olog.String("name", job.Name()))
return c.Cron.Schedule(schedule, innnerJob)
}
// GetEntryByName ...
func (c *Cron) GetEntryByName(name string) cron.Entry {
// todo(gorexlv): data race
return c.Entry(c.entries[name])
}
// AddJob ...
func (c *Cron) AddJob(spec string, cmd NamedJob) (EntryID, error) {
schedule, err := c.parser.Parse(spec)
if err != nil {
return 0, err
}
return c.Schedule(schedule, cmd), nil
}
// AddFunc ...
func (c *Cron) AddFunc(spec string, cmd func() error) (EntryID, error) {
return c.AddJob(spec, FuncJob(cmd))
}
// Run ...
func (c *Cron) Run() error {
// xdebug.PrintKVWithPrefix("worker", "run worker", fmt.Sprintf("%d job scheduled", len(c.Cron.Entries())))
c.logger.Info("run worker", olog.Int("number of scheduled jobs", len(c.Cron.Entries())))
c.Cron.Run()
return nil
}
// Stop ...
func (c *Cron) Stop() error {
_ = c.Cron.Stop()
return nil
}
type immediatelyScheduler struct {
Schedule
initOnce uint32
}
// Next ...
func (is *immediatelyScheduler) Next(curr time.Time) (next time.Time) {
if atomic.CompareAndSwapUint32(&is.initOnce, 0, 1) {
return curr
}
return is.Schedule.Next(curr)
}