Skip to content

Commit

Permalink
Merge pull request #68 from gocardless/handle-connection-timeouts
Browse files Browse the repository at this point in the history
Do not kill the worker thread on connection timeouts
  • Loading branch information
ivgiuliani authored Apr 23, 2021
2 parents 2964682 + 872d49d commit b7a0484
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/que/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
module Que
module Adapters
class ActiveRecord < Base
AR_UNAVAILABLE_CONNECTION_ERRORS = [
::ActiveRecord::ConnectionTimeoutError,
::ActiveRecord::ConnectionNotEstablished,
].freeze

def checkout
checkout_activerecord_adapter { |conn| yield conn.raw_connection }
rescue ::ActiveRecord::ConnectionTimeoutError => e
raise UnavailableConnection
rescue *AR_UNAVAILABLE_CONNECTION_ERRORS => e
raise UnavailableConnection.new(e)
end

def wake_worker_after_commit
Expand Down
15 changes: 13 additions & 2 deletions spec/lib/que/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,24 @@
end
end

context "when we can't checkout a new connection" do
context "when we time out checking out a new connection" do
it "rescues it and returns an error" do
FakeJob.enqueue(1)

expect(Que).
to receive(:execute).with(:lock_job, ["default", 0]).
and_raise(ActiveRecord::ConnectionTimeoutError)
and_raise(ActiveRecord::ConnectionTimeoutError)
expect(subject).to eq(:postgres_error)
end
end

context "when we can't connect to postgres" do
it "rescues it and returns an error" do
FakeJob.enqueue(1)

expect(Que).
to receive(:execute).with(:lock_job, ["default", 0]).
and_raise(ActiveRecord::ConnectionNotEstablished)
expect(subject).to eq(:postgres_error)
end
end
Expand Down

0 comments on commit b7a0484

Please sign in to comment.