-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.go
More file actions
179 lines (151 loc) · 3.85 KB
/
consumer.go
File metadata and controls
179 lines (151 loc) · 3.85 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
package gonyexpress
import (
"github.com/SebastiaanPasterkamp/gonyexpress/broker"
pl "github.com/SebastiaanPasterkamp/gonyexpress/payload"
"fmt"
"sync"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
// NewConsumer creates a Consumer Component instance ready to connect to the
// rabbitmq + queue and execute the operator function for every recieved
// message.
func NewConsumer(URI, qname string, workers int, operator Operator) Component {
return Component{
Broker: broker.New(URI, qname),
operator: operator,
workers: workers,
wg: sync.WaitGroup{},
}
}
// Operator is a function to be executed for every message received by the
// Consumer. The returned payload is advanced to the next step on the routing
// slip.
type Operator func(
traceID string, md pl.MetaData, args pl.Arguments, docs pl.Documents,
) (*pl.Documents, *pl.MetaData, error)
// Run launches the Component as a background service.
func (c *Component) Run() error {
if c.operator == nil {
return fmt.Errorf("cannot Run without operator")
}
if c.workers < 1 {
return fmt.Errorf("cannot Run without workers")
}
msgs, err := c.Connect()
if err != nil {
c.Close()
return errors.Wrap(err, "Failed to connect to RabbitMQ")
}
log.Info("Successfully Connected to our RabbitMQ Instance")
c.shutdown = make(chan bool)
for i := 0; i < c.workers; i++ {
c.wg.Add(1)
go c.worker(msgs)
}
log.Info("Component running")
return nil
}
// IsShuttingDown returns a channel to be closed when the Consumer is shutting
// down.
func (c *Component) IsShuttingDown() <-chan bool {
return c.shutdown
}
func (c *Component) worker(msgs <-chan amqp.Delivery) {
defer c.wg.Done()
log.Info("Launched worker...")
for {
select {
case <-c.IsShuttingDown():
log.Warning("Shutting down worker...")
return
case d := <-msgs:
msg, err := pl.MessageFromByteSlice(d.Body)
if err != nil {
log.Errorf("%s - Bad message: %+v in %+v\n",
d.CorrelationId, err, d.Body)
// TODO: enable retry
d.Nack(false, false)
continue
}
step, err := msg.CurrentStep()
if err != nil {
log.Errorf("%s - Bad message: %+v in %+v\n",
d.CorrelationId, err, d.Body)
// TODO: enable retry
d.Nack(false, false)
continue
}
if _, ok := msg.MetaData["ping"]; ok {
c.advance(d, msg, nil, nil)
continue
}
pl, md, err := c.operator(
msg.TraceID,
msg.MetaData,
step.Arguments,
msg.Documents,
)
if err != nil {
c.retry(d, msg, err)
} else {
c.advance(d, msg, pl, md)
}
}
}
}
// advance will send the message to the next step on the route
func (c *Component) advance(d amqp.Delivery, msg *pl.Message, docs *pl.Documents, md *pl.MetaData) {
next, err := msg.Advance(docs, md)
if err != nil {
log.Errorf("%s - Failed to produce next message: %+v\n", d.CorrelationId, err)
d.Nack(false, false)
return
}
if next == nil {
d.Ack(false)
return
}
err = c.SendMessage(*next)
if err != nil {
log.Errorf("%s - Failed to send message: %+v\n", d.CorrelationId, err)
d.Nack(false, true)
}
d.Ack(false)
}
// retry will send the message back to retry another time, if configured
func (c *Component) retry(d amqp.Delivery, msg *pl.Message, e error) {
next, err := msg.Retry(e)
if err != nil {
log.Errorf("%s - Failed to produc retry message: %+v\n",
d.CorrelationId, err)
}
if next == nil {
d.Ack(false)
return
}
err = c.SendMessage(*next)
if err != nil {
log.Errorf("%s - Failed to send message: %+v\n", d.CorrelationId, err)
d.Nack(false, true)
return
}
d.Ack(false)
}
// Shutdown will notify all workers to stop, and wait for all to finish.
func (c *Component) Shutdown() {
log.Println("Shutting down")
if c.shutdown == nil {
return
}
select {
case <-c.shutdown:
// Not running
default:
close(c.shutdown)
c.wg.Wait()
c.Close()
c.shutdown = nil
}
}