-
Notifications
You must be signed in to change notification settings - Fork 10
/
queue.go
55 lines (43 loc) · 1 KB
/
queue.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
package gotasks
import (
"log"
)
type Queue struct {
Name string
MaxLimit int
Async bool
// monitor
MonitorInterval int
}
type QueueOption func(*Queue)
func WithMaxLimit(max int) QueueOption {
return func(q *Queue) {
q.MaxLimit = max
}
}
func WithMonitorInterval(seconds int) QueueOption {
return func(q *Queue) {
q.MonitorInterval = seconds
}
}
func WithAsyncHandleTask(async bool) QueueOption {
return func(q *Queue) {
q.Async = async
}
}
func NewQueue(name string, options ...QueueOption) *Queue {
queue := &Queue{name, 10, false, 5}
for _, o := range options {
o(queue)
}
return queue
}
func (q *Queue) Enqueue(jobName string, argsMap ArgsMap) string {
return enqueue(q.Name, jobName, argsMap)
}
// enqueue a job(which will be wrapped in task) into queue
func enqueue(queueName, jobName string, argsMap ArgsMap) string {
taskID := broker.Enqueue(NewTask(queueName, jobName, argsMap))
log.Printf("job %s enqueued to %s, taskID is %s", jobName, queueName, taskID)
return taskID
}