class DynportTools::RedisQ

Constants

DEFAULTS

Attributes

redis[RW]
redis_key[RW]
retry_count[RW]

Public Class Methods

new(redis_key, options = {}) click to toggle source
# File lib/dynport_tools/redis_q.rb, line 5
def initialize(redis_key, options = {})
  DEFAULTS.merge(options).merge(:redis_key => redis_key).each do |key, value|
    self.send(:"#{key}=", value) if self.respond_to?(:"#{key}=")
  end
end

Public Instance Methods

count() click to toggle source
# File lib/dynport_tools/redis_q.rb, line 39
def count
  redis.zcard(redis_key)
end
each(options = {}) { |batch_size ? to_a.sort_by { |a| last }.map { |a| first } : keys.first| ... } click to toggle source
# File lib/dynport_tools/redis_q.rb, line 54
def each(options = {})
  entries_with_errors = []
  stats = { :errors => {}, :ok => [] }
  batch_size = options[:batch_size].to_i if options[:batch_size].to_i > 0
  while (result_hash = pop(batch_size || 1)).any?
    begin
      yield(batch_size ? result_hash.to_a.sort_by { |a| a.last }.reverse.map { |a| a.first } : result_hash.keys.first)
      stats[:ok] << result_hash.keys.join(",")
    rescue => err
      stats[:errors][result_hash.keys.join(",")] = ([err.message] + err.backtrace[0,5]).join("\n")
      entries_with_errors << result_hash if mark_failed(result_hash.keys.join(",")) < retry_count
    end
  end
  push_many(entries_with_errors, :failed => true) if entries_with_errors.any?
  stats
end
failed_key() click to toggle source
# File lib/dynport_tools/redis_q.rb, line 75
def failed_key
  "#{redis_key}/failed_counts"
end
failed_tries() click to toggle source
# File lib/dynport_tools/redis_q.rb, line 50
def failed_tries
  @failed_tries ||= Hash.new(0)
end
mark_failed(id) click to toggle source
# File lib/dynport_tools/redis_q.rb, line 71
def mark_failed(id)
  redis.zincrby(failed_key, 1, id).to_i
end
nil_or_lower?(a, b) click to toggle source
# File lib/dynport_tools/redis_q.rb, line 31
def nil_or_lower?(a, b)
  a.nil? || a.to_i < b
end
pop(number = 1) click to toggle source
# File lib/dynport_tools/redis_q.rb, line 43
def pop(number = 1)
  Hash[*redis.multi do
    redis.zrevrange(redis_key, 0, number - 1, :with_scores => true)
    redis.zremrangebyrank(redis_key, 0 - number, -1)
  end.first]
end
priority_of(id) click to toggle source
# File lib/dynport_tools/redis_q.rb, line 35
def priority_of(id)
  redis.zscore(redis_key, id)
end
push(id, priority = nil, options = {}) click to toggle source
# File lib/dynport_tools/redis_q.rb, line 11
def push(id, priority = nil, options = {})
  priority ||= Time.now.to_i * -1
  if nil_or_lower?(priority_of(id), priority)
    redis.multi if !options[:no_multi]
    redis.zrem(failed_key, id) if !options[:failed]
    redis.zadd(redis_key, priority, id)
    redis.exec if !options[:no_multi]
  end
end
push_many(array, options = {}) click to toggle source
# File lib/dynport_tools/redis_q.rb, line 21
def push_many(array, options = {})
  redis.multi do
    array.each do | (id, popularity) |
      (id.is_a?(Hash) ? id : { id => popularity }).each do |id2, popularity2|
        push(id2, popularity2, options.merge(:no_multi => true))
      end
    end
  end
end