-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.go
More file actions
193 lines (155 loc) · 4.34 KB
/
consumer.go
File metadata and controls
193 lines (155 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"os"
"github.com/assembla/cony"
model "github.com/psavelis/goa-pos-poc/app"
"github.com/streadway/amqp"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
url *string
)
type posConnection struct {
db *mgo.Database
}
func getCloudAmqpURL() (url *string) {
amqpUser := os.Getenv("CLOUD_AMQP_USER")
missingEnv := false
if amqpUser == "" {
missingEnv = true
log.Printf("$CLOUD_AMQP_USER must be set")
}
amqpPassword := os.Getenv("CLOUD_AMQP_PASSWORD")
if amqpPassword == "" {
missingEnv = true
log.Printf("$CLOUD_AMQP_PASSWORD must be set")
}
if missingEnv {
panic("CloudAmqp environment variables not configured")
}
url = flag.String("url", fmt.Sprintf("amqp://%s:%s@elephant.rmq.cloudamqp.com/%s", amqpUser, amqpPassword, amqpUser), "amqp url")
return url
}
func showUsageAndStatus() {
fmt.Printf("Consumer is running\n\n")
fmt.Println("Flags:")
flag.PrintDefaults()
fmt.Printf("\n\n")
}
func main() {
url = getCloudAmqpURL()
flag.Parse()
showUsageAndStatus()
// Creates a mongodb database instance
db := getDatabase()
// Construct new client with the flag url
// and default backoff policy
cli := cony.NewClient(
cony.URL(*url),
cony.Backoff(cony.DefaultBackoff),
)
// Declarations
// The queue name will be supplied by the AMQP server
que := &cony.Queue{
AutoDelete: true,
Name: "pos-purchase-created-queue",
}
exc := cony.Exchange{
Name: "purchase.created",
Kind: "fanout",
AutoDelete: true,
}
bnd := cony.Binding{
Queue: que,
Exchange: exc,
Key: "pubSub",
}
cli.Declare([]cony.Declaration{
cony.DeclareQueue(que),
cony.DeclareExchange(exc),
cony.DeclareBinding(bnd),
})
// Declare and register a consumer
cns := cony.NewConsumer(
que,
//cony.AutoAck(), // Auto sign the deliveries
)
cli.Consume(cns)
for cli.Loop() {
select {
case msg := <-cns.Deliveries():
log.Printf("Received body: %q\n", msg.Body)
// starts a new `goroutine` to process the msg.
go handleMessage(&msg, db)
case err := <-cns.Errors():
fmt.Printf("Consumer error: %v\n", err)
case err := <-cli.Errors():
fmt.Printf("Client error: %v\n", err)
}
}
}
// handleMessage process a new purchase message
func handleMessage(msg *amqp.Delivery, conn *posConnection) error {
// reuse from connection pool
session := conn.db.Session.Copy()
defer session.Close()
// defines an object with Purchase model defined in POS API
payload := model.Purchase{}
payload.Status = "FINISHED"
// deserializes the payload
if err := json.Unmarshal(msg.Body, &payload); err != nil {
fmt.Printf("Consumer failed to deserialize payload: %v\n", err)
msg.Nack(false, true) //failed to deserialize, requeues the message
return err
}
collection := session.DB("services-pos").C("Purchase")
err := collection.Update(bson.M{"_id": payload.TransactionID}, bson.M{"$set": bson.M{"status": payload.Status}})
if err != nil {
fmt.Printf("Consumer failed to update Purchase (ID=%q) status: %v\n", payload.TransactionID, err)
msg.Nack(false, true)
return err
}
// If when we built the consumer we didn't use
// the "cony.AutoAck()" option this is where we'd
// have to call the "amqp.Deliveries" methods "Ack",
// "Nack", "Reject"
//
// msg.Ack(false)
// msg.Nack(false)
// msg.Reject(false)
return msg.Ack(false)
}
func getDatabase() (db *posConnection) {
// MongoDB (Atlas) setup
tlsConfig := &tls.Config{}
tlsConfig.InsecureSkipVerify = true
mgoUser := os.Getenv("MONGO_USR")
if mgoUser == "" {
log.Printf("$MONGO_USR must be set")
}
mgoPassword := os.Getenv("MONGO_PWD")
if mgoPassword == "" {
log.Printf("$MONGO_PWD must be set")
}
dialInfo, err := mgo.ParseURL(fmt.Sprintf("mongodb://%s:%s@development-shard-00-00-ozch3.mongodb.net:27017,development-shard-00-01-ozch3.mongodb.net:27017,development-shard-00-02-ozch3.mongodb.net:27017/test?replicaSet=development-shard-0&authSource=admin", mgoUser, mgoPassword))
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
// services-pos database
database := *session.DB("services-pos")
return &posConnection{db: &database}
}