module Redisque

Attributes

halting[RW]

Public Instance Methods

connect(options) click to toggle source

Connect to Redis DB Example:

Redisque.connect(:password => 'secret', :db => 1)

Arguments:

options: (Hash)
# File lib/redisque.rb, line 16
def connect(options)
  @options.merge! options    
  @redis = Redis.new(@options)
end
crash_check(queue) click to toggle source

Check if backup queue has any task Arguments:

queue: (String)
# File lib/redisque.rb, line 68
def crash_check(queue)    
  redis.lrange(@backup_queue,  0, -1).each { |unfinished| redis.lpush(queue, unfinished) }
  redis.del(@backup_queue)
end
enqueue(queue, data) click to toggle source

Add task to queue Example:

Redisque.enqueue('tasks', {:id => 1, :email => 'my@email.com'})

Arguments:

queue: (String)
data: (Hash)
# File lib/redisque.rb, line 27
def enqueue(queue, data)
  redis.lpush(queue, JSON(data))
end
process(queue) { |parse| ... } click to toggle source

Process the queue Arguments:

queue: (String)
# File lib/redisque.rb, line 53
def process(queue)
  # wait for new value in `queue` for `timeout` time
  # pop it and copy to bak queue named `*{queue}`
  value = redis.rpoplpush(queue, @backup_queue)
  # if we do have some valid value then launch the worker
  if value
    yield(JSON.parse(value))
    # Remove task from backup queue
    redis.rpop @backup_queue
  end
end
redis(options = @options) click to toggle source

Return Redis connection Arguments:

options: (Hash)
# File lib/redisque.rb, line 46
def redis(options = @options)
  @redis || connect(options) 
end
workoff(queue, pause = 5, &block) click to toggle source

Run along the queue and execute tasks

# File lib/redisque.rb, line 32
def workoff(queue, pause = 5, &block)
  @backup_queue = '*' << queue
  crash_check(queue)
  loop do        
    process(queue, &block)
    break if @halting
    # anyway, we need to get some sleep
    sleep pause 
  end     
end