Class: RobotController::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/robot-controller/robots.rb

Constant Summary collapse

ROBOT_INSTANCE_MAX =

maximum number of processes a single robot can have

16

Class Method Summary collapse

Class Method Details

.instances_valid?(n) ⇒ Boolean

validates that the instances value is within range, e.g.,

instances_valid?(1) == 1
instances_valid?(16) == 16
instances_valid?(0) == 1             # out of range low, enforce minimum
instances_valid?(99) => RuntimeError # out of range high, error out

Returns:



43
44
45
46
# File 'lib/robot-controller/robots.rb', line 43

def instances_valid?(n)
  fail "TooManyInstances: #{n} > #{ROBOT_INSTANCE_MAX}" if n > ROBOT_INSTANCE_MAX
  (n < 1) ? 1 : n
end

.load(robots_fn, dir = 'config/environments', host = nil) ⇒ Object

main entry point



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/robot-controller/robots.rb', line 12

def load(robots_fn, dir = 'config/environments', host = nil)
  # Validate parameters
  robots_fn = File.join(dir, robots_fn) if dir
  fail "FileNotFound: #{robots_fn}" unless File.file?(robots_fn)

  # read the YAML file with the configuration of all the robots to run
  robots =  YAML.load_file(robots_fn)

  # determine current host if not provided
  host ||= `hostname -s`.strip

  # if the config lists this specific host, use it;
  # else check to see if '*' is a matching host
  unless robots.include?(host)
    if robots.include?('*')
      host = '*'
    else
      fail "HostMismatch: #{host} not defined in #{robots_fn}"
    end
  end

  # parse the host-specific YAML configuration
  parse_robots_configuration(robots[host])
end

.parse_lanes(lanes_spec) ⇒ Object

parse the lane values designator using the following syntax:

parse_lanes('') == ['default']
parse_lanes(' ') == ['default']
parse_lanes(' , ') == ['default']
parse_lanes(' , ,') == ['default']
parse_lanes('*') == ['*']
parse_lanes('1') == ['1']
parse_lanes('A') == ['A']
parse_lanes('A , B') == ['A', 'B']
parse_lanes('A,B,C') == ['A','B','C']
parse_lanes('A-C,E') == ['A-C', 'E']


60
61
62
63
# File 'lib/robot-controller/robots.rb', line 60

def parse_lanes(lanes_spec)
  lanes = lanes_spec.split(/,/).collect(&:strip).uniq
  lanes.join('') == '' ? ['default'] : lanes
end

.parse_robots_configuration(robots) ⇒ Array<Hash>

parse YAML lines for host where line is robot[:lane]

Returns:

  • [

    robot: 'foo',
    queues: ['foo_default'],
    n: 2
    

    , … ]



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/robot-controller/robots.rb', line 81

def parse_robots_configuration(robots)
  [].tap do |r|
    robots.each do |line|
      robot = line.split(/:/).collect(&:strip)
      robot.each do |j|
        fail "SyntaxError: '#{line}' is missing arguments" if j.strip == ''
      end

      # add defaults
      robot << 'default' if robot.size == 1
      robot << '1' if robot.size == 2

      # build queues for robot instances
      fail "SyntaxError: '#{line}' is missing arguments" unless robot.size == 3
      robot[2] = instances_valid?(robot[2].to_i)
      queues = queue_names(robot[0], robot[1])

      r << { robot: robot[0], queues: queues, n: robot[2] }
    end
  end
end

.queue_names(robot, lanes) ⇒ Object

generate the queue names for all given lanes, e.g.,

queue_names('z','A') => ['z_A']
queue_names('z','A,C') => ['z_A', 'z_C']


69
70
71
# File 'lib/robot-controller/robots.rb', line 69

def queue_names(robot, lanes)
  parse_lanes(lanes).collect { |lane| robot + '_' + lane }
end