Class: ActiveJob::Uniqueness::LockManager

Inherits:
Redlock::Client
  • Object
show all
Defined in:
lib/active_job/uniqueness/lock_manager.rb

Overview

Redlock requires a value of the lock to release the resource by Redlock::Client#unlock method. LockManager introduces LockManager#delete_lock to unlock by resource key only. See github.com/leandromoreira/redlock-rb/issues/51 for more details.

Constant Summary collapse

DELETE_LOCKS_SCAN_COUNT =
1000

Instance Method Summary collapse

Instance Method Details

#delete_lock(resource) ⇒ Object

Unlocks a resource by resource only.



10
11
12
13
14
15
16
17
18
# File 'lib/active_job/uniqueness/lock_manager.rb', line 10

def delete_lock(resource)
  @servers.each do |server|
    synced_redis_connection(server) do |conn|
      conn.call('DEL', resource)
    end
  end

  true
end

#delete_locks(wildcard) ⇒ Object

Unlocks multiple resources by key wildcard.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_job/uniqueness/lock_manager.rb', line 23

def delete_locks(wildcard)
  @servers.each do |server|
    synced_redis_connection(server) do |conn|
      cursor = 0
      while cursor != '0'
        cursor, keys = conn.call('SCAN', cursor, 'MATCH', wildcard, 'COUNT', DELETE_LOCKS_SCAN_COUNT)
        conn.call('UNLINK', *keys) unless keys.empty?
      end
    end
  end

  true
end