Class: Resque::Queue

Inherits:
Object
  • Object
show all
Includes:
Mutex_m
Defined in:
lib/resque/queue.rb

Overview

A queue interface that quacks like Queue from Ruby’s stdlib.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, redis, coder = Marshal) ⇒ Queue

Create a new Queue object with name on redis connection, and using the coder for encoding and decoding objects that are stored in redis.



21
22
23
24
25
26
27
28
29
30
# File 'lib/resque/queue.rb', line 21

def initialize name, redis, coder = Marshal
  super()
  @name       = name
  @redis_name = "queue:#{@name}"
  @redis      = redis
  @coder      = coder
  @destroyed  = false

  @redis.sadd(:queues, @name)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/resque/queue.rb', line 16

def name
  @name
end

#redis_nameObject (readonly)

Returns the value of attribute redis_name.



16
17
18
# File 'lib/resque/queue.rb', line 16

def redis_name
  @redis_name
end

Instance Method Details

#decode(object) ⇒ Object



113
114
115
# File 'lib/resque/queue.rb', line 113

def decode object
  @coder.load object
end

#destroyObject

Deletes this Queue from redis. This method is not available on the stdlib Queue.

If there are multiple queue objects of the same name, Queue A and Queue B and you delete Queue A, pushing to Queue B will have unknown side effects. Queue A will be marked destroyed, but Queue B will not.



98
99
100
101
102
# File 'lib/resque/queue.rb', line 98

def destroy
  @redis.del @redis_name
  @redis.srem(:queues, @name)
  @destroyed = true
end

#destroyed?Boolean

returns true if the queue is destroyed and false if it isn’t

Returns:

  • (Boolean)


105
106
107
# File 'lib/resque/queue.rb', line 105

def destroyed?
  @destroyed
end

#empty?Boolean

Is the queue empty?

Returns:

  • (Boolean)


88
89
90
# File 'lib/resque/queue.rb', line 88

def empty?
  size == 0
end

#encode(object) ⇒ Object



109
110
111
# File 'lib/resque/queue.rb', line 109

def encode object
  @coder.dump object
end

#lengthObject Also known as: size

Get the length of the queue



82
83
84
# File 'lib/resque/queue.rb', line 82

def length
  @redis.llen @redis_name
end

#pop(non_block = false) ⇒ Object

Pop an item off the queue. This method will block until an item is available.

Pass true for a non-blocking pop. If nothing is read on a non-blocking pop, a ThreadError is raised.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/resque/queue.rb', line 66

def pop non_block = false
  if non_block
    synchronize do
      value = @redis.lpop(@redis_name)
      raise ThreadError unless value
      decode value
    end
  else
    synchronize do
      value = @redis.blpop(@redis_name, 1) until value
      decode value.last
    end
  end
end

#push(object) ⇒ Object Also known as: <<, enq

Add object to the queue If trying to push to an already destroyed queue, it will raise a Resque::QueueDestroyed exception

Raises:



34
35
36
37
38
39
40
# File 'lib/resque/queue.rb', line 34

def push object
  raise QueueDestroyed if destroyed?

  synchronize do
    @redis.rpush @redis_name, encode(object)
  end
end

#slice(start, length) ⇒ Object

Returns a list of objects in the queue. This method is not available on the stdlib Queue.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/resque/queue.rb', line 47

def slice start, length
  if length == 1
    synchronize do
      decode @redis.lindex @redis_name, start
    end
  else
    synchronize do
      Array(@redis.lrange(@redis_name, start, start + length - 1)).map do |item|
        decode item
      end
    end
  end
end