-
Notifications
You must be signed in to change notification settings - Fork 77
/
recentjobpacker.go
83 lines (69 loc) · 1.76 KB
/
recentjobpacker.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
package dcron
import (
"container/heap"
"sync"
"time"
)
type JobWithTime struct {
JobName string
RunningTime time.Time
}
type JobWithTimeHeap []JobWithTime
func (jobHeap *JobWithTimeHeap) Pop() (ret interface{}) {
n := jobHeap.Len() - 1
ret = (*jobHeap)[n]
(*jobHeap) = (*jobHeap)[0:n]
return
}
func (jobHeap *JobWithTimeHeap) Push(x interface{}) {
(*jobHeap) = append((*jobHeap), x.(JobWithTime))
}
func (jobHeap *JobWithTimeHeap) Less(i, j int) bool {
return (*jobHeap)[i].RunningTime.Before((*jobHeap)[j].RunningTime)
}
func (jobHeap *JobWithTimeHeap) Len() int {
return len(*jobHeap)
}
func (jobHeap *JobWithTimeHeap) Swap(i, j int) {
t := (*jobHeap)[i]
(*jobHeap)[i] = (*jobHeap)[j]
(*jobHeap)[j] = t
}
func (jobHeap *JobWithTimeHeap) Index(i int) interface{} {
return (*jobHeap)[i]
}
type RecentJobPacker struct {
sync.Mutex
timeWindow time.Duration
recentJobs JobWithTimeHeap
}
func (rjp *RecentJobPacker) AddJob(jobName string, t time.Time) (err error) {
rjp.Lock()
defer rjp.Unlock()
heap.Push(&rjp.recentJobs, JobWithTime{
JobName: jobName,
RunningTime: t,
})
unRencentTime := time.Now().Add(-rjp.timeWindow)
for rjp.recentJobs.Len() > 0 && rjp.recentJobs.Index(0).(JobWithTime).RunningTime.Before(unRencentTime) {
heap.Pop(&rjp.recentJobs)
}
return
}
func (rjp *RecentJobPacker) PopAllJobs() (jobNames []string) {
rjp.Lock()
defer rjp.Unlock()
jobNames = make([]string, 0)
for rjp.recentJobs.Len() > 0 {
jobNames = append(jobNames, heap.Pop(&rjp.recentJobs).(JobWithTime).JobName)
}
rjp.recentJobs = make(JobWithTimeHeap, 0)
heap.Init(&rjp.recentJobs)
return
}
func NewRecentJobPacker(timeWindow time.Duration) IRecentJobPacker {
return &RecentJobPacker{
timeWindow: timeWindow,
recentJobs: make([]JobWithTime, 0),
}
}