Skip to content
1 change: 1 addition & 0 deletions lib/solid_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module SolidQueue
mattr_accessor :preserve_finished_jobs, default: true
mattr_accessor :clear_finished_jobs_after, default: 1.day
mattr_accessor :default_concurrency_control_period, default: 3.minutes
mattr_accessor :clear_connections_after_job, default: false

delegate :on_start, :on_stop, :on_exit, to: Supervisor

Expand Down
1 change: 1 addition & 0 deletions lib/solid_queue/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def post(execution)
wrap_in_app_executor do
thread_execution.perform
ensure
ActiveRecord::Base.clear_active_connections! if SolidQueue.clear_connections_after_job
available_threads.increment
mutex.synchronize { on_idle.try(:call) if idle? }
end
Expand Down
39 changes: 39 additions & 0 deletions test/unit/connection_clearing_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "test_helper"
require "active_support/testing/method_call_assertions"

class ConnectionClearingTest < ActiveSupport::TestCase
include ActiveSupport::Testing::MethodCallAssertions
include JobsTestHelper

self.use_transactional_tests = false

test "clears ActiveRecord connections when flag enabled" do
old_flag, SolidQueue.clear_connections_after_job = SolidQueue.clear_connections_after_job, true

ActiveRecord::Base.expects(:clear_active_connections!).at_least_once

AddToBufferJob.perform_later "clear"

worker = SolidQueue::Worker.new(queues: "background", threads: 1, polling_interval: 0.1)
worker.start
wait_for_jobs_to_finish_for(2.seconds)
worker.stop
ensure
SolidQueue.clear_connections_after_job = old_flag
end

test "does not clear ActiveRecord connections when flag disabled" do
old_flag, SolidQueue.clear_connections_after_job = SolidQueue.clear_connections_after_job, false

ActiveRecord::Base.expects(:clear_active_connections!).never

AddToBufferJob.perform_later "noclear"

worker = SolidQueue::Worker.new(queues: "background", threads: 1, polling_interval: 0.1)
worker.start
wait_for_jobs_to_finish_for(2.seconds)
worker.stop
ensure
SolidQueue.clear_connections_after_job = old_flag
end
end
Loading