Module: Resque

Extended by:
Resque
Includes:
Helpers
Included in:
Resque
Defined in:
lib/resque.rb,
lib/resque/job.rb,
lib/resque/stat.rb,
lib/resque/errors.rb,
lib/resque/server.rb,
lib/resque/worker.rb,
lib/resque/failure.rb,
lib/resque/helpers.rb,
lib/resque/version.rb,
lib/resque/failure/base.rb,
lib/resque/failure/redis.rb,
lib/resque/failure/hoptoad.rb

Defined Under Namespace

Modules: Failure, Helpers, Stat Classes: Job, NoClassError, NoQueueError, Server, Worker

Constant Summary collapse

Version =
'1.1.0'

Instance Method Summary collapse

Methods included from Helpers

#classify, #constantize, #decode, #encode

Instance Method Details

#enqueue(klass, *args) ⇒ Object

This method can be used to conveniently add a job to a queue. It assumes the class you’re passing it is a real Ruby class (not a string or reference) which either:

a) has a @queue ivar set
b) responds to `queue`

If either of those conditions are met, it will use the value obtained from performing one of the above operations to determine the queue.

If no queue can be inferred this method will return a non-true value.

This method is considered part of the ‘stable` API.



129
130
131
132
133
# File 'lib/resque.rb', line 129

def enqueue(klass, *args)
  queue = klass.instance_variable_get(:@queue)
  queue ||= klass.queue if klass.respond_to?(:queue)
  Job.create(queue, klass, *args)
end

#infoObject

Returns a hash, similar to redis-rb’s #info, of interesting stats.



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/resque.rb', line 165

def info
  return {
    :pending   => queues.inject(0) { |m,k| m + size(k) },
    :processed => Stat[:processed],
    :queues    => queues.size,
    :workers   => workers.size.to_i,
    :working   => working.size,
    :failed    => Stat[:failed],
    :servers   => [redis.server]
  }
end

#keysObject

Returns an array of all known Resque keys in Redis. Redis’ KEYS operation is O(N) for the keyspace, so be careful - this can be slow for big databases.



179
180
181
182
183
# File 'lib/resque.rb', line 179

def keys
  redis.keys("*").map do |key|
    key.sub('resque:', '')
  end
end

#list_range(key, start = 0, count = 1) ⇒ Object

Does the dirty work of fetching a range of items from a Redis list and converting them into Ruby objects.



88
89
90
91
92
93
94
95
96
# File 'lib/resque.rb', line 88

def list_range(key, start = 0, count = 1)
  if count == 1
    decode redis.lindex(key, start)
  else
    Array(redis.lrange(key, start, start+count-1)).map do |item|
      decode item
    end
  end
end

#peek(queue, start = 0, count = 1) ⇒ Object

Returns an array of items currently queued. Queue name should be a string.

start and count should be integer and can be used for pagination. start is the item to begin, count is how many items to return.

To get the 3rd page of a 30 item, paginatied list one would use:

Resque.peek('my_list', 59, 30)


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

def peek(queue, start = 0, count = 1)
  list_range("queue:#{queue}", start, count)
end

#pop(queue) ⇒ Object

Pops a job off a queue. Queue name should be a string.

Returns a Ruby object.



64
65
66
# File 'lib/resque.rb', line 64

def pop(queue)
  decode redis.lpop("queue:#{queue}")
end

#push(queue, item) ⇒ Object

Pushes a job onto a queue. Queue name should be a string and the item should be any JSON-able Ruby object.



56
57
58
59
# File 'lib/resque.rb', line 56

def push(queue, item)
  watch_queue(queue)
  redis.rpush "queue:#{queue}", encode(item)
end

#queuesObject

Returns an array of all known Resque queues as strings.



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

def queues
  redis.smembers(:queues)
end

#redisObject

Returns the current Redis connection. If none has been created, will create a new one.



39
40
41
42
43
# File 'lib/resque.rb', line 39

def redis
  return @redis if @redis
  self.redis = 'localhost:6379'
  self.redis
end

#redis=(server) ⇒ Object

Accepts a ‘hostname:port’ string or a Redis server.



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

def redis=(server)
  case server
  when String
    host, port = server.split(':')
    redis = Redis.new(:host => host, :port => port, :thread_safe => true)
    @redis = Redis::Namespace.new(:resque, :redis => redis)
  when Redis
    @redis = Redis::Namespace.new(:resque, :redis => server)
  else
    raise "I don't know what to do with #{server.inspect}"
  end
end

#reserve(queue) ⇒ Object

This method will return a ‘Resque::Job` object or a non-true value depending on whether a job can be obtained. You should pass it the precise name of a queue: case matters.

This method is considered part of the ‘stable` API.



140
141
142
# File 'lib/resque.rb', line 140

def reserve(queue)
  Job.reserve(queue)
end

#size(queue) ⇒ Object

Returns an int representing the size of a queue. Queue name should be a string.



70
71
72
# File 'lib/resque.rb', line 70

def size(queue)
  redis.llen("queue:#{queue}").to_i
end

#to_sObject



45
46
47
# File 'lib/resque.rb', line 45

def to_s
  "Resque Client connected to #{redis.server}"
end

#watch_queue(queue) ⇒ Object

Used internally to keep track of which queues we’ve created. Don’t call this directly.



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

def watch_queue(queue)
  @watched_queues ||= {}
  return if @watched_queues[queue]
  redis.sadd(:queues, queue.to_s)
end

#workersObject

A shortcut to Worker.all



150
151
152
# File 'lib/resque.rb', line 150

def workers
  Worker.all
end

#workingObject

A shortcut to Worker.working



155
156
157
# File 'lib/resque.rb', line 155

def working
  Worker.working
end