-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrmq.go
139 lines (108 loc) · 2.68 KB
/
rmq.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
package main
import (
"fmt"
"github.com/0x6e6562/gosnow"
log "github.com/cihub/seelog"
"github.com/jessevdk/go-flags"
"github.com/michaelklishin/rabbit-hole"
"github.com/relops/rmq/work"
"os"
"strings"
"sync"
)
var logConfig = `
<seelog type="sync">
<outputs formatid="main">
<console/>
</outputs>
<formats>
<format id="main" format="%Date(2006-02-01 03:04:05.000) - %Msg%n"/>
</formats>
</seelog>`
var (
opts work.Options
VERSION string = "0.2.52"
)
func init() {
opts.AdvertizedVersion = VERSION
opts.Version = printVersionAndExit
// We might want to make this overridable
logger, err := log.LoggerFromConfigAsString(logConfig)
if err != nil {
fmt.Printf("Could not load seelog configuration: %s\n", err)
return
}
log.ReplaceLogger(logger)
}
func main() {
if _, err := flags.Parse(&opts); err != nil {
os.Exit(0)
}
if err := opts.Validate(); err != nil {
fmt.Printf("%v\n", err)
os.Exit(0)
}
if opts.UsesMgmt() {
// TODO make more configurable
url := fmt.Sprintf("http://%s:%d", opts.Host, opts.MgmtPort)
rmqc, _ := rabbithole.NewClient(url, opts.Username, opts.Password)
if opts.Info {
work.Info(rmqc)
} else if opts.Delete && len(opts.Queue) > 0 {
work.DeleteQueue(rmqc, opts.Vhost, opts.Queue)
} else if len(opts.QueueInfo) > 0 && len(opts.HA) == 0 {
work.Queues(rmqc)
} else if opts.Delete && len(opts.HAName) > 0 {
work.DeleteMirror(rmqc, opts.Vhost, opts.HAName)
} else if len(opts.HAName) > 0 && len(opts.Queue) > 0 {
work.CreateMirror(rmqc, opts.Vhost, opts.HAName, opts.Queue, opts.Replication, int(opts.Priority), opts.Nodes...)
} else if len(opts.HA) > 0 {
work.Mirroring(rmqc)
} else {
fmt.Println("Unspecified management command")
}
os.Exit(0)
}
flake, err := gosnow.NewSnowFlake(201)
if err != nil {
log.Errorf("Could not initialize snowflake: %s", err)
os.Exit(1)
}
signal := make(chan error)
var wg sync.WaitGroup
for i := 0; i < (&opts).Connections; i++ {
c, err := work.NewClient(&opts, flake)
if err != nil {
log.Error(err)
os.Exit(1)
}
for i := 0; i < (&opts).Concurrency; i++ {
if opts.IsSender() {
wg.Add(1)
go work.StartSender(c, signal, &opts, &wg)
} else {
go work.StartReceiver(c, signal, &opts)
}
}
}
err = <-signal
if err != nil {
if shouldLogError(err) {
log.Error(err)
}
os.Exit(1)
}
wg.Wait()
}
func shouldLogError(err error) bool {
if strings.Contains(err.Error(), "PRECONDITION_FAILED") {
log.Error(err)
log.Info("Potential attempt to redeclare an existing queue - to avoid this, use the -n option")
return false
}
return true
}
func printVersionAndExit() {
fmt.Fprintf(os.Stderr, "%s %s\n", "rmq", VERSION)
os.Exit(0)
}