-
Notifications
You must be signed in to change notification settings - Fork 48
/
receive.go
143 lines (125 loc) · 3.73 KB
/
receive.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
package queries
import (
"context"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/kubemq-io/kubemq-community/pkg/utils"
"github.com/kubemq-io/kubemq-community/config"
"github.com/spf13/cobra"
"time"
)
type ReceiveOptions struct {
cfg *config.Config
channel string
group string
autoResponse bool
}
var queriesReceiveExamples = `
# Receive 'queries' from a 'queries' channel (blocks until next body)
kubemq queries receive some-channel
# Receive 'queries' from a 'queries' channel with group(blocks until next body)
kubemq queries receive some-channel -g G1
`
var queriesReceiveLong = `Queries receive (Subscribe) command allows to receive a body from a 'queries' channel and response with appropriate reply`
var queriesReceiveShort = `Queries receive a body from a 'queries' channel`
func NewCmdQueriesReceive(ctx context.Context, cfg *config.Config) *cobra.Command {
o := &ReceiveOptions{
cfg: cfg,
}
cmd := &cobra.Command{
Use: "receive",
Aliases: []string{"r", "rec", "subscribe", "sub"},
Short: queriesReceiveShort,
Long: queriesReceiveLong,
Example: queriesReceiveExamples,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
utils.CheckErr(o.Complete(args), cmd)
utils.CheckErr(o.Validate())
utils.CheckErr(o.Run(ctx))
},
}
cmd.PersistentFlags().StringVarP(&o.group, "group", "g", "", "set 'queries' channel consumer group (load balancing)")
cmd.PersistentFlags().BoolVarP(&o.autoResponse, "auto-response", "a", false, "set auto response executed query")
return cmd
}
func (o *ReceiveOptions) Complete(args []string) error {
if len(args) >= 1 {
o.channel = args[0]
return nil
}
return fmt.Errorf("missing channel argument")
}
func (o *ReceiveOptions) Validate() error {
return nil
}
func (o *ReceiveOptions) Run(ctx context.Context) error {
client, err := utils.GetKubeMQClient(ctx, o.cfg)
if err != nil {
return fmt.Errorf("create kubemq client, %s", err.Error())
}
defer func() {
_ = client.Close()
}()
errChan := make(chan error, 1)
queriesChan, err := client.SubscribeToQueries(ctx, o.channel, o.group, errChan)
if err != nil {
utils.Println(fmt.Errorf("receive 'queries' messages, %s", err.Error()).Error())
}
for {
utils.Println("waiting for the next query body...")
select {
case err := <-errChan:
return fmt.Errorf("server disconnected with error: %s", err.Error())
case query, opened := <-queriesChan:
if !opened {
utils.Println("server disconnected")
return nil
}
printQueryReceive(query)
if o.autoResponse {
err = client.R().SetRequestId(query.Id).SetExecutedAt(time.Now()).SetResponseTo(query.ResponseTo).SetBody([]byte("executed your query")).Send(ctx)
if err != nil {
return err
}
utils.Println("auto execution sent executed response ")
continue
}
var isExecuted bool
prompt := &survey.Confirm{
Renderer: survey.Renderer{},
Message: "Set executed ?",
Help: "",
}
err := survey.AskOne(prompt, &isExecuted)
if err != nil {
return err
}
if isExecuted {
respBody := ""
prompt := &survey.Input{
Renderer: survey.Renderer{},
Message: "Set response body",
Default: "response-to",
Help: "",
}
err := survey.AskOne(prompt, &respBody)
if err != nil {
return err
}
err = client.R().SetRequestId(query.Id).SetExecutedAt(time.Now()).SetResponseTo(query.ResponseTo).SetBody([]byte(respBody)).Send(ctx)
if err != nil {
return err
}
continue
}
err = client.R().SetRequestId(query.Id).SetError(fmt.Errorf("query not executed")).SetResponseTo(query.ResponseTo).Send(ctx)
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}