-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
54 lines (41 loc) · 846 Bytes
/
worker.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
package goworker
import (
"fmt"
"sync"
)
var mqMap sync.Map
var onErrorLog = func(s string) {
fmt.Println(s)
}
var onInfoLog = func(s string) {
fmt.Println(s)
}
func InitLog(onError func(string), onInfo func(string)) {
onErrorLog = onError
onInfoLog = onInfo
}
//最大任务队列, 与最大工作线程
func Init(name string, maxQueue int, maxWorker int) {
jobQueue := newJobQueue(maxQueue, maxWorker)
mqMap.Store(name, jobQueue)
}
//丢任务进去
func Push(name string, job func()) {
mqInter, ok := mqMap.Load(name)
if !ok {
if onErrorLog != nil {
onErrorLog(fmt.Sprintf("name:%s的mq不存在", name))
}
job()
return
}
mq, ok := mqInter.(*jobQueue_)
if !ok {
if onErrorLog != nil {
onErrorLog(fmt.Sprintf("name:%s的mq不能转成 *jobQuene_", name))
}
job()
return
}
mq.PushJob(job)
}