Skip to content

Commit 8aa3f4c

Browse files
committed
reset connection and log on connection error
1 parent 849a0fa commit 8aa3f4c

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

chef/lib/chef/index_queue/amqp_client.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ def disconnected!
7373
end
7474

7575
def send_action(action, data)
76-
exchange.publish({"action" => action.to_s, "payload" => data}.to_json)
76+
begin
77+
exchange.publish({"action" => action.to_s, "payload" => data}.to_json)
78+
rescue Bunny::ServerDownError, Bunny::ConnectionError, Errno::ECONNRESET => e
79+
Chef::Log.error("Disconnected from the AMQP Broker, cannot queue data to the indexer")
80+
disconnected!
81+
raise e
82+
end
7783
end
7884

7985
private

chef/spec/unit/index_queue_spec.rb

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ def @amqp_client.connected?; false; end # stubbing predicate methods not working
147147
@publisher.reset!
148148
end
149149

150+
after do
151+
@publisher.disconnected!
152+
end
153+
150154
it "is a singleton" do
151155
lambda {Chef::IndexQueue::Indexable::AmqpClient.new}.should raise_error
152156
end
@@ -174,11 +178,35 @@ def @amqp_client.connected?; false; end # stubbing predicate methods not working
174178
@publisher.exchange.should == @exchange
175179
end
176180

177-
it "publishes an action to the exchange" do
178-
@amqp_client.stub!(:qos)
179-
data = {"some_data" => "in_a_hash"}
180-
@exchange.should_receive(:publish).with({"action" => "hot_chef_on_queue", "payload" => data}.to_json)
181-
@publisher.send_action(:hot_chef_on_queue, data)
181+
describe "publishing" do
182+
before do
183+
@amqp_client.stub!(:qos)
184+
@data = {"some_data" => "in_a_hash"}
185+
end
186+
187+
it "publishes an action to the exchange" do
188+
@exchange.should_receive(:publish).with({"action" => "hot_chef_on_queue", "payload" => @data}.to_json)
189+
@publisher.send_action(:hot_chef_on_queue, @data)
190+
end
191+
192+
it "resets the client upon a Bunny::ServerDownError when publishing" do
193+
@exchange.should_receive(:publish).and_raise(Bunny::ServerDownError)
194+
@publisher.should_receive(:disconnected!).twice
195+
lambda {@publisher.send_action(:hot_chef_on_queue, @data)}.should raise_error(Bunny::ServerDownError)
196+
end
197+
198+
it "resets the client upon a Bunny::ConnectionError when publishing" do
199+
@exchange.should_receive(:publish).and_raise(Bunny::ConnectionError)
200+
@publisher.should_receive(:disconnected!).twice
201+
lambda {@publisher.send_action(:hot_chef_on_queue, @data)}.should raise_error(Bunny::ConnectionError)
202+
end
203+
204+
it "resets the client upon a Errno::ECONNRESET when publishing" do
205+
@exchange.should_receive(:publish).and_raise(Errno::ECONNRESET)
206+
@publisher.should_receive(:disconnected!).twice
207+
lambda {@publisher.send_action(:hot_chef_on_queue, @data)}.should raise_error(Errno::ECONNRESET)
208+
end
209+
182210
end
183211

184212
it "creates a queue bound to its exchange with a temporary UUID" do

0 commit comments

Comments
 (0)