Skip to content

Commit

Permalink
add additional comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithads committed Jul 5, 2024
1 parent adbbcb6 commit c291312
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/que/adapters/active_record_with_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ def checkout_activerecord_adapter(&block)
end

def lock_database_connection
if Thread.current[:db_connection]
return Thread.current[:db_connection] if Thread.current[:db_connection].active?
end
# We are storing this in thread variable here to make sure
# same connection is used to acquire and release the advisory locks.
# Advisory lock will not be released if any other connection from the
# pool tries to release the lock
Thread.current[:db_connection] ||= @lock_record.connection
Thread.current[:db_connection] = @lock_record.connection
end

def execute(command, params=[])
Expand Down Expand Up @@ -51,14 +54,19 @@ def lock_job_with_lock_database(queue, cursor)

def cleanup!
@job_connection_pool.release_connection
@lock_record.release_connection
@lock_record.remove_connection
end

def pg_try_advisory_lock?(job_id)
lock_database_connection.execute("SELECT pg_try_advisory_lock(#{job_id})").try(:first)&.fetch('pg_try_advisory_lock')
end

def unlock_job(job_id)
# 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})")
end
end
Expand Down

0 comments on commit c291312

Please sign in to comment.