class Coursemology::Evaluator::Client

Public Class Methods

initialize(host, api_user_email, api_token) click to toggle source
# File lib/coursemology/evaluator/client.rb, line 3
def self.initialize(host, api_user_email, api_token)
  Coursemology::Evaluator::Models::Base.base_url = host
  Coursemology::Evaluator::Models::Base.api_user_email = api_user_email
  Coursemology::Evaluator::Models::Base.api_token = api_token

  Coursemology::Evaluator::Models::Base.initialize
end
new(one_shot = false) click to toggle source

@param [Boolean] one_shot If the client should only fire one request.

# File lib/coursemology/evaluator/client.rb, line 12
def initialize(one_shot = false)
  @terminate = one_shot
end

Public Instance Methods

run() click to toggle source
# File lib/coursemology/evaluator/client.rb, line 16
def run
  Signal.trap('SIGTERM', method(:on_sig_term))
  loop(&method(:client_loop))
end

Private Instance Methods

allocate_evaluations() click to toggle source

Requests evaluations from the server.

@return [Array<Coursemology::Evaluator::Models::ProgrammingEvaluation>] The evaluations

retrieved from the server.
# File lib/coursemology/evaluator/client.rb, line 45
def allocate_evaluations
  ActiveSupport::Notifications.instrument('allocate.client.evaluator.coursemology') do
    languages = Coursemology::Polyglot::Language.concrete_languages.map(&:display_name)
    Coursemology::Evaluator::Models::ProgrammingEvaluation.allocate(language: languages)
  end
rescue Flexirest::HTTPUnauthorisedClientException => e
  ActiveSupport::Notifications.publish('allocate_fail.client.evaluator.coursemology', e: e)
  nil
end
client_loop() click to toggle source

Performs one iteration of the client loop.

# File lib/coursemology/evaluator/client.rb, line 24
def client_loop
  evaluations = allocate_evaluations
  if evaluations && !evaluations.empty?
    on_allocate(evaluations)
  else
    raise StopIteration if @terminate

    # :nocov:
    # This sleep might not be triggered in the specs, because interruptions to the thread is
    # nondeterministically run by the OS scheduler.
    sleep(Coursemology::Evaluator.config.poll_interval)
    # :nocov:
  end

  raise StopIteration if @terminate
end
on_allocate(evaluations) click to toggle source

The callback for handling an array of allocated evaluations.

@param [Array<Coursemology::Evaluator::Models::ProgrammingEvaluation>] evaluations The

evaluations retrieved from the server.
# File lib/coursemology/evaluator/client.rb, line 59
def on_allocate(evaluations)
  evaluations.each do |evaluation|
    on_evaluation(evaluation)
  end
end
on_evaluation(evaluation) click to toggle source

The callback for handling an evaluation.

@param [Coursemology::Evaluator::Models::ProgrammingEvaluation] evaluation The evaluation

retrieved from the server.
# File lib/coursemology/evaluator/client.rb, line 69
def on_evaluation(evaluation)
  ActiveSupport::Notifications.instrument('evaluate.client.evaluator.coursemology',
                                          evaluation: evaluation) do
    evaluation.evaluate
  end

  ActiveSupport::Notifications.instrument('save.client.evaluator.coursemology') do
    evaluation.save
  end
end
on_sig_term() click to toggle source

The callback for handling SIGTERM sent to the process.

# File lib/coursemology/evaluator/client.rb, line 81
def on_sig_term
  @terminate = true
end