From 3045201eea405550e6548a1a84bb8bacda2b6ee7 Mon Sep 17 00:00:00 2001 From: "Ben Sheldon [he/him]" Date: Mon, 23 Aug 2021 19:29:19 -0700 Subject: [PATCH] Add temporary additional lock on good_jobs-[active_job_id] (#336) --- lib/good_job/job.rb | 8 +++++++- spec/integration/scheduler_spec.rb | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/good_job/job.rb b/lib/good_job/job.rb index f2a2d101f..55bca204f 100644 --- a/lib/good_job/job.rb +++ b/lib/good_job/job.rb @@ -189,7 +189,13 @@ def self.perform_with_advisory_lock break if good_job.blank? break :unlocked unless good_job&.executable? - good_job.perform + begin + good_job.with_advisory_lock(key: "good_jobs-#{good_job.active_job_id}") do + good_job.perform + end + rescue RecordAlreadyAdvisoryLockedError => e + ExecutionResult.new(value: nil, handled_error: e) + end end end diff --git a/spec/integration/scheduler_spec.rb b/spec/integration/scheduler_spec.rb index f551d56ea..e9be1705d 100644 --- a/spec/integration/scheduler_spec.rb +++ b/spec/integration/scheduler_spec.rb @@ -18,8 +18,10 @@ def perform(*_args, **_kwargs) thread_name = Thread.current.name || Thread.current.object_id + expected_locks_per_thread = 2 # TODO: Temporary value for GoodJob v1.99. Should be reduced to 1 for GoodJob 2.0. + locks_count = PgLock.advisory_lock.owns.count - if locks_count > 1 + if locks_count > expected_locks_per_thread puts "Thread #{thread_name} owns #{locks_count} locks." puts "GoodJobs locked by this connection:"