-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
146 lines (116 loc) · 3.32 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
package gioc
import (
"sync"
)
type taskManager struct {
addTaskChan chan *taskDefinition
removeTaskChan chan *taskDefinition
stopServeChan chan bool
onServeMutex sync.Mutex
onServe bool
}
func (tm *taskManager) addTask(task *taskDefinition) {
tm.addTaskChan <- task
}
func (tm *taskManager) serve() {
tm.onServeMutex.Lock()
defer tm.onServeMutex.Unlock()
if !tm.onServe {
return
}
tm.onServe = true
go func() {
runningTasksListeners := newRunningTasksListenersMap()
for {
select {
case newTaskDef := <-tm.addTaskChan:
// If this task manager was told to stop serving - do not process new task
if !tm.onServe {
continue
}
// If such task is already running - just add new listener to running task
if _, taskAlreadyRunning := runningTasksListeners.get(newTaskDef.taskName); taskAlreadyRunning {
runningTasksListeners.append(newTaskDef.taskName, newTaskDef.listener)
continue
}
// Run task
runningTasksListeners.append(newTaskDef.taskName, newTaskDef.listener)
go func() {
result, taskError := newTaskDef.perform()
taskResultObj := &taskResult{
result: result,
taskError: taskError,
}
listeners, _ := runningTasksListeners.get(newTaskDef.taskName)
for _, listener := range listeners {
listener <- taskResultObj
}
tm.removeTaskChan <- newTaskDef
}()
case processedTask := <-tm.removeTaskChan:
runningTasksListeners.delete(processedTask.taskName)
case <-tm.stopServeChan:
// This stopServeChan channel used to stop this goroutine after stopServe() call if no task are running
tm.onServe = false
}
if !tm.onServe && runningTasksListeners.len() == 0 {
return
}
}
}()
}
func (tm *taskManager) stopServe() {
tm.stopServeChan <- true
}
// --------------------------------------------
type taskDefinition struct {
taskName string
listener chan *taskResult
perform func() (interface{}, error)
}
// --------------------------------------------
type taskResult struct {
result interface{}
taskError error
}
// --------------------------------------------
type runningTasksListenersMap struct {
runningTasks map[string][]chan *taskResult
mutex sync.RWMutex
}
func (m *runningTasksListenersMap) get(key string) ([]chan *taskResult, bool) {
m.mutex.RLock()
defer m.mutex.RUnlock()
val, isSet := m.runningTasks[key]
return val, isSet
}
func (m *runningTasksListenersMap) append(taskName string, listener chan *taskResult) {
m.mutex.Lock()
defer m.mutex.Unlock()
if _, keyIsPresent := m.runningTasks[taskName]; !keyIsPresent {
m.runningTasks[taskName] = make([]chan *taskResult, 0)
}
m.runningTasks[taskName] = append(m.runningTasks[taskName], listener)
}
func (m *runningTasksListenersMap) len() int {
return len(m.runningTasks)
}
func (m *runningTasksListenersMap) delete(key string) {
m.mutex.Lock()
defer m.mutex.Unlock()
delete(m.runningTasks, key)
}
// --------------------------------------------
func newRunningTasksListenersMap() *runningTasksListenersMap {
return &runningTasksListenersMap{
runningTasks: make(map[string][]chan *taskResult, 0),
}
}
func newTaskManager() *taskManager {
return &taskManager{
addTaskChan: make(chan *taskDefinition),
removeTaskChan: make(chan *taskDefinition),
stopServeChan: make(chan bool),
onServe: true,
}
}