Class: Backport::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/backport/machine.rb

Overview

The Backport server controller.

Instance Method Summary collapse

Constructor Details

#initializeMachine

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.

Parameters:



43
44
45
46
# File 'lib/backport/machine.rb', line 43

def prepare server
  servers.push server
  server.start unless stopped?
end

#runvoid

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

#serversArray<Server::Base>

Returns:



49
50
51
# File 'lib/backport/machine.rb', line 49

def servers
  @servers ||= []
end

#stopvoid

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.

Returns:

  • (Boolean)


33
34
35
# File 'lib/backport/machine.rb', line 33

def stopped?
  @stopped ||= false
end

#tickvoid

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