class ResqueSlidingWindow::WindowSlider

Attributes

args[RW]
klass[RW]

Public Class Methods

new(klass, args) click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 10
def initialize(klass, args)
  self.klass = klass
  self.args = args
end

Public Instance Methods

close() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 25
def close
  redis.del(slide_key)
end
slide() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 15
def slide
  if first?
    mark_first
  elsif should_requeue?
    requeue
  else
    false
  end
end

Private Instance Methods

delayed_key_values() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 112
def delayed_key_values
  delayed_timestamps.inject({}) do |hash, timestamp|
    hash[timestamp_to_key(timestamp)] = get(timestamp_to_key(timestamp))
    hash
  end
end
delayed_timestamps() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 102
def delayed_timestamps
  redis.zrange(:delayed_queue_schedule, 0,-1)
end
first?() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 31
def first?
  !on_queue?
end
get(key) click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 106
def get(key)
  redis.rpop(key).tap { |val|
    redis.rpush(key, val)
  }
end
key_to_timestamp(key) click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 94
def key_to_timestamp(key)
  key.to_s.gsub(/delayed:/, "").to_i
end
keyify(args) click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 78
def keyify(args)
  "(#{args.map { |arg|
    if arg.is_a?(Array)
      keyify(arg)
    elsif arg.respond_to?(:to_a) && !(arg.is_a?(String) || arg.is_a?(Symbol) || arg.is_a?(Numeric))
      keyify(arg.to_a)
    else
      if arg.respond_to?(:to_key)
        arg.to_key
      else
        arg.to_s
      end
    end
  }.join(",").gsub(/\s/,"")})"
end
mark_first() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 35
def mark_first
  mark_time
end
mark_time() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 74
def mark_time
  redis.set(slide_key, Time.now.utc.to_i)
end
matching_delayed() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 50
def matching_delayed
  delayed_key_values.select { |_, value| value == search }
end
max_time() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 62
def max_time
  klass.respond_to?(:max_time) ? klass.public_send(:max_time) : 1.minute
end
maxed_out?() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 66
def maxed_out?
  (redis.get(slide_key).to_i + max_time) <= Time.now.utc.to_i
end
on_queue?() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 58
def on_queue?
  matching_delayed.count > 0
end
requeue() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 39
def requeue
  matching_delayed.each do |key, _|
    remove_delayed_job_from_timestamp(key_to_timestamp(key), klass, *args)
  end
  true
end
should_requeue?() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 46
def should_requeue?
  !maxed_out?
end
slide_key() click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 70
def slide_key
  "sliding:#{klass.name}:#{keyify(args)}:count"
end
timestamp_to_key(timestamp) click to toggle source
# File lib/resque_sliding_window/window_slider.rb, line 98
def timestamp_to_key(timestamp)
  "delayed:#{timestamp}"
end