Move RunnerConnection into class Runner

The old approach was to require the runner connection. This did
not work anymore with Zeitwerk in Rails 6. @sebastian.serth and I
moved the Connection class in `lib` into the ActiveRecord class
`Runner`. This will also work with future changes like specific
error classes. Furthermore the config was fixed and simplified.

Co-authored-by: Sebastian Serth <Sebastian.Serth@hpi.de>
This commit is contained in:
Felix Auringer
2021-05-25 10:47:49 +02:00
committed by Sebastian Serth
parent 63d997a7e3
commit 8d968e01e6
4 changed files with 8 additions and 15 deletions

View File

@ -1,7 +1,5 @@
# frozen_string_literal: true
require 'runner/runner_connection'
class Runner < ApplicationRecord
BASE_URL = CodeOcean::Config.new(:code_ocean).read[:runner_management][:url]
HEADERS = {'Content-Type' => 'application/json'}.freeze
@ -18,7 +16,7 @@ class Runner < ApplicationRecord
def self.for(user, exercise)
execution_environment = ExecutionEnvironment.find(exercise.execution_environment_id)
runner = Runner.find_or_create_by(user: user, execution_environment: execution_environment)
runner = find_or_create_by(user: user, execution_environment: execution_environment)
return runner if runner.save
@ -51,7 +49,7 @@ class Runner < ApplicationRecord
starting_time = Time.zone.now
websocket_url = execute_command(command)[:websocketUrl]
EventMachine.run do
socket = RunnerConnection.new(websocket_url)
socket = Runner::Connection.new(websocket_url)
yield(self, socket) if block_given?
end
Time.zone.now - starting_time # execution time
@ -70,7 +68,7 @@ class Runner < ApplicationRecord
def new_runner
url = "#{BASE_URL}/runners"
time_limit = CodeOcean::Config.new(:code_ocean)[:runner_management][:unused_runner_expiration_time]
time_limit = CodeOcean::Config.new(:code_ocean).read[:runner_management][:unused_runner_expiration_time]
body = {executionEnvironmentId: execution_environment.id, timeLimit: time_limit}
response = Faraday.post(url, body.to_json, HEADERS)
response_body = parse response