Skip to content

Commit

Permalink
Merge pull request #25 from keithwiersema/add-network-error-handling-…
Browse files Browse the repository at this point in the history
…in-start-node

add network error handling to Events::StartNode
  • Loading branch information
sti111oveu authored Sep 25, 2017
2 parents d95fcb4 + f6ccd1f commit 821ad85
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/backbeat/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions lib/backbeat/workers/async_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion spec/unit/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion spec/unit/workers/async_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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

Expand Down

0 comments on commit 821ad85

Please sign in to comment.