-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
183 lines (159 loc) · 4.06 KB
/
queue.go
File metadata and controls
183 lines (159 loc) · 4.06 KB
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
package poorq
import (
"fmt"
"os"
"sort"
"strconv"
"sync"
"time"
"github.com/subiz/log"
)
var allQueues = []*Queue{}
func init() {
go func() {
for {
time.Sleep(2 * time.Minute)
for _, queue := range allQueues {
// clean runningTasks so the memory doesn't growth too big
runningTasks := map[int64]int64{}
queue.Lock()
slowJobs := map[int64]int64{} // id -> duration, records which job took looong time to execute
now := time.Now().UnixMilli()
for jobid, start := range queue.runningTasks {
runningTasks[jobid] = start
if now-start > 60_000 {
slowJobs[jobid] = now - start/1000
}
}
queue.runningTasks = runningTasks
lentask, tail, isRunning := len(queue.tasks), queue.tail, queue.isRunning
queue.Unlock()
log.Info("POORQ REPORT STATUS", queue.path, "PENDING TAKS:", lentask, "TAIL:", tail, "ISRUNNING", isRunning)
var slowjobs string
for id, sec := range slowJobs {
slowjobs += strconv.Itoa(int(id)) + ":" + strconv.Itoa(int(sec)) + " "
}
if slowjobs != "" {
log.Info("POORQ REPORT SLOWJOB", slowjobs)
}
}
}
}()
}
type Queue struct {
*sync.Mutex
tail int64
path string
isRunning bool // sleep | running
cb func(data []byte) error
tasks []Task
runningTasks map[int64]int64 // id -> timestamp
}
type Task struct {
Id int64
Data []byte
}
// NewQueue creates new queue, do not call this function multiple times with the same <path>
func NewQueue(path string, cb func(data []byte) error) *Queue {
if path == "" {
path = "."
}
if path[len(path)-1] != '/' {
path += "/"
}
queue := &Queue{Mutex: &sync.Mutex{}, path: path, cb: cb, runningTasks: map[int64]int64{}}
tasks := listTaskFromDisk(path)
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].Id < tasks[j].Id
})
queue.tasks = tasks
if len(tasks) > 0 {
queue.tail = tasks[len(tasks)-1].Id
}
allQueues = append(allQueues, queue)
go queue.do()
return queue
}
func (queue *Queue) do() {
queue.Lock()
if queue.isRunning {
queue.Unlock() // quit if already run
return
}
queue.isRunning = true
for len(queue.tasks) > 0 {
tasks := queue.tasks
queue.tasks = nil
queue.Unlock()
for _, task := range tasks {
go func(task Task) {
queue.Lock()
start := time.Now()
queue.runningTasks[task.Id] = start.UnixMilli()
queue.Unlock()
var err error
defer func() {
commitTaskToDisk(queue.path, task.Id)
queue.Lock()
delete(queue.runningTasks, task.Id)
queue.Unlock()
if r := recover(); r != nil {
err = log.EServer(nil, log.M{"r": r, "path": queue.path, "task_id": task.Id})
}
status := "success"
if err != nil {
status = "failed, " + err.Error()
}
log.Info("subiz", "POORQ", queue.path, "TASKDONE", task.Id, "TOOK", time.Since(start), "STATUS", status)
}()
err = queue.cb(task.Data)
}(task)
}
queue.Lock()
}
queue.isRunning = false
queue.Unlock()
}
// Push adds a new task the the queue
func (queue *Queue) Push(data []byte) (int64, error) {
queue.Lock()
defer queue.Unlock()
task_id := queue.tail + 1
if err := saveTaskToDisk(queue.path, task_id, data); err != nil {
return -1, err
}
queue.tasks = append(queue.tasks, Task{Id: task_id, Data: data})
queue.tail = queue.tail + 1
if !queue.isRunning {
go queue.do()
}
return queue.tail, nil
}
func listTaskFromDisk(path string) []Task {
files, err := os.ReadDir(path)
if err != nil {
log.EServer(err, log.M{"path": path})
return nil
}
var tasks []Task
for _, file := range files {
if file.IsDir() {
continue
}
filename := fmt.Sprintf("%s%s", path, file.Name())
dat, err := os.ReadFile(filename)
if err != nil {
log.EServer(err, log.M{"path": path})
}
task_id, _ := strconv.Atoi(file.Name())
tasks = append(tasks, Task{Id: int64(task_id), Data: dat})
}
return tasks
}
func saveTaskToDisk(path string, id int64, data []byte) error {
file := fmt.Sprintf("%s%d", path, id)
return os.WriteFile(file, data, 0644)
}
func commitTaskToDisk(path string, id int64) error {
return os.Remove(fmt.Sprintf("%s%d", path, id))
}