/
kafkaConsumerMsgs.go
executable file
·66 lines (57 loc) · 1.72 KB
/
kafkaConsumerMsgs.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
/*
Copyright [2019] - [2020], PERSISTENCE TECHNOLOGIES PTE. LTD. and the persistenceSDK contributors
SPDX-License-Identifier: Apache-2.0
*/
package rest
import (
"github.com/cosmos/cosmos-sdk/client/context"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/persistenceOne/persistenceSDK/utilities/rest/queuing"
"time"
)
// KafkaConsumerMessages : messages to consume 5 second delay
func KafkaConsumerMessages(cliCtx context.CLIContext, kafkaState queuing.KafkaState) {
quit := make(chan bool)
var cliContextList []context.CLIContext
var baseRequestList []rest.BaseReq
var ticketIDList []queuing.Ticket
var msgList []sdkTypes.Msg
go func() {
for {
select {
case <-quit:
return
default:
kafkaMsg := queuing.KafkaTopicConsumer("Topic", kafkaState.Consumers, cliCtx.Codec)
if kafkaMsg.Msg != nil {
cliContextList = append(cliContextList, queuing.CliCtxFromKafkaMsg(kafkaMsg, cliCtx))
baseRequestList = append(baseRequestList, kafkaMsg.BaseRequest)
ticketIDList = append(ticketIDList, kafkaMsg.TicketID)
msgList = append(msgList, kafkaMsg.Msg)
}
}
}
}()
time.Sleep(queuing.SleepTimer)
quit <- true
if len(msgList) == 0 {
return
}
output, err := SignAndBroadcastMultiple(baseRequestList, cliContextList, msgList)
if err != nil {
jsonError, e := cliCtx.Codec.MarshalJSON(struct {
Error string `json:"error"`
}{Error: err.Error()})
if e != nil {
panic(err)
}
for _, ticketID := range ticketIDList {
queuing.AddResponseToDB(ticketID, jsonError, kafkaState.KafkaDB, cliCtx.Codec)
}
return
}
for _, ticketID := range ticketIDList {
queuing.AddResponseToDB(ticketID, output, kafkaState.KafkaDB, cliCtx.Codec)
}
}