Class: HelloGoodbye::Foreman
- Inherits:
-
Object
- Object
- HelloGoodbye::Foreman
- Defined in:
- lib/hello_goodbye/foreman.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_SERVER =
"127.0.0.1"
Instance Attribute Summary collapse
-
#console ⇒ Object
Returns the value of attribute console.
-
#foreman_started ⇒ Object
readonly
Returns the value of attribute foreman_started.
-
#my_id ⇒ Object
Returns the value of attribute my_id.
-
#port ⇒ Object
Returns the value of attribute port.
-
#server ⇒ Object
Returns the value of attribute server.
-
#server_id ⇒ Object
Returns the value of attribute server_id.
Class Method Summary collapse
-
.console_type ⇒ Object
Returns the current console type for this class.
- .default_server ⇒ Object
-
.set_console_type(console_sym) ⇒ Object
Overrides the default ForemanConsole console type to fire up when #start! is called.
Instance Method Summary collapse
-
#employ ⇒ Object
Sets the foreman status to :running and calls self.start.
-
#initialize(options = {}) ⇒ Foreman
constructor
Parameters: options: * server => * port => The port to run the server on.
-
#my_name ⇒ Object
Detects the name to report this foreman class as.
-
#running? ⇒ Boolean
true if the foreman is currently employing workers.
-
#start ⇒ Object
Starts the foreman’s worker spawning action.
-
#start! ⇒ Object
Starts the console for the foreman.
- #start_console ⇒ Object
- #start_with_reactor(&block) ⇒ Object
-
#status ⇒ Object
Reports the current status of the foreman.
-
#status=(status) ⇒ Object
Sets the foreman status to either :running or :stopped.
-
#stop ⇒ Object
Stops the foreman’s worker spawning action.
-
#unemploy ⇒ Object
Sets the foreman status to :stopped and calls self.stop.
Constructor Details
#initialize(options = {}) ⇒ Foreman
Parameters: options:
* server =>
* port => The port to run the server on
28 29 30 31 32 33 |
# File 'lib/hello_goodbye/foreman.rb', line 28 def initialize(={}) self.server = [:server] self.port = [:port] @foreman_started = false end |
Instance Attribute Details
#console ⇒ Object
Returns the value of attribute console.
4 5 6 |
# File 'lib/hello_goodbye/foreman.rb', line 4 def console @console end |
#foreman_started ⇒ Object (readonly)
Returns the value of attribute foreman_started.
5 6 7 |
# File 'lib/hello_goodbye/foreman.rb', line 5 def foreman_started @foreman_started end |
#my_id ⇒ Object
Returns the value of attribute my_id.
4 5 6 |
# File 'lib/hello_goodbye/foreman.rb', line 4 def my_id @my_id end |
#port ⇒ Object
Returns the value of attribute port.
4 5 6 |
# File 'lib/hello_goodbye/foreman.rb', line 4 def port @port end |
#server ⇒ Object
Returns the value of attribute server.
4 5 6 |
# File 'lib/hello_goodbye/foreman.rb', line 4 def server @server end |
#server_id ⇒ Object
Returns the value of attribute server_id.
4 5 6 |
# File 'lib/hello_goodbye/foreman.rb', line 4 def server_id @server_id end |
Class Method Details
.console_type ⇒ Object
Returns the current console type for this class.
16 17 18 |
# File 'lib/hello_goodbye/foreman.rb', line 16 def self.console_type @console_type ||= :foreman end |
.default_server ⇒ Object
20 21 22 |
# File 'lib/hello_goodbye/foreman.rb', line 20 def self.default_server DEFAULT_SERVER end |
.set_console_type(console_sym) ⇒ Object
Overrides the default ForemanConsole console type to fire up when #start! is called.
11 12 13 |
# File 'lib/hello_goodbye/foreman.rb', line 11 def self.set_console_type(console_sym) @console_type = console_sym end |
Instance Method Details
#employ ⇒ Object
Sets the foreman status to :running and calls self.start
88 89 90 91 |
# File 'lib/hello_goodbye/foreman.rb', line 88 def employ self.start self.status = :running end |
#my_name ⇒ Object
Detects the name to report this foreman class as. For example, will be “test” for “HelloGoodbye::TestForeman”.
61 62 63 64 65 66 67 |
# File 'lib/hello_goodbye/foreman.rb', line 61 def my_name begin self.class.name.match(/^HelloGoodbye::(.*)Foreman$/)[1].downcase rescue self.class.name end end |
#running? ⇒ Boolean
true if the foreman is currently employing workers
78 79 80 |
# File 'lib/hello_goodbye/foreman.rb', line 78 def running? self.status == :running end |
#start ⇒ Object
Starts the foreman’s worker spawning action.
36 37 38 |
# File 'lib/hello_goodbye/foreman.rb', line 36 def start raise ArgumentError, "Foreman.start must be implemented by child class." end |
#start! ⇒ Object
Starts the console for the foreman. Subclasses should implement this method, passing a block to super to start up any tasks (AMQB subscriber, etc) that may need to be done.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hello_goodbye/foreman.rb', line 48 def start! raise RuntimeError, "Foreman already started!" if @foreman_started == true start_with_reactor do self.start_console yield if block_given? end @foreman_started = true end |
#start_console ⇒ Object
103 104 105 106 107 108 |
# File 'lib/hello_goodbye/foreman.rb', line 103 def start_console me = self self.server_id = EM::start_server(self.server, self.port, Console.get(self.class.console_type)) do |c| c.foreman = me end end |
#start_with_reactor(&block) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/hello_goodbye/foreman.rb', line 110 def start_with_reactor(&block) if EM.reactor_running? block.call else EM.run { block.call } end end |
#status ⇒ Object
Reports the current status of the foreman. Returns:
* :stopped if the foreman is not currently employing workers.
* :running if the foreman is active
73 74 75 |
# File 'lib/hello_goodbye/foreman.rb', line 73 def status @status || :stopped end |
#status=(status) ⇒ Object
Sets the foreman status to either :running or :stopped
83 84 85 |
# File 'lib/hello_goodbye/foreman.rb', line 83 def status=(status) @status = status.to_sym end |
#stop ⇒ Object
Stops the foreman’s worker spawning action.
41 42 43 |
# File 'lib/hello_goodbye/foreman.rb', line 41 def stop raise ArgumentError, "Foreman.start must be implemented by child class." end |
#unemploy ⇒ Object
Sets the foreman status to :stopped and calls self.stop
94 95 96 97 |
# File 'lib/hello_goodbye/foreman.rb', line 94 def unemploy self.stop self.status = :stopped end |