class Hive::Register

Central register of devices and workers in the hive

Attributes

controllers[R]

Public Class Methods

new() click to toggle source
# File lib/hive/register.rb, line 9
def initialize
  @controllers = []
  @devices = {}
  @max_devices = 5 # TODO Add to configuration file
  if Hive.config.ports?
    @port_allocator = Hive::PortAllocator.new(minimum: Hive.config.ports.minimum, maximum: Hive.config.ports.maximum)
  else
    @port_allocator = Hive::PortAllocator.new(ports: [])
  end
end

Public Instance Methods

check_controllers() click to toggle source
# File lib/hive/register.rb, line 60
def check_controllers
  Hive.logger.debug("Devices before update: #{@devices.inspect}")
  new_device_list = {}
  @controllers.each do |c|
    begin
      new_device_list[c.class] = []
      @devices[c.class] = [] if ! @devices.has_key?(c.class)
      Hive.logger.info("Checking controller #{c.class}")
      c.detect.each do |device|
        Hive.logger.debug("Found #{device.inspect}")
        i = @devices[c.class].find_index(device)
        if i
          @devices[c.class][i].status = device.status
          new_device_list[c.class] << @devices[c.class][i]
        else
          device.port_allocator = @port_allocator.allocate_port_range(c.port_range_size)
          new_device_list[c.class] << device
        end
      end
      Hive.logger.debug("new_device_list: #{new_device_list.inspect}")

      # Remove any devices that have not been rediscovered
      (@devices[c.class] - new_device_list[c.class]).each do |d|
        if d.stop
          @port_allocator.release_port_range(d.port_allocator)
          @devices[c.class].delete(d)
        end
      end

      # Add any new devices
      (new_device_list[c.class] - @devices[c.class]).each do |d|
        @devices[c.class] << d
      end
      # Check that all known devices have running workers
      @devices[c.class].each do |d|
        if d.claimed?
          d.stop if d.running?
        else
          d.start if ! d.running?
        end
      end
    rescue Hive::Controller::DeviceDetectionFailed
      Hive.logger.warn("Failed to detect devices for #{c.class}")
    end
  end
  Hive.logger.debug("Devices after update: #{@devices.inspect}")
end
clear_workspaces() click to toggle source
# File lib/hive/register.rb, line 117
def clear_workspaces
  candidates = Dir.glob("#{Hive.config.logging.home}/*")
    .select{ |f|
      File.directory?(f) \
      && File.exists?("#{f}/job_info") \
      && File.read("#{f}/job_info").chomp.to_s =~ /completed/
    }.sort_by{ |f|
      File.mtime(f)
    }.reverse
  if candidates && candidates.length > Hive.config.logging.homes_to_keep
    candidates[Hive.config.logging.homes_to_keep..-1].each do |dir|
      Hive.logger.info("Found (and deleting) #{dir}")
      FileUtils.rm_rf(dir)
    end
  end
end
devices() click to toggle source
# File lib/hive/register.rb, line 20
def devices
  list = []
  @devices.each do |controller, device_list|
    list.concat(device_list)
  end
  list
end
housekeeping() click to toggle source
# File lib/hive/register.rb, line 108
def housekeeping
  clear_workspaces

  if Hive.config.timings.stats_update_interval? && @next_stat_update < Time.now
    Hive.send_statistics
    @next_stat_update += Hive.config.timings.stats_update_interval
  end
end
instantiate_controllers(controller_details = Hive.config.controllers) click to toggle source
# File lib/hive/register.rb, line 32
def instantiate_controllers(controller_details = Hive.config.controllers)
  if controller_details
    controller_details.each do |type, opts|
      Hive.logger.info("Adding controller for '#{type}'")
      require "hive/controller/#{type}"
      controller = Object.const_get('Hive').const_get('Controller').const_get(type.capitalize).new(opts.to_hash)
      @controllers << controller
    end
  end
  check_controllers
  @controllers
end
run() click to toggle source
# File lib/hive/register.rb, line 45
def run
  @next_stat_update = Time.now
  loop do
    Hive.poll
    housekeeping
    check_controllers
    sleep Hive.config.timings.controller_loop_interval

    # For the moment, clear Hive Mind logs each time
    # TODO Something better so that warnings and errors are not hidden
    Hive.logger.clear({component:  Hive.logger.default_progname, level: Hive.config.logging.hm_logs_to_delete})
    Hive.logger.debug('Hive Mind log cleared')
  end
end
worker_pids() click to toggle source
# File lib/hive/register.rb, line 28
def worker_pids
  self.devices.collect{ |d| d.worker_pid }.compact
end