class Resque::Plugins::ResqueSliders::Commander

Attributes

stale_hosts[R]

Hosts that have config data (queues and values), but the host is not running the daemon.

Public Class Methods

new() click to toggle source
# File lib/resque-sliders/commander.rb, line 11
def initialize
  @host_status = redis_get_hash(host_config_key)
  @stale_hosts = Resque.redis.smembers(known_hosts_key) - hosts
end

Public Instance Methods

all_hosts() click to toggle source

Array of all hosts (current + stale)

# File lib/resque-sliders/commander.rb, line 27
def all_hosts
  (hosts + stale_hosts).sort
end
change(host, queue, quantity) click to toggle source

Changes queues to quantiy for host. Returns boolean.

# File lib/resque-sliders/commander.rb, line 67
def change(host, queue, quantity)
  # queue is sanitized by:
  # replacing punctuation with spaces, strip end spaces, split on remaining whitespace, and join again on comma.
  queue2 = queue.gsub(/['":]/, '').strip.gsub(/\s+/, ',').split(/, */).reject { |x| x.nil? or x.empty? }.join(',')
  raise 'Queue Different' unless queue == queue2
  redis_set_hash("#{key_prefix}:#{host}", queue2, quantity) unless queue2.empty?
end
current_children(host) click to toggle source

Return current children count or nil if Host hasn’t registered itself.

# File lib/resque-sliders/commander.rb, line 44
def current_children(host)
  @host_status["#{host}:current_children"].to_i if max_children(host)
end
delete(host, queue) click to toggle source

Deletes queue for host. Returns boolean.

# File lib/resque-sliders/commander.rb, line 77
def delete(host, queue)
  redis_del_hash("#{key_prefix}:#{host}", queue)
end
hosts() click to toggle source

Return Array of currently online hosts

# File lib/resque-sliders/commander.rb, line 17
def hosts
  Set.new.tap do |l|
    @host_status.keys.each do |x|
      x = x.split(':')
      l << x.first unless %w(reload pause stop).include?(x.last)
    end
  end.to_a.sort
end
max_children(host) click to toggle source

Return max children count or nil if Host hasn’t registered itself.

# File lib/resque-sliders/commander.rb, line 49
def max_children(host)
  max = @host_status["#{host}:max_children"].to_i
  max == 0 ? nil : max # if Max isn't set its not running
end
max_children!(host, count) click to toggle source

Override max_children on host (Dangerous!)

# File lib/resque-sliders/commander.rb, line 55
def max_children!(host, count)
  @hostname = host
  register_setting('max_children', count)
end
queues_on(host) click to toggle source

Return Array of queues on host

# File lib/resque-sliders/commander.rb, line 61
def queues_on(host)
  queue_values(host).keys if all_hosts.include?(host)
end
remove_all_host_keys(hostname) click to toggle source

Remove all keys for a host (clean)

# File lib/resque-sliders/commander.rb, line 32
def remove_all_host_keys(hostname)
  # expensive process O(N)
  keys_to_delete = Resque.redis.keys("#{key_prefix}:*").select { |k| name = k.split(':').last; hostname == name }
  # look at config hash, remove fields if relate to this hostname
  fields_to_delete = redis_get_hash(host_config_key).keys.select { |k| name = k.split(':').first; hostname == name }
  # do delete
  Resque.redis.del(keys_to_delete) unless keys_to_delete.empty?
  redis_del_hash(host_config_key, fields_to_delete) unless fields_to_delete.empty?
  del_from_known_hosts(hostname)
end