-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
228 lines (207 loc) · 4.46 KB
/
task.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
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2018, dmc ([email protected]),
//
// Authors: dmc,
//
// Distribution:.
package gotask
import (
"fmt"
"sync"
"time"
)
const (
idLen = 20
dayInterval = time.Hour * 24
)
const (
polling = iota
daily
monthly
yearly
)
var (
monthlyNextFunc = func(tm time.Time) time.Time {
step := 1
PASS:
newTime := tm.AddDate(0, step, 0)
if newTime.Day() != tm.Day() {
step++
// some months may not include this day
goto PASS
}
return newTime
}
yearlyNextFunc = func(tm time.Time) time.Time {
step := 1
PASS:
newTime := tm.AddDate(step, 0, 0)
if newTime.Month() != tm.Month() {
step++
goto PASS
}
return newTime
}
)
// Task Polling tasks
type Task struct {
sync.RWMutex
id string
executeTime time.Time
nextExecuteTime func(time.Time) time.Time
paused bool
taskType int
do func()
}
// NewTask create a new polling task
func NewTask(t time.Duration, do func()) (*Task, error) {
if t < time.Millisecond {
return nil, fmt.Errorf("the execution interval is too short")
}
idStr := RandStringBytesMaskImprSrc(idLen)
return &Task{
id: idStr,
do: do,
nextExecuteTime: func(tm time.Time) time.Time {
return tm.Add(t)
},
executeTime: time.Now().Add(t),
taskType: polling,
}, nil
}
// NewDailyTask create a new daily task
func NewDailyTask(tm string, do func()) (*Task, error) {
idStr := RandStringBytesMaskImprSrc(idLen)
pt := newTimeParser(dailyParseType)
begin, err := pt.Parse(tm)
if err != nil {
return nil, err
}
if begin.Before(time.Now()) {
begin = begin.Add(dayInterval)
}
return &Task{
id: idStr,
do: do,
executeTime: begin,
nextExecuteTime: func(tm time.Time) time.Time {
return tm.Add(dayInterval)
},
taskType: daily,
}, nil
}
// NewDailyTasks create new daily tasks
func NewDailyTasks(tms []string, do func()) ([]*Task, error) {
var ts []*Task
for _, tm := range tms {
dt, err := NewDailyTask(tm, do)
if err != nil {
return nil, err
}
ts = append(ts, dt)
}
return ts, nil
}
// NewMonthlyTask initialize a function that executes each month
func NewMonthlyTask(tm string, do func()) (*Task, error) {
idStr := RandStringBytesMaskImprSrc(idLen)
pt := newTimeParser(monthlyParseType)
begin, err := pt.Parse(tm)
if err != nil {
return nil, err
}
if begin.Before(time.Now()) {
begin = monthlyNextFunc(begin)
}
return &Task{
id: idStr,
do: do,
executeTime: begin,
nextExecuteTime: monthlyNextFunc,
taskType: monthly,
}, nil
}
// NewMonthlyTasks initialize a function that executes each month
func NewMonthlyTasks(tms []string, do func()) ([]*Task, error) {
var ts []*Task
for _, tm := range tms {
mt, err := NewMonthlyTask(tm, do)
if err != nil {
return nil, err
}
ts = append(ts, mt)
}
return ts, nil
}
// NewYearlyTask initialize a function that executes each month
func NewYearlyTask(tm string, do func()) (*Task, error) {
idStr := RandStringBytesMaskImprSrc(idLen)
pt := newTimeParser(yearlyParseType)
begin, err := pt.Parse(tm)
if err != nil {
return nil, err
}
if begin.Before(time.Now()) {
begin = yearlyNextFunc(begin)
}
return &Task{
id: idStr,
do: do,
executeTime: begin,
nextExecuteTime: yearlyNextFunc,
taskType: yearly,
}, nil
}
// NewYearlyTasks initialize a function that executes each month
func NewYearlyTasks(tms []string, do func()) ([]*Task, error) {
var ts []*Task
for _, tm := range tms {
mt, err := NewYearlyTask(tm, do)
if err != nil {
return nil, err
}
ts = append(ts, mt)
}
return ts, nil
}
// ExecuteTime gets the next execution time
func (t *Task) ExecuteTime() time.Time {
t.RLock()
defer t.RUnlock()
return t.executeTime
}
// SetInterval modify execution interval only for polling task
func (t *Task) setInterval(td time.Duration) {
t.Lock()
defer t.Unlock()
t.nextExecuteTime = func(tm time.Time) time.Time {
return tm.Add(td)
}
t.executeTime = time.Now().Add(td)
}
// refreshExecuteTime refresh execution interval
func (t *Task) refreshExecuteTime() {
t.Lock()
defer t.Unlock()
t.executeTime = t.nextExecuteTime(t.executeTime)
}
// ID return taskID
func (t *Task) ID() string {
return t.id
}
// pause the runnning task
func (t *Task) pause() {
t.Lock()
defer t.Unlock()
t.paused = true
}
func (t *Task) isPaused() bool {
t.RLock()
defer t.RUnlock()
return t.paused
}
// resume the paused task
func (t *Task) resume() {
t.Lock()
defer t.Unlock()
t.paused = false
}