From cc8d50bceedf1ca59fed9b503113d1b639657215 Mon Sep 17 00:00:00 2001 From: Ankitha Damodara Date: Fri, 5 Jul 2024 14:53:12 +0100 Subject: [PATCH] add a prefix to job_id while locking --- lib/que/adapters/active_record_with_lock.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/que/adapters/active_record_with_lock.rb b/lib/que/adapters/active_record_with_lock.rb index dbd6293..1a33378 100644 --- a/lib/que/adapters/active_record_with_lock.rb +++ b/lib/que/adapters/active_record_with_lock.rb @@ -4,6 +4,7 @@ module Que module Adapters class ActiveRecordWithLock < Que::Adapters::ActiveRecord + LOCK_PREFIX = ENV["QUE_LOCK_PREFIX"] || 1111 # this is a random number def initialize(job_connection_pool:, lock_record:) @job_connection_pool = job_connection_pool @lock_record = lock_record @@ -56,18 +57,20 @@ def cleanup! end def pg_try_advisory_lock?(job_id) + lock_variable = "#{LOCK_PREFIX}#{job_id}".to_i lock_database_connection.execute( - "SELECT pg_try_advisory_lock(#{job_id})", + "SELECT pg_try_advisory_lock(#{lock_variable})", ).try(:first)&.fetch("pg_try_advisory_lock") end def unlock_job(job_id) + lock_variable = "#{LOCK_PREFIX}#{job_id}".to_i # If for any reason the connection that is used to get this advisory lock # is corrupted, the lock on this job_id would already be released when the # connection holding the lock goes bad. # Now, if a new connection tries to release the non existing lock this would just no op # by returning false and return a warning "WARNING: you don't own a lock of type ExclusiveLock" - lock_database_connection.execute("SELECT pg_advisory_unlock(#{job_id})") + lock_database_connection.execute("SELECT pg_advisory_unlock(#{lock_variable})") end end end