-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtask.go
147 lines (127 loc) · 2.94 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
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
package task
import (
"math/rand"
"runtime"
"sync"
"time"
)
const (
StateWaiting = "waiting"
StateCompleted = "completed"
StateError = "failed"
StateNone = "none"
StateOverdue = "overdue"
)
var (
taskStateMutex sync.Mutex
taskPool sync.Pool
taskChan = make(chan *Task, runtime.NumCPU())
taskState = make(map[string]string)
)
type Task struct {
Param map[string]interface{}
Factory []FacFunc
UUID string
Expiration int64
}
type FacFunc func(string, map[string]interface{}) (string, error)
func NewTask(param map[string]interface{}, factory []FacFunc, d time.Duration) *Task {
var expiration int64
if d > 0 {
expiration = time.Now().Add(d).UnixNano()
} else {
expiration = -1
}
t := taskPool.Get()
if t == nil {
return &Task{
param,
factory,
getUUID(20),
expiration,
}
} else {
task := t.(*Task)
(*task).Param = param
(*task).Factory = factory
(*task).UUID = getUUID(20)
(*task).Expiration = expiration
return task
}
}
func AddTask(task *Task) string {
go func() {
taskChan <- task
}()
uuid := (*task).UUID
UpdateTaskState(uuid, StateWaiting)
return uuid
}
func UpdateTaskState(uuid, state string) {
taskStateMutex.Lock()
defer taskStateMutex.Unlock()
taskState[uuid] = state
}
func GetTaskState(uuid string) (state string) {
taskStateMutex.Lock()
defer taskStateMutex.Unlock()
resultState, exists := taskState[uuid]
if !exists {
state = StateNone
} else {
state = resultState
}
return
}
func taskReceiver() {
var taskUUID string
var err error
for {
task := <-taskChan
if ((*task).Expiration > 0 && time.Now().UnixNano() < (*task).Expiration) || (*task).Expiration < 0 {
for _, f := range (*task).Factory {
taskUUID, err = f((*task).UUID, (*task).Param)
}
if err != nil {
UpdateTaskState(taskUUID, StateError)
taskPool.Put(task)
} else {
UpdateTaskState(taskUUID, StateCompleted)
taskPool.Put(task)
}
} else {
UpdateTaskState(taskUUID, StateOverdue)
taskPool.Put(task)
}
}
}
func InitTaskReceiver(num int) {
for i := 0; i < num; i++ {
go taskReceiver()
}
}
func getUUID(length int64) string {
ele := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "v", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
ele, _ = random(ele)
uuid := ""
rand.Seed(time.Now().UnixNano())
var i int64
for i = 0; i < length; i++ {
uuid += ele[rand.Intn(59)]
}
return uuid
}
func random(strings []string) ([]string, error) {
rand.Seed(time.Now().UnixNano())
for i := len(strings) - 1; i > 0; i-- {
num := rand.Intn(i + 1)
strings[i], strings[num] = strings[num], strings[i]
}
str := make([]string, 0)
for i := 0; i < len(strings); i++ {
str = append(str, strings[i])
}
return str, nil
}