-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
91 lines (70 loc) · 1.58 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
package chrono
import (
"container/heap"
"time"
)
type taskQueue []*Task
func newTaskQueue() *taskQueue {
t := make(taskQueue, 0, 100)
return &t
}
func (q *taskQueue) PushTask(t *Task) {
heap.Push(q, t)
}
func (q *taskQueue) PopTask() (_ *Task) {
t, _ := heap.Pop(q).(*Task)
return t
}
func (q *taskQueue) PeekTask() (_ *Task) {
return (*q)[0]
}
func (q taskQueue) HasExpiredTasks(now time.Time) bool {
return len(q) != 0 && !now.Before(q[0].Deadline)
}
func (q taskQueue) HasTasks() bool {
return len(q) != 0
}
func (q *taskQueue) RemoveTask(t *Task) {
if t.IsPending() {
heap.Remove(q, t.indexInQueue)
}
}
func (q taskQueue) Len() int { return len(q) }
func (q taskQueue) Less(i, j int) bool {
return q[i].Deadline.Before(q[j].Deadline)
}
func (q taskQueue) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
q[i].indexInQueue, q[j].indexInQueue = i, j
}
func (q *taskQueue) Push(v interface{}) {
task := v.(*Task)
task.indexInQueue = len(*q)
*q = append(*q, task)
}
func (q *taskQueue) Pop() interface{} {
tasks := *q
n := len(tasks)
oldestTask := tasks[n-1]
oldestTask.indexInQueue = -1
*q = tasks[0 : n-1]
return oldestTask
}
type Task struct {
Deadline time.Time
Action func(t *Task, now time.Time) (followingTask *Task)
indexInQueue int
}
func newTask(deadline time.Time, run func(t *Task, now time.Time) *Task) *Task {
return &Task{
Deadline: deadline,
Action: run,
indexInQueue: -1,
}
}
func (t *Task) Run(now time.Time) (followingTask *Task) {
return t.Action(t, now)
}
func (t Task) IsPending() bool {
return t.indexInQueue != -1
}