Class: Judges::Update

Inherits:
Object show all
Defined in:
lib/judges/commands/update.rb

Overview

The update command.

This class is instantiated by the bin/judge command line interface. You are not supposed to instantiate it yourself.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(loog) ⇒ Update

Initialize.

Parameters:

  • loog (Loog)

    Logging facility



33
34
35
36
# File 'lib/judges/commands/update.rb', line 33

def initialize(loog)
  @loog = loog
  @start = Time.now
end

Instance Method Details

#run(opts, args) ⇒ Object

Run the update command (called by the bin/judges script).

Parameters:

  • opts (Hash)

    Command line options (start with ‘–’)

  • args (Array)

    List of command line arguments

Raises:

  • (RuntimeError)

    If not exactly two arguments provided or directory is missing



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/judges/commands/update.rb', line 42

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)
  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(timeout: opts['timeout']&.to_i, lifetime: opts['lifetime']&.to_i)
  if options.lifetime && options.timeout && options.lifetime < options.timeout * 1.1
    raise "The --timeout=#{options.timeout} must be at least 10 percent smaller than --lifetime=#{options.lifetime}"
  end
  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,
    epoch: @epoch, shuffle: opts['shuffle'], boost: opts['boost'],
    demote: opts['demote'], seed: opts['seed']
  )
  begin
    Timeout.timeout(opts['lifetime']) do
      loop_them(impex, judges, fb, opts, options)
    end
  rescue Timeout::Error, Timeout::ExitException => e
    @loog.error("Terminated due to --lifetime=#{opts['lifetime']}")
    raise e unless opts['quiet']
    @loog.info("Had to stop due to the --lifetime=#{opts['lifetime']}")
  ensure
    impex.export(fb)
  end
end