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.2.0'
Instance Method Summary collapse
-
#enqueue(klass, *args) ⇒ Object
This method can be used to conveniently add a job to a queue.
-
#info ⇒ Object
Returns a hash, similar to redis-rb’s #info, of interesting stats.
-
#keys ⇒ Object
Returns an array of all known Resque keys in Redis.
-
#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.
-
#peek(queue, start = 0, count = 1) ⇒ Object
Returns an array of items currently queued.
-
#pop(queue) ⇒ Object
Pops a job off a queue.
-
#push(queue, item) ⇒ Object
Pushes a job onto a queue.
-
#queues ⇒ Object
Returns an array of all known Resque queues as strings.
-
#redis ⇒ Object
Returns the current Redis connection.
-
#redis=(server) ⇒ Object
Accepts a ‘hostname:port’ string or a Redis server.
-
#remove_queue(queue) ⇒ Object
Given a queue name, completely deletes the queue.
-
#reserve(queue) ⇒ Object
This method will return a ‘Resque::Job` object or a non-true value depending on whether a job can be obtained.
-
#size(queue) ⇒ Object
Returns an int representing the size of a queue.
- #to_s ⇒ Object
-
#watch_queue(queue) ⇒ Object
Used internally to keep track of which queues we’ve created.
-
#workers ⇒ Object
A shortcut to Worker.all.
-
#working ⇒ Object
A shortcut to Worker.working.
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.
137 138 139 140 141 |
# File 'lib/resque.rb', line 137 def enqueue(klass, *args) queue = klass.instance_variable_get(:@queue) queue ||= klass.queue if klass.respond_to?(:queue) Job.create(queue, klass, *args) end |
#info ⇒ Object
Returns a hash, similar to redis-rb’s #info, of interesting stats.
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/resque.rb', line 173 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 |
#keys ⇒ Object
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.
187 188 189 190 191 |
# File 'lib/resque.rb', line 187 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 |
#queues ⇒ Object
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 |
#redis ⇒ Object
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 |
#remove_queue(queue) ⇒ Object
Given a queue name, completely deletes the queue.
104 105 106 107 108 109 |
# File 'lib/resque.rb', line 104 def remove_queue(queue) @watched_queues ||= {} @watched_queues.delete(queue.to_s) redis.srem(:queues, queue.to_s) redis.del("queue:#{queue}") 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.
148 149 150 |
# File 'lib/resque.rb', line 148 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_s ⇒ Object
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.
113 114 115 116 117 |
# File 'lib/resque.rb', line 113 def watch_queue(queue) @watched_queues ||= {} return if @watched_queues[queue] redis.sadd(:queues, queue.to_s) end |