module Irrc::Runner

Public Instance Methods

run(threads) click to toggle source
# File lib/irrc/runner.rb, line 3
def run(threads)
  done = []

  loop do
    # Trick to avoid dead lock
    if last_thread_of?(threads) && @queue.empty?
      terminate

      # Queue guard objects notifying other threads to return results
      logger.debug "Queue #{threads - 1} guard objects"
      (threads - 1).times { @queue.push nil }

      return done
    end

    query = @queue.pop

    # Trick to avoid dead lock
    if query.nil?
      terminate
      return done
    end

    connect unless established?

    begin
      logger.info "Processing #{query.object}"
      query = process(query)
      query.success
      logger.debug "Queue new #{query.children.size} queries"
      query.children.each {|q| @queue << q }
    rescue
      logger.error "#{$!.message} when processing #{query.object} for #{query.root.object}"
      query.fail
    end

    done << query if query.root?
  end
end

Private Instance Methods

cache(object, sources) { || ... } click to toggle source
# File lib/irrc/runner.rb, line 46
def cache(object, sources, &block)
  @cache["#{object}:#{sources}"] ||= yield
end
execute(command) click to toggle source
# File lib/irrc/runner.rb, line 50
def execute(command)
  return if command.nil? || command == ''

  logger.debug %(Executing "#{command}")

  result = connection.cmd(command)
  logger.debug %(Got "#{result}")

  result.gsub(/#.*$/, '')  # Trim comments
end
last_thread_of?(num_threads) click to toggle source
# File lib/irrc/runner.rb, line 61
def last_thread_of?(num_threads)
  return @queue.num_waiting == num_threads - 1
end
terminate() click to toggle source
# File lib/irrc/runner.rb, line 65
def terminate
  logger.info "No more queries"
  close
end