-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicroservice_paralleltask.go
More file actions
165 lines (146 loc) · 4.34 KB
/
microservice_paralleltask.go
File metadata and controls
165 lines (146 loc) · 4.34 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Create and maintain by Chaiyapong Lapliengtrakul (chaiyapong@3dsinteractive.com), All right reserved (2021 - Present)
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
// ptaskWorker register worker node for ParallelTask
func (ms *Microservice) ptaskWorkerNode(path string, cacheServer string, mqServers string, h ServiceHandleFunc) {
topic := escapeName("ptask", path)
mq := NewMQ(mqServers, ms)
err := mq.CreateTopicR(topic, 5, 1, time.Hour*24*30)
if err != nil {
ms.Log("PTASK", err.Error())
return
}
ms.Consume(mqServers, topic, "ptask", -1, func(ctx IContext) error {
message := map[string]interface{}{}
err := json.Unmarshal([]byte(ctx.ReadInput()), &message)
if err != nil {
ms.Log("PTASK", err.Error())
return err
}
taskID, _ := message["task_id"].(string)
workerID, _ := message["worker_id"].(string)
input, _ := message["input"].(string)
return h(NewPTaskContext(ms, cacheServer, taskID, workerID, input))
})
}
// PTaskWorkerNode register worker node for ParallelTask
func (ms *Microservice) PTaskWorkerNode(path string, cacheServer string, mqServers string, h ServiceHandleFunc) {
go ms.ptaskWorkerNode(path, cacheServer, mqServers, h)
}
func (ms *Microservice) handlePTaskPOST(path string, cacheServer string, mqServers string, ctx IContext) error {
topic := escapeName("ptask", path)
// 1. Read Input
input := ctx.ReadInput()
taskIDParam := ctx.QueryParam("task_id")
workerCountStr := ctx.QueryParam("worker_count")
if len(taskIDParam) == 0 {
return fmt.Errorf("task_id in query param is required")
}
// 2. Get status of current task
// - If it is running, then return
// - If it is not running, then start task
taskID := "ptask-" + taskIDParam
cacher := ctx.Cacher(cacheServer)
statusStr, err := cacher.Get(taskID)
if err != nil {
ms.Log("PTASK", err.Error())
return err
}
status := map[string]interface{}{}
if len(statusStr) != 0 {
err = json.Unmarshal([]byte(statusStr), &status)
if err != nil {
ms.Log("PTASK", err.Error())
return err
}
taskStatus, _ := status["status"].(string)
if taskStatus == "running" {
return nil
}
}
// 3. Create new task status and save in cache
workerCount, err := strconv.Atoi(workerCountStr)
if err != nil {
workerCount = 3 // default workers size
}
workers := []map[string]interface{}{}
messages := []map[string]interface{}{}
for i := 0; i < workerCount; i++ {
workerID := taskID + "-" + randString()
message := map[string]interface{}{
"task_id": taskID,
"worker_id": workerID,
"input": input,
}
messages = append(messages, message)
workers = append(workers, map[string]interface{}{
"status": "running",
"worker_id": workerID,
"code": "",
"response": "",
"error": "",
})
}
status = map[string]interface{}{
"status": "running",
"workers": workers,
}
// 4. Set Status in Cache
expire := time.Minute * 30
cacher.Set(taskID, status, expire)
// 5. Send message to start ptask
prod := ctx.Producer(mqServers)
for _, message := range messages {
prod.SendMessage(topic, "", message)
}
// 6. Response task_id
res := map[string]string{
"task_id": taskIDParam,
}
ctx.Response(http.StatusOK, res)
return nil
}
func (ms *Microservice) handlePTaskGET(path string, cacheServer string, mqServers string, ctx IContext) error {
// 1. Read Input
taskIDParam := ctx.QueryParam("task_id")
if len(taskIDParam) == 0 {
return fmt.Errorf("task_id in query param is required")
}
// 2. Get status of current task
// - If it is running, then return
// - If it is not running, then start task
taskID := "ptask-" + taskIDParam
cacher := ctx.Cacher(cacheServer)
statusStr, err := cacher.Get(taskID)
if err != nil {
ms.Log("PTASK", err.Error())
return err
}
status := map[string]interface{}{}
if len(statusStr) > 0 {
err = json.Unmarshal([]byte(statusStr), &status)
if err != nil {
ms.Log("PTASK", err.Error())
return err
}
}
ctx.Response(http.StatusOK, status)
return nil
}
// PTask register handler to start/stop/status ParallelTask
func (ms *Microservice) PTaskEndpoint(path string, cacheServer string, mqServers string) {
// Start PTask
ms.POST(path, func(ctx IContext) error {
return ms.handlePTaskPOST(path, cacheServer, mqServers, ctx)
})
// Get PTask Status
ms.GET(path, func(ctx IContext) error {
return ms.handlePTaskGET(path, cacheServer, mqServers, ctx)
})
}