forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_task_service.go
126 lines (102 loc) · 2.6 KB
/
async_task_service.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
package task
import (
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
// Access to the currentTasks map should always be performed in the semaphore
// Use the taskSem channel for that
type asyncTaskService struct {
uuidGen boshuuid.Generator
logger boshlog.Logger
currentTasks map[string]Task
taskChan chan Task
taskSem chan func()
}
func NewAsyncTaskService(uuidGen boshuuid.Generator, logger boshlog.Logger) (service Service) {
s := asyncTaskService{
uuidGen: uuidGen,
logger: logger,
currentTasks: make(map[string]Task),
taskChan: make(chan Task),
taskSem: make(chan func()),
}
go s.processTasks()
go s.processSemFuncs()
return s
}
func (service asyncTaskService) CreateTask(
taskFunc Func,
cancelFunc CancelFunc,
endFunc EndFunc,
) (Task, error) {
uuid, err := service.uuidGen.Generate()
if err != nil {
return Task{}, err
}
return service.CreateTaskWithID(uuid, taskFunc, cancelFunc, endFunc), nil
}
func (service asyncTaskService) CreateTaskWithID(
id string,
taskFunc Func,
cancelFunc CancelFunc,
endFunc EndFunc,
) Task {
return Task{
ID: id,
State: StateRunning,
Func: taskFunc,
CancelFunc: cancelFunc,
EndFunc: endFunc,
}
}
func (service asyncTaskService) StartTask(task Task) {
taskChan := make(chan Task)
service.taskSem <- func() {
service.currentTasks[task.ID] = task
taskChan <- task
}
recordedTask := <-taskChan
service.taskChan <- recordedTask
}
func (service asyncTaskService) FindTaskWithID(id string) (Task, bool) {
taskChan := make(chan Task)
foundChan := make(chan bool)
service.taskSem <- func() {
task, found := service.currentTasks[id]
taskChan <- task
foundChan <- found
}
return <-taskChan, <-foundChan
}
func (service asyncTaskService) processSemFuncs() {
defer service.logger.HandlePanic("Task Service Process Sem Funcs")
for {
do := <-service.taskSem
do()
}
}
func (service asyncTaskService) processTasks() {
defer service.logger.HandlePanic("Task Service Process Tasks")
for {
task := <-service.taskChan
value, err := task.Func()
if err != nil {
task.Error = err
task.State = StateFailed
service.logger.Error("Task Service", "Failed processing task #%s got: %s", task.ID, err.Error())
} else {
task.Value = value
task.State = StateDone
}
if task.EndFunc != nil {
task.EndFunc(task)
}
// Nil to prevent to memory leaks in case these are closures.
task.Func = nil
task.CancelFunc = nil
task.EndFunc = nil
service.taskSem <- func() {
service.currentTasks[task.ID] = task
}
}
}