-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
194 lines (161 loc) · 3.68 KB
/
scheduler.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
package periodic
import (
"sync"
"time"
)
type TaskStatus int
const (
Unknown TaskStatus = iota
Running
Stopped
)
type scheduledTask struct {
task Task
interval time.Duration
status TaskStatus
stopSig chan struct{}
}
//Scheduler struct has task informations
type Scheduler struct {
rwMutex sync.RWMutex
taskMap map[string]*scheduledTask
}
func NewScheduler() *Scheduler {
return &Scheduler{
taskMap: make(map[string]*scheduledTask),
}
}
//RegisterTask register a task that runs periodically
func (s *Scheduler) RegisterTask(taskName string, interval time.Duration, task Task) error {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()
if _, ok := s.taskMap[taskName]; ok {
return ErrDuplicated
}
s.taskMap[taskName] = &scheduledTask{
task: task,
interval: interval,
status: Stopped,
}
return nil
}
//GetTaskStatus returns status of specific task
func (s *Scheduler) GetTaskStatus(taskName string) (TaskStatus, error) {
s.rwMutex.RLock()
defer s.rwMutex.RUnlock()
if task, ok := s.taskMap[taskName]; ok {
return task.status, nil
}
return Unknown, ErrNotRegistered
}
//GetNumOfTasks returns the number of registered tasks in scheduler
func (s *Scheduler) GetNumOfTasks(status ...TaskStatus) int {
s.rwMutex.RLock()
defer s.rwMutex.RUnlock()
if len(status) == 0 {
return len(s.taskMap)
}
count := 0
for _, st := range s.taskMap {
if st.status == status[0] {
count++
}
}
return count
}
//Run excutes tasks that status are "Stopped"
//If there are no parameters the scheduler runs all tasks.
//On the other hand, If there are parameters the scehduler runs specific tasks
func (s *Scheduler) Run(taskNames ...string) {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()
if len(taskNames) == 0 {
for _, t := range s.taskMap {
schedule(t)
}
return
}
for _, taskName := range taskNames {
if t, ok := s.taskMap[taskName]; ok {
schedule(t)
}
}
}
func schedule(st *scheduledTask) {
if st.status == Running {
return
}
taskFunc := st.task.GetTaskFunc()
params := st.task.GetTaskFuncParams()
f := func() {
taskFunc.Call(params)
}
stopSigChan := make(chan struct{})
go func(do func(), interval time.Duration) {
timer := time.NewTimer(interval)
calculatedInterval := interval
for {
//TODO: In the case of immediate execution, it should also be handled.
timer.Reset(calculatedInterval)
select {
case <-timer.C:
case <-stopSigChan:
releaseTimer(timer)
return
}
start := time.Now()
do()
end := time.Now()
elapsed := end.Sub(start)
calculatedInterval = interval - elapsed
if calculatedInterval < 0 {
calculatedInterval = 0
}
}
}(f, st.interval)
st.stopSig = stopSigChan
st.status = Running
}
func releaseTimer(timer *time.Timer) {
if !timer.Stop() {
<-timer.C
}
}
//Stop stops tasks that status are "Running"
//If there are no parameters the scheduler stops all tasks.
//On the other hand, If there are parameters the scehduler stops specific tasks
func (s *Scheduler) Stop(taskNames ...string) {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()
if len(taskNames) == 0 {
for _, t := range s.taskMap {
stop(t)
}
return
}
for _, taskName := range taskNames {
if t, ok := s.taskMap[taskName]; ok {
stop(t)
}
}
}
func stop(st *scheduledTask) {
if st.status != Running {
return
}
st.status = Stopped
close(st.stopSig)
}
// Reset resets a interval time of scheduled task.
// if the task is running, it will stop the task and then cahnge a interval
func (s *Scheduler) Reset(taskName string, interval time.Duration) error {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()
task, ok := s.taskMap[taskName]
if !ok {
return ErrNotRegistered
}
stop(task)
task.interval = interval
return nil
}