Class: Resque::One::QueueLocker

Inherits:
Object
  • Object
show all
Defined in:
lib/resque/one/queue_locker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, queue) ⇒ QueueLocker

Returns a new instance of QueueLocker.



7
8
9
10
# File 'lib/resque/one/queue_locker.rb', line 7

def initialize(redis, queue)
  @redis = redis
  @queue = queue
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



5
6
7
# File 'lib/resque/one/queue_locker.rb', line 5

def queue
  @queue
end

#redisObject (readonly)

Returns the value of attribute redis.



5
6
7
# File 'lib/resque/one/queue_locker.rb', line 5

def redis
  @redis
end

Instance Method Details

#lock(job_info) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/resque/one/queue_locker.rb', line 16

def lock(job_info)
  return false if locked? job_info

  job_key = key_for job_info
  redis.set job_key, job_info.id
  redis.expire job_key, Resque::One.lock_ttl if Resque::One.lock_ttl

  true
end

#locked?(job_info) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/resque/one/queue_locker.rb', line 12

def locked?(job_info)
  !redis.get(key_for(job_info)).nil?
end

#unlock(job_info) ⇒ Object



26
27
28
# File 'lib/resque/one/queue_locker.rb', line 26

def unlock(job_info)
  redis.del key_for(job_info)
end

#unlock_all(klass = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/resque/one/queue_locker.rb', line 30

def unlock_all(klass=nil)
  filter = klass ? "#{queue_key}:#{klass.to_s}:*" : "#{queue_key}:*"
  redis.scan_each(match: filter, count: Resque::One.scan_count) do |key|
    redis.del key
  end
end