-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (63 loc) · 2.07 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
// encryptonator consumes the queue called 'encryptonator', when message arrives move the
// files to queued directory, run a go routine to create aes key, split the
// file in chunks, encrypt and merge all chunks and publish a message again
// to rabbitMQ
package main
import (
"flag"
"log"
"log/syslog"
"os"
"os/signal"
"sync"
"syscall"
)
var aesKey string
func main() {
var consumers int
var uri, exchange, exchangeType, queueName, bindingKey, consumerTag string
flag.StringVar(&uri, "uri", "amqp://localhost:5672/", "AMQP URI")
flag.StringVar(&exchange, "exchange", "test-exchange", "Durable, non-auto-deleted AMQP exchange name")
flag.StringVar(&exchangeType, "exchange-type", "direct", "Exchange type - direct|fanout|topic|x-custom")
flag.StringVar(&queueName, "queue", "go-test", "Ephemeral AMQP queue name")
flag.StringVar(&bindingKey, "key", "test-key", "AMQP binding key")
flag.StringVar(&consumerTag, "consumer-tag", "simple-consumer", "AMQP consumer tag (should not be blank)")
flag.IntVar(&consumers, "consumers", 5, "Number of consumers")
flag.Parse()
logwriter, err := syslog.New(syslog.LOG_NOTICE, "encryptonator-go")
if err == nil {
log.SetOutput(logwriter)
}
quitchan := make(chan string, consumers)
// listen for CTRL-C
go func() {
// we use buffered to mitigate losing the signal
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt, os.Kill, syscall.SIGTERM)
<-sigchan // wait for signal
close(quitchan) // signal consumers to stop
}()
// start consumers
log.Printf("Start %d consumers", consumers)
var wg sync.WaitGroup
for i := 0; i < consumers; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
c := &consumer{
uri: uri,
exchange: exchange,
exchangeType: exchangeType,
queueName: queueName,
bindingKey: bindingKey,
consumerTag: consumerTag,
}
log.Printf("Start consumer %d", i)
if err := c.Consume(quitchan); err != nil {
log.Printf("Consumer %d: %s", i, err)
}
log.Printf("Stop consumer %d", i)
}(i)
}
wg.Wait() // wait for consumers to finish
}