Module: Sidekiq::LockableJob::LockService

Defined in:
lib/sidekiq/lockable_job/lock_service.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

REDIS_PREFIX_KEY =
self.to_s.downcase

Class Method Summary collapse

Class Method Details

.handle_locked_by(keys, worker_instance:, job:) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/sidekiq/lockable_job/lock_service.rb', line 21

def self.handle_locked_by(keys, worker_instance:, job:)
  keys = [keys] unless keys.nil? || keys.is_a?(Array)
  keys&.each do |key|
    raise LockedJobError.new("Locked by #{key}") if locked?(key)
  end
  # job is not locked and should be processed
  false
end

.lock(key) ⇒ Object



9
10
11
# File 'lib/sidekiq/lockable_job/lock_service.rb', line 9

def self.lock(key)
  redis.set(redis_key(key), Time.now.to_i)
end

.locked?(key) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/sidekiq/lockable_job/lock_service.rb', line 17

def self.locked?(key)
  redis.exists(redis_key(key))
end

.redisObject



34
35
36
# File 'lib/sidekiq/lockable_job/lock_service.rb', line 34

def self.redis
  $redis
end

.redis_key(key) ⇒ Object



30
31
32
# File 'lib/sidekiq/lockable_job/lock_service.rb', line 30

def self.redis_key(key)
  "#{REDIS_PREFIX_KEY}:#{key}"
end

.unlock(key) ⇒ Object



13
14
15
# File 'lib/sidekiq/lockable_job/lock_service.rb', line 13

def self.unlock(key)
  redis.del(redis_key(key))
end