/
gocron.go
217 lines (178 loc) · 4.34 KB
/
gocron.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package scheduler
import (
"github.com/go-co-op/gocron"
"time"
)
// init registers the "gocron" provider, which is an awesome scheduler.
// Please check out the github repo: (github.com/go-co-op/gocron).
func init() {
err := Register(GoCron, fn)
if err != nil {
panic(err)
}
}
// fn creates a new Provider.
func fn(option interface{}) (Provider, error) {
// TODO set options for timezone.
cron := &cron{}
cron.scheduler = gocron.NewScheduler(time.UTC)
return cron, nil
}
type cron struct {
scheduler *gocron.Scheduler
currentName string
names []string
}
type job struct {
job *gocron.Job
name string
}
//------------------------------
// Provider interface
//------------------------------
// Every satisfy the Provider interface.
func (c *cron) Start() {
c.scheduler.StartAsync()
}
// Every satisfy the Provider interface.
func (c *cron) Stop() {
c.scheduler.Stop()
}
// Every satisfy the Provider interface.
func (c *cron) Status() string {
if c.scheduler.IsRunning() {
return StatusRunning
}
return StatusNotRunning
}
// Every satisfy the Provider interface.
func (c *cron) Jobs() []ProviderJobDetail {
scheduledJobs := c.scheduler.Jobs()
jobs := make([]ProviderJobDetail, len(scheduledJobs))
for i, j := range scheduledJobs {
jobs[i] = &job{job: j, name: c.names[i]}
}
return jobs
}
//------------------------------
// ProviderJob interface
//------------------------------
// Every satisfy the ProviderJob interface.
func (c *cron) Every(interval interface{}) ProviderJob {
c.scheduler.Every(interval)
c.currentName = ""
return c
}
// Second satisfy the ProviderJob interface.
func (c *cron) Second() ProviderJob {
c.scheduler.Second()
return c
}
// Minute satisfy the ProviderJob interface.
func (c *cron) Minute() ProviderJob {
c.scheduler.Minute()
return c
}
// Day satisfy the ProviderJob interface.
func (c *cron) Day() ProviderJob {
c.scheduler.Day()
return c
}
// Monday satisfy the ProviderJob interface.
func (c *cron) Monday() ProviderJob {
c.scheduler.Monday()
return c
}
// Tuesday satisfy the ProviderJob interface.
func (c *cron) Tuesday() ProviderJob {
c.scheduler.Tuesday()
return c
}
// Wednesday satisfy the ProviderJob interface.
func (c *cron) Wednesday() ProviderJob {
c.scheduler.Wednesday()
return c
}
// Thursday satisfy the ProviderJob interface.
func (c *cron) Thursday() ProviderJob {
c.scheduler.Thursday()
return c
}
// Friday satisfy the ProviderJob interface.
func (c *cron) Friday() ProviderJob {
c.scheduler.Friday()
return c
}
// Saturday satisfy the ProviderJob interface.
func (c *cron) Saturday() ProviderJob {
c.scheduler.Saturday()
return c
}
// Sunday satisfy the ProviderJob interface.
func (c *cron) Sunday() ProviderJob {
c.scheduler.Sunday()
return c
}
// Week satisfy the ProviderJob interface.
func (c *cron) Week() ProviderJob {
c.scheduler.Week()
return c
}
// At satisfy the ProviderJob interface.
func (c *cron) At(at string) ProviderJob {
c.scheduler.At(at)
return c
}
// Name satisfy the ProviderJob interface.
func (c *cron) Name(name string) ProviderJob {
c.currentName = name
return c
}
// Month satisfy the ProviderJob interface.
func (c *cron) Month(dayOfMonth ...int) ProviderJob {
day := 1
if len(dayOfMonth) == 1 {
day = dayOfMonth[0]
}
c.scheduler.Month(day)
return c
}
// Tag satisfy the ProviderJob interface.
func (c *cron) Tag(t ...string) ProviderJob {
c.scheduler.Tag(t...)
return c
}
// Singleton satisfy the ProviderJob interface.
func (c *cron) Singleton() ProviderJob {
c.scheduler.SingletonMode()
return c
}
// Do satisfy the ProviderJob interface.
func (c *cron) Do(jobFun interface{}, params ...interface{}) error {
c.names = append(c.names, c.currentName)
_, err := c.scheduler.Do(jobFun, params...)
return err
}
//------------------------------
// ProviderJobDetail interface
//------------------------------
// Name satisfy the ProviderJobDetail interface.
func (j *job) Name() string {
return j.name
}
// Counter satisfy the ProviderJobDetail interface.
func (j *job) Counter() int {
return j.job.RunCount()
}
// Tags satisfy the ProviderJobDetail interface.
func (j *job) Tags() []string {
return j.job.Tags()
}
// LastRun satisfy the ProviderJobDetail interface.
func (j *job) LastRun() time.Time {
return j.job.LastRun()
}
// NextRun satisfy the ProviderJobDetail interface.
func (j *job) NextRun() time.Time {
return j.job.NextRun()
}