-
Notifications
You must be signed in to change notification settings - Fork 2
/
subscriber.go
180 lines (152 loc) · 4.33 KB
/
subscriber.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
package rabbit
import (
"errors"
"fmt"
"log"
"os"
"os/user"
"sync"
"github.com/streadway/amqp"
)
var (
// Subscribers is a map of all of the registered Subscribers
Subscribers map[string]Subscriber
// Handlers is a map of all of the registered Subscriber Handlers
Handlers map[string]func(amqp.Delivery) bool
subscribersStarted = false
lock sync.RWMutex
nonDevEnvironments = []string{"production", "prod", "staging", "stage"}
)
// Subscriber contains all of the necessary data for Publishing and Subscriber to RabbitMQ Topics
type Subscriber struct {
Concurrency int
Durable bool
Exchange string
Queue string
RoutingKey string
PrefetchCount int
AutoDelete bool
ManualAck bool
}
func (subscriber *Subscriber) printDetails() {
log.Printf(`Starting subscriber
Durable: %t
Exchange: %s
Queue: %s
RoutingKey: %s
AutoDelete: %v
ManualAck: %v
`,
subscriber.Durable,
subscriber.Exchange,
subscriber.Queue,
subscriber.RoutingKey,
subscriber.AutoDelete,
subscriber.ManualAck,
)
}
// StartSubscribers spins up all of the registered Subscribers and consumes messages on their
// respective queues.
func StartSubscribers() error {
lock.Lock()
defer lock.Unlock()
conn := connectionWithoutLock()
subscribersStarted = true
return startSubscribers(conn)
}
func startSubscribers(conn *amqp.Connection) error {
for _, subscriber := range Subscribers {
for i := 0; i < subscriber.Concurrency; i++ {
subscriber.printDetails()
channel, err := createConnectionClosingChannel(conn)
if err != nil {
return err
}
if err := CreateQueue(channel, &subscriber); err != nil {
return err
}
if err := createConsumer(channel, &subscriber); err != nil {
return err
}
}
}
return nil
}
// Register adds a subscriber and handler to the subscribers pool
func Register(s Subscriber, handler func(amqp.Delivery) bool) {
if Subscribers == nil {
Subscribers = make(map[string]Subscriber)
Handlers = make(map[string]func(amqp.Delivery) bool)
}
if Handlers == nil {
Handlers = make(map[string]func(amqp.Delivery) bool)
}
Subscribers[s.RoutingKey] = s
Handlers[s.RoutingKey] = handler
}
// CloseSubscribers removes all subscribers, handlers, and closes the amqp connection
func CloseSubscribers() {
lock.Lock()
defer lock.Unlock()
subscribersStarted = false
Subscribers = nil
Handlers = nil
if _connection != nil {
c := _connection
_connection = nil
c.Close()
}
}
//DeleteQueue does what it says, deletes a queue in rabbit
func DeleteQueue(s Subscriber) error {
conn := connection()
channel, err := conn.Channel()
if err != nil {
return errors.New("Can't delete a queue: can't create a channel")
}
return deleteQueue(channel, &s)
}
// PrefixQueueInDev will prefix the queue name with the name of your current user if of the APP_ENV variable is set
// to a non production value ("production", "prod", "staging", "stage").
// This is used for running a worker in your local environment but connecting to a stage
// or prodution rabbit server.
func (subscriber *Subscriber) PrefixQueueInDev() {
env := appEnv()
if !IsDevelopmentEnv() {
return
}
username := currentUsersName()
if env == "test" {
username = "test_" + username
}
subscriber.Queue = fmt.Sprintf("%s_%s", username, subscriber.Queue)
}
// AutoDeleteInDev will set the Subscribers AutoDelete setting to true as long as you are in a development environement.
// Non production environements have a APP_ENV value that isn't ("production", "prod", "staging", "stage").
// This is used for running a worker in your local environment but connecting to a stage
// or prodution rabbit server. You want to ensure the Subscriber gets AutoDeleted on the remote server.
func (subscriber *Subscriber) AutoDeleteInDev() {
if IsDevelopmentEnv() {
subscriber.AutoDelete = true
}
}
// IsDevelopmentEnv tells you if you are currently running in a development environment
func IsDevelopmentEnv() bool {
env := appEnv()
return !stringInSlice(env, nonDevEnvironments)
}
func appEnv() string {
env := os.Getenv("APP_ENV")
// Check PLATFORM_ENV for backwards compatibility
if len(env) == 0 {
env = os.Getenv("PLATFORM_ENV")
}
return env
}
func currentUsersName() string {
username := "unknown"
if userData, err := user.Current(); err == nil {
username = userData.Username
}
return username
}