Class: Resque::One::QueueLocker

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

Constant Summary collapse

PREFIX =
'resque-one'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, queue) ⇒ QueueLocker

Returns a new instance of QueueLocker.



9
10
11
12
# File 'lib/resque/one/queue_locker.rb', line 9

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

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



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

def queue
  @queue
end

#redisObject (readonly)

Returns the value of attribute redis.



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

def redis
  @redis
end

Instance Method Details

#lock(job_info) ⇒ Object



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

def lock(job_info)
  return false if locked? job_info

  job_key = key_for job_info
  redis.set job_key, job_info.id

  true
end

#locked?(job_info) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/resque/one/queue_locker.rb', line 14

def locked?(job_info)
  redis.keys(key_for(job_info)).any?
end

#unlock(job_info) ⇒ Object



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

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

#unlock_all(klass = nil) ⇒ Object



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

def unlock_all(klass=nil)
  filter = klass ? "#{queue_key}:#{klass.to_s}:*" : "#{queue_key}:*"
  redis.keys(filter).each do |key|
    redis.del key
  end
end