/
context_asynctask.go
80 lines (68 loc) · 2 KB
/
context_asynctask.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
// Create and maintain by Chaiyapong Lapliengtrakul (chaiyapong@3dsinteractive.com), All right reserved (2021 - Present)
package main
import (
"fmt"
"runtime"
"strings"
"time"
)
// AsyncTaskContext implement IContext it is context for Consumer
type AsyncTaskContext struct {
ms *Microservice
cacheServer string
ref string
input string
}
// NewAsyncTaskContext is the constructor function for AsyncTaskContext
func NewAsyncTaskContext(ms *Microservice, cacheServer string, ref string, input string) *AsyncTaskContext {
return &AsyncTaskContext{
ms: ms,
cacheServer: cacheServer,
ref: ref,
input: input,
}
}
// Log will log a message
func (ctx *AsyncTaskContext) Log(message string) {
_, fn, line, _ := runtime.Caller(1)
fns := strings.Split(fn, "/")
fmt.Println("ATASK:", fns[len(fns)-1], line, message)
}
// Param return parameter by name (empty in AsyncTask)
func (ctx *AsyncTaskContext) Param(name string) string {
return ""
}
// QueryParam return empty in async task
func (ctx *AsyncTaskContext) QueryParam(name string) string {
return ""
}
// ReadInput return message (return empty in AsyncTask)
func (ctx *AsyncTaskContext) ReadInput() string {
return ctx.input
}
// ReadInputs return messages in batch (return nil in AsyncTask)
func (ctx *AsyncTaskContext) ReadInputs() []string {
return nil
}
// Response return response to client
func (ctx *AsyncTaskContext) Response(responseCode int, responseData interface{}) {
cacher := ctx.Cacher(ctx.cacheServer)
res := map[string]interface{}{
"status": "success",
"code": responseCode,
"data": responseData,
}
cacher.Set(ctx.ref, res, 30*time.Minute)
}
// Cacher return cacher
func (ctx *AsyncTaskContext) Cacher(server string) ICacher {
return ctx.ms.getCacher(server)
}
// Producer return producer
func (ctx *AsyncTaskContext) Producer(servers string) IProducer {
return ctx.ms.getProducer(servers)
}
// MQ return MQ
func (ctx *AsyncTaskContext) MQ(servers string) IMQ {
return NewMQ(servers, ctx.ms)
}