diff --git a/lib/backbeat/events.rb b/lib/backbeat/events.rb index e7d1437..cc71d14 100644 --- a/lib/backbeat/events.rb +++ b/lib/backbeat/events.rb @@ -85,6 +85,12 @@ def call(node) else Server.fire_event(ClientComplete, node) end + rescue NetworkError => e + Kernel.sleep(Config.options[:connection_error_wait]) + if (node.reload.current_client_status != :complete) + response = { error: { message: e.message } } + Server.fire_event(ClientError.new(response), node) + end rescue HttpError => e response = { error: { message: e.message } } Server.fire_event(ClientError.new(response), node) diff --git a/lib/backbeat/workers/async_worker.rb b/lib/backbeat/workers/async_worker.rb index 3123a2f..bebc352 100644 --- a/lib/backbeat/workers/async_worker.rb +++ b/lib/backbeat/workers/async_worker.rb @@ -76,11 +76,6 @@ def business_perform(event_class, node_data, options) rescue DeserializeError => e error(status: :deserialize_node_error, node: node_data["node_id"], error: e, backtrace: e.backtrace) raise e - rescue NetworkError => e - Kernel.sleep(Config.options[:connection_error_wait]) - if (node.reload.current_client_status != :complete) - handle_processing_error(e, event_class, node, options) - end rescue => e handle_processing_error(e, event_class, node, options) end diff --git a/spec/unit/events_spec.rb b/spec/unit/events_spec.rb index 52dcaf3..4f64a3a 100644 --- a/spec/unit/events_spec.rb +++ b/spec/unit/events_spec.rb @@ -168,13 +168,23 @@ Backbeat::Events::StartNode.call(node) end - it "fires the ClientError event if the client call fails" do + it "fires the ClientError event if the client call fails with an HttpError" do allow(Backbeat::Client).to receive(:perform).with(node) { raise Backbeat::HttpError.new("Failed", {}) } expect(Backbeat::Server).to receive(:fire_event).with(Backbeat::Events::ClientError, node) Backbeat::Events::StartNode.call(node) end + + it "waits for ClientComplete event if the client call fails with a NetworkError" do + allow(Backbeat::Client).to receive(:perform).with(node) { raise Backbeat::NetworkError.new("Errno::ECONNRESET", {}) } + allow(Backbeat::Config).to receive(:options).and_return({connection_error_wait: 0.01}) + + expect(Kernel).to receive(:sleep).with(0.01) + expect(Backbeat::Server).to receive(:fire_event).with(Backbeat::Events::ClientError, node) + + Backbeat::Events::StartNode.call(node) + end end context "without client action" do diff --git a/spec/unit/workers/async_worker_spec.rb b/spec/unit/workers/async_worker_spec.rb index 4670300..48f9c71 100644 --- a/spec/unit/workers/async_worker_spec.rb +++ b/spec/unit/workers/async_worker_spec.rb @@ -79,7 +79,7 @@ expect(Backbeat::Workers::AsyncWorker.jobs.count).to eq(1) end - it "waits for a client complete response if the server receives a connection reset" do + it "waits for a client complete response if the server receives a connection reset and transitions to server status retrying and client status errored" do node = FactoryGirl.create(:node, user: user, workflow: workflow, current_server_status: :started, current_client_status: :ready) allow(HTTParty).to receive(:post) { raise Errno::ECONNRESET } @@ -93,6 +93,10 @@ { "retries" => 1 } ) + node = node.reload + expect(node.current_server_status).to eq(:retrying) + expect(node.current_client_status).to eq(:errored) + expect(Backbeat::Workers::AsyncWorker.jobs.count).to eq(1) end