-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
273 lines (246 loc) · 6.21 KB
/
main.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"context"
"flag"
"github.com/oklog/run"
"github.com/pkg/errors"
"os"
"os/signal"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
var (
srcURI = flag.String("src-uri", "", `Source URI e.g. amqp://username:password@rabbitmq-fqdn:5672`)
srcQueue = flag.String("src-queue", "", `Source queue name`)
dstURI = flag.String("dst-uri", "", `Destination URI e.g. amqp://username:password@rabbitmq-fqdn:5672`)
dstQueue = flag.String("dst-queue", "", `Destination queue name`)
limit = flag.Int("limit", 0, "Limit the number of messages")
tx = flag.Bool("tx", false, `Use producer transactions (slow)`)
)
type moveCommand struct {
Source struct {
URI string
Queue string
}
Destination struct {
URI string
Queue string
}
Limit int
Tx bool
}
func (s *moveCommand) Run(ctx context.Context) error {
if err := s.Validate(); err != nil {
return err
}
consumerConn, err := amqp.Dial(s.Source.URI)
if err != nil {
return errors.Wrapf(err, "Cannot connect consumer to '%s'", s.Source.URI)
}
//noinspection ALL
defer consumerConn.Close()
consumerChannel, err := consumerConn.Channel()
if err != nil {
return errors.Wrapf(err, "Cannot create consumer channel")
}
//noinspection ALL
defer consumerChannel.Close()
producerConn, err := amqp.Dial(s.Destination.URI)
if err != nil {
return errors.Wrapf(err, "Cannot connect producer to '%s'", s.Destination.URI)
}
//noinspection ALL
defer producerConn.Close()
producerChannel, err := producerConn.Channel()
if err != nil {
return errors.Wrapf(err, "Cannot create producer channel")
}
//noinspection ALL
defer producerChannel.Close()
consumerQueue, err := consumerChannel.QueueDeclarePassive(
s.Source.Queue,
true,
false,
false,
false,
nil,
)
if err != nil {
return errors.Wrapf(err, "Cannot describe consumer queue '%s'", s.Source.Queue)
}
producerQueue, err := producerChannel.QueueDeclarePassive(
s.Destination.Queue,
true,
false,
false,
false,
nil,
)
if err != nil {
return errors.Wrapf(err, "Cannot describe producer queue '%s'", s.Destination.Queue)
}
log.Infof("Number of messages in consumer queue %s: %d", s.Source.Queue, consumerQueue.Messages)
log.Infof("Number of messages in producer queue %s: %d", s.Destination.Queue, producerQueue.Messages)
//
if consumerQueue.Messages == 0 && s.Limit >= 0 {
log.Infof("Consumer queue is empty")
return nil
}
limit := s.Limit
if s.Limit == 0 {
limit = consumerQueue.Messages
}
log.Infof("Moving messages from %s to %s with limit %d", s.Source.Queue, s.Destination.Queue, limit)
return s.move(ctx, consumerChannel, producerChannel, limit)
}
func (s *moveCommand) move(ctx context.Context, consumerChannel, producerChannel *amqp.Channel, limit int) error {
msgs, err := consumerChannel.Consume(
s.Source.Queue, // queue
"rabbitmq-mv",
false,
false,
false,
false,
nil,
)
if err != nil {
return errors.Wrapf(err, "Cannot consume from queue '%s'", s.Source.Queue)
}
if s.Tx {
// this will be slow ...
err = producerChannel.Tx()
if err != nil {
return errors.Wrapf(err, "Cannot open producer transaction")
}
}
var counter int
consumeLoop:
for {
select {
case d := <-msgs:
err = producerChannel.Publish(
"",
s.Destination.Queue,
true,
false,
amqp.Publishing{
Body: d.Body,
Headers: d.Headers,
ContentType: d.ContentType,
ContentEncoding: d.ContentEncoding,
DeliveryMode: d.DeliveryMode,
Priority: d.Priority,
CorrelationId: d.CorrelationId,
ReplyTo: d.ReplyTo,
Expiration: d.Expiration,
MessageId: d.MessageId,
Timestamp: d.Timestamp,
Type: d.Type,
UserId: d.UserId,
AppId: d.AppId,
})
if err != nil {
return err
}
if s.Tx {
err = producerChannel.TxCommit()
if err != nil {
return errors.Wrapf(err, "Cannot commit producer transaction")
}
}
err = consumerChannel.Ack(d.DeliveryTag, false)
if err != nil {
return errors.Wrapf(err, "Cannot ack consumer message")
}
counter++
if limit >= 0 && counter >= limit {
break consumeLoop
}
case <-time.After(1 * time.Second):
if limit >= 0 {
break consumeLoop
}
log.Infof("Moved so far %d messages ", counter)
case <-ctx.Done():
log.Warnf("Move interrupted after %d messages ", counter)
break consumeLoop
}
}
log.Infof("Moved %d messages ", counter)
return nil
}
func (s *moveCommand) Validate() error {
if s.Source.URI == "" {
return errors.New("Source URI must not be empty")
}
if s.Source.Queue == "" {
return errors.New("Source Queue must not be empty")
}
if s.Destination.URI == "" {
return errors.New("Destination URI must not be empty")
}
if s.Destination.Queue == "" {
return errors.New("Destination Queue must not be empty")
}
if s.Source.URI == s.Destination.URI && s.Source.Queue == s.Destination.Queue {
return errors.New("Source and Destination are the same")
}
return nil
}
func newMoveCommand() (*moveCommand, error) {
c := moveCommand{}
c.Source.URI = *srcURI
c.Source.Queue = *srcQueue
c.Destination.URI = *dstURI
c.Destination.Queue = *dstQueue
c.Limit = *limit
c.Tx = *tx
if c.Destination.URI == "" {
c.Destination.URI = c.Source.URI
}
if c.Source.URI == "" {
c.Source.URI = c.Destination.URI
}
return &c, nil
}
func main() {
flag.Parse()
start := time.Now()
defer func() {
log.Infof("Operation took %v", time.Now().Sub(start))
}()
moveCommand, err := newMoveCommand()
if err != nil {
log.Fatalf("%v", err)
}
shutdownCtx, shutdownFunc := context.WithCancel(context.Background())
defer shutdownFunc()
var g run.Group
g.Add(func() error {
defer shutdownFunc()
handleSigterm(shutdownCtx)
return nil
}, func(error) {
})
g.Add(func() error {
defer shutdownFunc()
return moveCommand.Run(shutdownCtx)
}, func(error) {
})
err = g.Run()
if err != nil {
log.Fatalf("%v", err)
}
}
func handleSigterm(ctx context.Context) {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ctx.Done():
log.Debugf("shutdown received ...")
case q := <-quit:
log.Infof("quit %v received ... ", q)
}
}