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

#check_signal, #host_config_key, #key_prefix, #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.keys("#{key_prefix}:*").map { |x| y = x.split(':').last; y unless x == host_config_key or hosts.include?(y) }.compact.sort
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)



22
23
24
# File 'lib/resque-sliders/commander.rb', line 22

def all_hosts
  (hosts + stale_hosts).sort
end

#change(host, queue, quantity) ⇒ Object

Changes queues to quantiy for host. Returns boolean.



50
51
52
53
54
55
56
57
# File 'lib/resque-sliders/commander.rb', line 50

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.
  # FIXME
  queue2 = queue.downcase.gsub(/[^a-z 0-9,_\-\*]/, '').strip.split(/, */).reject {|x| (x.length == 1 and %w(- _).include?(x)) 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.



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

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.



61
62
63
# File 'lib/resque-sliders/commander.rb', line 61

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

#hostsObject

Return Array of currently online hosts



17
18
19
# File 'lib/resque-sliders/commander.rb', line 17

def hosts
  @host_status.keys.select { |x| x unless %w(reload pause stop).include? x.split(':').last }.map { |x| x.split(':').first }.uniq.sort
end

#max_children(host) ⇒ Object

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



32
33
34
35
# File 'lib/resque-sliders/commander.rb', line 32

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!)



38
39
40
41
# File 'lib/resque-sliders/commander.rb', line 38

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

#queues_on(host) ⇒ Object

Return Array of queues on host



44
45
46
# File 'lib/resque-sliders/commander.rb', line 44

def queues_on(host)
  queue_values(host).keys if all_hosts.include?(host)
end