class Judges::Update

def run(opts, args)

Raises:
  • (RuntimeError) - If not exactly two arguments provided or directory is missing

Parameters:
  • args (Array) -- List of command line arguments
  • opts (Hash) -- Command line options (start with '--')
def run(opts, args)
  raise 'Exactly two arguments required' unless args.size == 2
  dir = args[0]
  raise "The directory is absent: #{dir.to_rel}" unless File.exist?(dir)
  start = Time.now
  impex = Judges::Impex.new(@loog, args[1])
  fb = impex.import(strict: false)
  fb = Factbase::Logged.new(fb, @loog) if opts['log']
  options = Judges::Options.new(opts['option'])
  if opts['options-file']
    options += Judges::Options.new(
      File.readlines(opts['options-file'])
        .compact
        .reject(&:empty?)
        .map { |ln| ln.strip.split('=', 1).map(&:strip).join('=') }
    )
    @loog.debug("Options loaded from #{opts['options-file']}")
  end
  if options.empty?
    @loog.debug('No options provided by the --option flag')
  else
    @loog.debug("The following options provided:\n\t#{options.to_s.gsub("\n", "\n\t")}")
  end
  judges = Judges::Judges.new(dir, opts['lib'], @loog, start:, shuffle: opts['shuffle'], boost: opts['boost'],
                                                       demote: opts['demote'])
  c = 0
  churn = Factbase::Churn.new
  errors = []
  sum = fb.query('(eq what "judges-summary")').each.to_a
  if sum.empty?
    @loog.info('Summary fact not found') unless opts['summary'] == 'off'
  else
    @loog.info("Summary fact found:\n\t#{Factbase::FactAsYaml.new(sum.first).to_s.gsub("\n", "\n\t")}")
  end
  if !sum.empty? && opts['summary'] == 'add' && fb.query('(eq what "judges-summary")').delete!
    @loog.info('Summary fact deleted')
  end
  elapsed(@loog, level: Logger::INFO) do
    loop do
      c += 1
      if c > 1
        if opts['lifetime'] && Time.now - @start > opts['lifetime']
          @loog.info("Not starting cycle ##{c}, no time left")
          c -= 1
          break
        end
        @loog.info("\nStarting cycle ##{c}#{" (out of #{opts['max-cycles']})" if opts['max-cycles']}...")
      end
      delta = cycle(opts, judges, fb, options, start, errors)
      churn += delta
      impex.export(fb)
      if delta.zero?
        @loog.info("The update cycle ##{c} has made no changes to the factbase, let's stop")
        break
      end
      if !opts['max-cycles'].nil? && c >= opts['max-cycles']
        @loog.info("Too many cycles already, as set by --max-cycles=#{opts['max-cycles']}, breaking")
        break
      end
      if opts['fail-fast'] && !errors.empty?
        @loog.info("Due to #{errors.count} errors we must stop at the update cycle ##{c}")
        break
      end
      @loog.info("The cycle #{c} did #{delta}")
    end
    throw :"👍 Update completed in #{c} cycle(s), did #{churn}"
  end
  return unless %w[add append].include?(opts['summary'])
  summarize(fb, churn, errors, start, c)
  impex.export(fb)
end