-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
197 lines (172 loc) · 3.49 KB
/
entry.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
// Package gotask Copyright (c) 2018, dmc ([email protected]),
//
// Authors: dmc,
//
// Distribution:.
package gotask
import (
"errors"
"sort"
"sync"
"time"
)
var (
// ErrTaskNotFound cannot found task by id
ErrTaskNotFound = errors.New("task does not exist")
// ErrTaskTypeInValid only polling task can modify interval
ErrTaskTypeInValid = errors.New("this type does not support modifying the execution interval")
)
// Tasks 任务列表
type Tasks []*Task
func (s Tasks) Len() int { return len(s) }
func (s Tasks) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Tasks) Less(i, j int) bool {
return s[i].ExecuteTime().Before(s[j].ExecuteTime())
}
type taskList struct {
// all tasks
taskers Tasks
}
type intervalChange struct {
task *Task
interval time.Duration
}
var (
tasks *taskList
editC = make(chan interface{})
removeC = make(chan string)
wg = &sync.WaitGroup{}
)
func init() {
tasks = &taskList{}
go doAllTask()
}
// AddToTaskList add the task to the execution list
func AddToTaskList(ts ...*Task) {
for _, t := range ts {
if t == nil {
continue
}
wg.Add(1)
editC <- t
}
}
func (tl *taskList) addToTaskList(t *Task) {
tl.taskers = append(tl.taskers, t)
wg.Done()
}
// Remove remove corresponding tasks through the id of task
func Remove(id string) {
removeC <- id
}
func (tl *taskList) remove(id string) {
for k, v := range tl.taskers {
if v.id == id {
tl.taskers = append(tl.taskers[:k], tl.taskers[k+1:]...)
}
}
}
// ChangeInterval changes the interval between the tasks specified by the ID,
// Apply only to polling tasks.
func ChangeInterval(id string, interval time.Duration) error {
tsk, err := tasks.getConcurrent(id)
if err != nil {
return err
}
if tsk.taskType != polling {
return ErrTaskTypeInValid
}
editC <- &intervalChange{
task: tsk,
interval: interval,
}
return nil
}
// Pause the running task, it only returns error
// when can not found task
func Pause(id string) error {
tsk, err := tasks.getConcurrent(id)
if err != nil {
return err
}
tsk.pause()
return nil
}
// Resume the paused task, it only returns error
// when can not found task
func Resume(id string) error {
tsk, err := tasks.getConcurrent(id)
if err != nil {
return err
}
tsk.resume()
return nil
}
func changeInterval(i *intervalChange) {
i.task.setInterval(i.interval)
}
func (tl *taskList) get(id string) *Task {
for _, v := range tl.taskers {
if v.id == id {
return v
}
}
return nil
}
func (tl *taskList) getConcurrent(id string) (*Task, error) {
tsk := tasks.get(id)
if tsk == nil {
wg.Wait()
tsk = tasks.get(id)
if tsk == nil {
return nil, ErrTaskNotFound
}
}
return tsk, nil
}
func doAllTask() {
var timer *time.Timer
var now time.Time
for {
sort.Sort(tasks.taskers)
now = time.Now()
if len(tasks.taskers) == 0 {
timer = time.NewTimer(time.Hour * 100000)
} else {
sub := tasks.taskers[0].ExecuteTime().Sub(now)
if sub < 0 {
sub = 0
}
timer = time.NewTimer(sub)
}
for {
select {
case now = <-timer.C:
doNestedTask()
case edit := <-editC:
now = time.Now()
timer.Stop()
if t, ok := edit.(*Task); ok {
tasks.addToTaskList(t)
} else if ic, ok := edit.(*intervalChange); ok {
changeInterval(ic)
}
case id := <-removeC:
tasks.remove(id)
}
break
}
}
}
func doNestedTask() {
for _, v := range tasks.taskers {
if v.ExecuteTime().Before(time.Now()) {
if !v.isPaused() {
go v.do()
}
v.refreshExecuteTime()
} else {
return
}
}
}