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:"