Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/models/solid_queue/recurring_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def enqueue(at:)
end
rescue RecurringExecution::AlreadyRecorded
payload[:skipped] = true
# The execution for this task and run time was already recorded by another
# thread or process. Lookup the existing execution so we can still expose
# the Active Job identifier in the instrumentation payload. This allows
# consumers (and our test-suite) to reliably correlate the event with the
# previously enqueued job even when it is reported as skipped.
if (existing_execution = SolidQueue::RecurringExecution.find_by(task_key: key, run_at: at))
payload[:active_job_id] = existing_execution.job&.active_job_id
end
false
rescue Job::EnqueueError => error
payload[:enqueue_error] = error.message
Expand Down
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