@@ -61,7 +61,7 @@ func WithClient(c *amqp.Connection) BrokerOption {
6161
6262// NewBroker creates a broker backed by RabbitMQ.
6363// By default, it connects to localhost.
64- func NewBroker (options ... BrokerOption ) * Broker {
64+ func NewBroker (options ... BrokerOption ) ( * Broker , error ) {
6565 br := Broker {
6666 amqpUri : DefaultAmqpUri ,
6767 receiveTimeout : DefaultReceiveTimeout * time .Second ,
@@ -75,32 +75,34 @@ func NewBroker(options ...BrokerOption) *Broker {
7575 if br .conn == nil {
7676 conn , err := amqp .Dial (br .amqpUri )
7777 if err != nil {
78- log .Panicf ("Failed to connect to RabbitMQ: %s" , err )
79- return nil
78+ return nil , err
8079 }
80+
8181 br .conn = conn
8282 }
8383
8484 channel , err := br .conn .Channel ()
8585 if err != nil {
86- log .Panicf ("Failed to open a channel: %s" , err )
87- return nil
86+ return nil , err
8887 }
88+
8989 br .channel = channel
9090
91- return & br
91+ return & br , nil
9292}
9393
9494// Send inserts the specified message at the head of the queue.
9595// Note, the method is safe to call concurrently.
9696func (br * Broker ) Send (m []byte , q string ) error {
97- var headers map [string ]interface {}
98- var body []byte
99- var contentType string
100- var contentEncoding string
101- var deliveryMode uint8
102- var correlationId string
103- var replyTo string
97+ var (
98+ headers map [string ]interface {}
99+ body []byte
100+ contentType string
101+ contentEncoding string
102+ deliveryMode uint8
103+ correlationId string
104+ replyTo string
105+ )
104106
105107 if br .rawMode {
106108 headers = make (amqp.Table )
@@ -131,7 +133,7 @@ func (br *Broker) Send(m []byte, q string) error {
131133 replyTo = properties_in ["reply_to" ].(string )
132134 }
133135
134- err := br .channel .Publish (
136+ return br .channel .Publish (
135137 "" , // exchange
136138 q , // routing key
137139 false , // mandatory
@@ -145,21 +147,22 @@ func (br *Broker) Send(m []byte, q string) error {
145147 ReplyTo : replyTo ,
146148 Body : body ,
147149 })
148-
149- return err
150150}
151151
152152// Observe sets the queues from which the tasks should be received.
153153// Note, the method is not concurrency safe.
154- func (br * Broker ) Observe (queues []string ) {
154+ func (br * Broker ) Observe (queues []string ) error {
155155 br .queues = queues
156- for _ , queue := range queues {
157- durable := true
158- autoDelete := false
159- exclusive := false
160- noWait := false
161156
157+ var (
158+ durable = true
159+ autoDelete = false
160+ exclusive = false
161+ noWait = false
162+ )
163+ for _ , queue := range queues {
162164 // Check whether the queue exists.
165+ // If the queue doesn't exist, attempt to create it.
163166 _ , err := br .channel .QueueDeclarePassive (
164167 queue ,
165168 durable ,
@@ -168,32 +171,33 @@ func (br *Broker) Observe(queues []string) {
168171 noWait ,
169172 nil ,
170173 )
171-
172- // If the queue doesn't exist, attempt to create it.
173174 if err != nil {
174- // QueueDeclarePassive() will close the channel if the queue does not exist, so we have to create a new channel when this happens.
175+ // QueueDeclarePassive() will close the channel if the queue does not exist,
176+ // so we have to create a new channel when this happens.
175177 if br .channel .IsClosed () {
176178 channel , err := br .conn .Channel ()
177179 if err != nil {
178- log . Panicf ( "Failed to open a channel: %s" , err )
180+ return err
179181 }
182+
180183 br .channel = channel
181184 }
182185
183- _ , err : = br .channel .QueueDeclare (
186+ _ , err = br .channel .QueueDeclare (
184187 queue ,
185188 durable ,
186189 autoDelete ,
187190 exclusive ,
188191 noWait ,
189192 nil ,
190193 )
191-
192194 if err != nil {
193- log . Panicf ( "Failed to declare a queue: %s" , err )
195+ return err
194196 }
195197 }
196198 }
199+
200+ return nil
197201}
198202
199203// Receive fetches a Celery task message from a tail of one of the queues in RabbitMQ.
@@ -205,8 +209,8 @@ func (br *Broker) Receive() ([]byte, error) {
205209
206210 var err error
207211
208- delivery , delivery_exists := br .delivery [queue ]
209- if ! delivery_exists {
212+ delivery , deliveryExists := br .delivery [queue ]
213+ if ! deliveryExists {
210214 delivery , err = br .channel .Consume (
211215 queue , // queue
212216 "" , // consumer
@@ -216,7 +220,6 @@ func (br *Broker) Receive() ([]byte, error) {
216220 false , // noWait
217221 nil , // args
218222 )
219-
220223 if err != nil {
221224 return nil , err
222225 }
0 commit comments