Class: Resque::Plugins::ResqueSliders::Commander

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/resque-sliders/commander.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#add_to_known_hosts, #check_signal, #del_from_known_hosts, #host_config_key, #key_prefix, #known_hosts_key, #pause?, #queue_values, #redis_del_hash, #redis_get_hash, #redis_get_hash_field, #redis_set_hash, #register_setting, #reload?, #set_signal_flag, #stop?, #unregister_setting

Constructor Details

#initializeCommander

Returns a new instance of Commander.



11
12
13
14
# 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

Instance Attribute Details

#stale_hostsObject (readonly)

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



9
10
11
# File 'lib/resque-sliders/commander.rb', line 9

def stale_hosts
  @stale_hosts
end

Instance Method Details

#all_hostsObject

Array of all hosts (current + stale)



27
28
29
# File 'lib/resque-sliders/commander.rb', line 27

def all_hosts
  (hosts + stale_hosts).sort
end

#change(host, queue, quantity) ⇒ Object

Changes queues to quantiy for host. Returns boolean.



67
68
69
70
71
72
73
# 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) ⇒ Object

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



44
45
46
# 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) ⇒ Object

Deletes queue for host. Returns boolean.



77
78
79
# File 'lib/resque-sliders/commander.rb', line 77

def delete(host, queue)
  redis_del_hash("#{key_prefix}:#{host}", queue)
end

#hostsObject

Return Array of currently online hosts



17
18
19
20
21
22
23
24
# 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) ⇒ Object

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



49
50
51
52
# 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) ⇒ Object

Override max_children on host (Dangerous!)



55
56
57
58
# File 'lib/resque-sliders/commander.rb', line 55

def max_children!(host, count)
  @hostname = host
  register_setting('max_children', count)
end

#queues_on(host) ⇒ Object

Return Array of queues on host



61
62
63
# 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) ⇒ Object

Remove all keys for a host (clean)



32
33
34
35
36
37
38
39
40
41
# 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