Class: HelloGoodbye::Foreman

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

Direct Known Subclasses

ForemenManager

Constant Summary collapse

DEFAULT_SERVER =
"127.0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options={})
  self.server = options[:server]
  self.port = options[:port]

  @foreman_started = false
end

Instance Attribute Details

#consoleObject

Returns the value of attribute console.



4
5
6
# File 'lib/hello_goodbye/foreman.rb', line 4

def console
  @console
end

#foreman_startedObject (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_idObject

Returns the value of attribute my_id.



4
5
6
# File 'lib/hello_goodbye/foreman.rb', line 4

def my_id
  @my_id
end

#portObject

Returns the value of attribute port.



4
5
6
# File 'lib/hello_goodbye/foreman.rb', line 4

def port
  @port
end

#serverObject

Returns the value of attribute server.



4
5
6
# File 'lib/hello_goodbye/foreman.rb', line 4

def server
  @server
end

#server_idObject

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_typeObject

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_serverObject



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

#employObject

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_nameObject

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

Returns:

  • (Boolean)


78
79
80
# File 'lib/hello_goodbye/foreman.rb', line 78

def running?
  self.status == :running
end

#startObject

Starts the foreman’s worker spawning action.

Raises:

  • (ArgumentError)


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.

Raises:

  • (RuntimeError)


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_consoleObject



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

#statusObject

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

#stopObject

Stops the foreman’s worker spawning action.

Raises:

  • (ArgumentError)


41
42
43
# File 'lib/hello_goodbye/foreman.rb', line 41

def stop
  raise ArgumentError, "Foreman.start must be implemented by child class."
end

#unemployObject

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