Class: Backport::Machine
- Inherits:
-
Object
- Object
- Backport::Machine
- Defined in:
- lib/backport/machine.rb
Overview
The Backport server controller.
Instance Method Summary collapse
-
#initialize ⇒ Machine
constructor
A new instance of Machine.
-
#prepare(server) ⇒ void
Add a server to the machine.
-
#run ⇒ void
Run the machine.
- #servers ⇒ Array<Server::Base>
-
#stop ⇒ void
Stop the machine.
-
#stopped? ⇒ Boolean
True if the machine is stopped.
-
#tick ⇒ void
Update the machine’s servers.
Constructor Details
#initialize ⇒ Machine
Returns a new instance of Machine.
5 6 7 |
# File 'lib/backport/machine.rb', line 5 def initialize @stopped = true end |
Instance Method Details
#prepare(server) ⇒ void
This method returns an undefined value.
Add a server to the machine. The server will be started when the machine starts. If the machine is already running, the server will be started immediately.
43 44 45 46 |
# File 'lib/backport/machine.rb', line 43 def prepare server servers.push server server.start unless stopped? end |
#run ⇒ void
This method returns an undefined value.
Run the machine. If a block is provided, it gets executed before the maching starts its main loop. The main loop blocks program execution until the machine is stopped.
14 15 16 17 18 19 20 |
# File 'lib/backport/machine.rb', line 14 def run return unless stopped? servers.clear @stopped = false yield if block_given? run_server_thread end |
#servers ⇒ Array<Server::Base>
49 50 51 |
# File 'lib/backport/machine.rb', line 49 def servers @servers ||= [] end |
#stop ⇒ void
This method returns an undefined value.
Stop the machine.
25 26 27 28 29 |
# File 'lib/backport/machine.rb', line 25 def stop servers.map(&:stop) servers.clear @stopped = true end |
#stopped? ⇒ Boolean
True if the machine is stopped.
33 34 35 |
# File 'lib/backport/machine.rb', line 33 def stopped? @stopped ||= false end |
#tick ⇒ void
This method returns an undefined value.
Update the machine’s servers.
56 57 58 59 60 |
# File 'lib/backport/machine.rb', line 56 def tick servers.delete_if(&:stopped?) stop if servers.empty? servers.each(&:tick) end |