Class: HelloGoodbye::ForemenManager

Inherits:
Foreman
  • Object
show all
Defined in:
lib/hello_goodbye/foremen_manager.rb

Constant Summary collapse

DEFAULT_MANAGER_PORT =
8080

Constants inherited from Foreman

HelloGoodbye::Foreman::DEFAULT_SERVER

Instance Attribute Summary

Attributes inherited from Foreman

#console, #foreman_started, #my_id, #server, #server_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Foreman

console_type, default_server, #employ, #my_name, #running?, set_console_type, #start, #start_console, #start_with_reactor, #status, #status=, #stop, #unemploy

Constructor Details

#initialize(options = {}) ⇒ ForemenManager

Returns a new instance of ForemenManager.



14
15
16
17
18
19
# File 'lib/hello_goodbye/foremen_manager.rb', line 14

def initialize(options={})
  @next_foreman_id = -1
  options.map do |key,value|
    self.send("#{key}=",value) if self.respond_to?("#{key}=".to_sym)
  end
end

Class Method Details

.default_manager_portObject



10
11
12
# File 'lib/hello_goodbye/foremen_manager.rb', line 10

def self.default_manager_port
  DEFAULT_MANAGER_PORT
end

Instance Method Details

#foremenObject



25
26
27
# File 'lib/hello_goodbye/foremen_manager.rb', line 25

def foremen
  @foremen ||= []
end

#next_foreman_idObject



29
30
31
# File 'lib/hello_goodbye/foremen_manager.rb', line 29

def next_foreman_id
  @next_foreman_id += 1
end

#on_error(&block) ⇒ Object



52
53
54
55
56
# File 'lib/hello_goodbye/foremen_manager.rb', line 52

def on_error(&block)
  EM::error_handler do |e|
    block.call
  end
end

#portObject



21
22
23
# File 'lib/hello_goodbye/foremen_manager.rb', line 21

def port
  @port || DEFAULT_MANAGER_PORT
end

#register_foreman(foreman_hash) ⇒ Object

Registers a new forman Parameters:

  • foreman_hash

    :port => (int) Port number to listen on.
    :class => A reference to the foreman class
              that will handle the connection and spawn
              workers.
    


40
41
42
# File 'lib/hello_goodbye/foremen_manager.rb', line 40

def register_foreman(foreman_hash)
  self.foremen << foreman_hash.merge(:reference => nil, :id => next_foreman_id)
end

#reportObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hello_goodbye/foremen_manager.rb', line 91

def report
  [].tap do |r|
    self.foremen.each do |foreman|
      r << {
        :id =>  foreman[:id],
        :name => foreman[:reference].my_name,
        :status => foreman[:reference].status.to_s
      }
    end
  end
end

#start!Object

Starts the manager console and all the registered foremen.



46
47
48
49
50
# File 'lib/hello_goodbye/foremen_manager.rb', line 46

def start!
  super do
    self.start_foremen
  end
end

#start_foremenObject

Start the consoles for all foremen. This does not start employing workers.



60
61
62
63
64
65
66
# File 'lib/hello_goodbye/foremen_manager.rb', line 60

def start_foremen
  self.foremen.each do |foreman|
    foreman[:reference] = foreman[:class].new(:server => self.server, 
                                              :port => foreman[:port])
    foreman[:reference].start!
  end
end

#trigger_foreman(mode, id) ⇒ Object

Trigger all formen to either start or stop employing workers. Parameters:

  • mode => :start or :stop

  • id => The foreman’s integer ID or name



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hello_goodbye/foremen_manager.rb', line 72

def trigger_foreman(mode,id)
  [].tap do |toggled_foremen|
    self.foremen.each do |foreman|
      if foreman[:reference].nil? || (id != "all" && foreman[:reference].my_name != id && foreman[:id] != id.to_i)
        next
      end
      next if mode == :start && foreman[:reference].running?
      next if mode == :stop && !foreman[:reference].running?

      if mode == :start
        foreman[:reference].employ
      else
        foreman[:reference].unemploy
      end
      toggled_foremen << foreman[:id]
    end
  end
end