-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
154 lines (132 loc) · 2.83 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
package gron
import (
"fmt"
"math/rand"
"reflect"
"strconv"
"strings"
"time"
)
// TaskName is the name of the task
type TaskName string
type timeUnit int
const shifting = 50
const (
seconds timeUnit = iota
minutes
hours
days
weekday
)
type status int
const (
active status = iota
inactive
)
// Task is a periodic task
type Task struct {
Name TaskName
interval uint
unit timeUnit
at string
weekday time.Weekday
fn interface{} // TODO: support args channel
c chan struct{}
status status
errMsg string
latestRun time.Time
}
func newTask() Task {
return Task{}
}
func (t *Task) run() {
defer func() {
t.status = inactive
publishDisabledEvent(*t)
}()
t.status = active
publishEnabledEvent(*t)
LOOP:
for {
r := rand.Intn(shifting)
timer := time.NewTimer(willRunAfter(*t) + +time.Duration(r)*time.Millisecond)
select {
case <-t.c:
timer.Stop()
break LOOP
case <-timer.C:
t.latestRun = time.Now()
stepRun(t)
}
}
}
func willRunAfter(t Task) time.Duration {
interval := time.Duration(t.interval)
unit := t.unit
now := time.Now()
y, m, d := now.Date()
rt := time.Date(y, m, d, 0, 0, 0, now.Nanosecond(), time.Local)
// at specific time
ts := make([]string, 3)
if len(t.at) > 0 {
ts = strings.Split(t.at, ":")
}
if v, err := strconv.Atoi(ts[0]); err == nil {
rt = rt.Add(time.Duration(v) * time.Hour)
} else {
rt = rt.Add(time.Duration(now.Hour()) * time.Hour)
}
if v, err := strconv.Atoi(ts[1]); err == nil {
rt = rt.Add(time.Duration(v) * time.Minute)
} else {
rt = rt.Add(time.Duration(now.Minute()) * time.Minute)
}
if v, err := strconv.Atoi(ts[2]); err == nil {
rt = rt.Add(time.Duration(v) * time.Second)
} else {
rt = rt.Add(time.Duration(now.Second()) * time.Second)
}
// on specific day
if unit == weekday {
wd := rt.Weekday()
rt = rt.AddDate(0, 0, int(t.weekday+7-wd)%7)
// wait for a week if we have run it today yet
// or we have missed that time we specified today
if t.weekday == wd && !t.latestRun.IsZero() || rt.Before(now) {
rt = rt.AddDate(0, 0, 7)
}
} else {
if now.Before(rt) {
return rt.Sub(now)
}
// purely periodic
// run immediate if not specified
if len(t.at) == 0 && t.latestRun.IsZero() {
return time.Duration(0)
}
switch unit {
case seconds:
rt = rt.Add(time.Duration(interval * time.Second))
case minutes:
rt = rt.Add(time.Duration(interval * time.Minute))
case hours:
rt = rt.Add(time.Duration(interval * time.Hour))
case days:
rt = rt.Add(time.Duration(interval * time.Hour * 24))
}
}
return rt.Sub(now)
}
func stepRun(t *Task) {
defer func() {
if r := recover(); r != nil {
t.errMsg = fmt.Sprintf("%v", r)
publishFailedEvent(*t)
return
}
publishFinishedEvent(*t)
}()
publishRunningEvent(*t)
v := reflect.ValueOf(t.fn)
v.Call([]reflect.Value{})
}