Rename API routes
This commit is contained in:

committed by
Sebastian Serth

parent
c36ec447ff
commit
cf1e4d6edf
@ -161,9 +161,9 @@ class SubmissionsController < ApplicationController
|
|||||||
tubesock.send_data JSON.dump({cmd: :timeout})
|
tubesock.send_data JSON.dump({cmd: :timeout})
|
||||||
@output = "timeout: #{@output}"
|
@output = "timeout: #{@output}"
|
||||||
elsif @output.empty?
|
elsif @output.empty?
|
||||||
tubesock.send_data JSON.dump({cmd: :write, stream: :stdout, data: t('exercises.implement.no_output', timestamp: l(Time.now, format: :short))})
|
tubesock.send_data JSON.dump({cmd: :write, stream: :stdout, data: t('exercises.implement.no_output', timestamp: l(Time.now, format: :short)) + "\n"})
|
||||||
end
|
end
|
||||||
tubesock.send_data JSON.dump({cmd: :write, stream: :stdout, data: t('exercises.implement.exit', exit_code: exit_code)}) unless status == :timeouted
|
tubesock.send_data JSON.dump({cmd: :write, stream: :stdout, data: t('exercises.implement.exit', exit_code: exit_code) + "\n"}) unless status == :timeouted
|
||||||
kill_socket(tubesock)
|
kill_socket(tubesock)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -187,7 +187,7 @@ class Submission < ApplicationRecord
|
|||||||
|
|
||||||
def prepared_container
|
def prepared_container
|
||||||
request_time = Time.now
|
request_time = Time.now
|
||||||
container = Container.new(execution_environment, execution_environment.permitted_execution_time)
|
container = Runner.new(execution_environment, execution_environment.permitted_execution_time)
|
||||||
container.copy_submission_files self
|
container.copy_submission_files self
|
||||||
container.waiting_time = Time.now - request_time
|
container.waiting_time = Time.now - request_time
|
||||||
yield(container) if block_given?
|
yield(container) if block_given?
|
||||||
|
@ -1,26 +1,26 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'container_connection'
|
require 'runner_connection'
|
||||||
|
|
||||||
class Container
|
class Runner
|
||||||
BASE_URL = CodeOcean::Config.new(:code_ocean).read[:container_management][:url]
|
BASE_URL = CodeOcean::Config.new(:code_ocean).read[:container_management][:url]
|
||||||
HEADERS = {"Content-Type" => "application/json"}
|
HEADERS = {"Content-Type" => "application/json"}
|
||||||
|
|
||||||
attr_accessor :waiting_time
|
attr_accessor :waiting_time
|
||||||
|
|
||||||
def initialize(execution_environment, time_limit = nil)
|
def initialize(execution_environment, time_limit = nil)
|
||||||
url = "#{BASE_URL}/execution-environments/#{execution_environment.id}/containers/create"
|
url = "#{BASE_URL}/runners"
|
||||||
body = {}
|
body = {execution_environment_id: execution_environment.id}
|
||||||
if time_limit
|
if time_limit
|
||||||
body[:time_limit] = time_limit
|
body[:time_limit] = time_limit
|
||||||
end
|
end
|
||||||
response = Faraday.post(url, body.to_json, HEADERS)
|
response = Faraday.post(url, body.to_json, HEADERS)
|
||||||
response = parse response
|
response = parse response
|
||||||
@container_id = response[:id]
|
@id = response[:id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_files(files)
|
def copy_files(files)
|
||||||
url = container_url + "/files"
|
url = runner_url + "/files"
|
||||||
body = { files: files.map{ |filename, content| { filename: filename, content: content } } }
|
body = { files: files.map{ |filename, content| { filename: filename, content: content } } }
|
||||||
Faraday.post(url, body.to_json, HEADERS)
|
Faraday.post(url, body.to_json, HEADERS)
|
||||||
end
|
end
|
||||||
@ -34,8 +34,8 @@ class Container
|
|||||||
end
|
end
|
||||||
|
|
||||||
def execute_command(command)
|
def execute_command(command)
|
||||||
url = container_url + "/execute"
|
url = runner_url + "/execute"
|
||||||
response = Faraday.patch(url, {command: command}.to_json, HEADERS)
|
response = Faraday.post(url, {command: command}.to_json, HEADERS)
|
||||||
response = parse response
|
response = parse response
|
||||||
response
|
response
|
||||||
end
|
end
|
||||||
@ -44,24 +44,24 @@ class Container
|
|||||||
starting_time = Time.now
|
starting_time = Time.now
|
||||||
websocket_url = execute_command(command)[:websocket_url]
|
websocket_url = execute_command(command)[:websocket_url]
|
||||||
EventMachine.run do
|
EventMachine.run do
|
||||||
socket = ContainerConnection.new(websocket_url)
|
socket = RunnerConnection.new(websocket_url)
|
||||||
yield(self, socket) if block_given?
|
yield(self, socket) if block_given?
|
||||||
end
|
end
|
||||||
Time.now - starting_time # execution time
|
Time.now - starting_time # execution time
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
Faraday.delete container_url
|
Faraday.delete runner_url
|
||||||
end
|
end
|
||||||
|
|
||||||
def status
|
def status
|
||||||
parse(Faraday.get(container_url))[:status].to_sym
|
parse(Faraday.get(runner_url))[:status].to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def container_url
|
def runner_url
|
||||||
"#{BASE_URL}/containers/#{@container_id}"
|
"#{BASE_URL}/runners/#{@id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(response)
|
def parse(response)
|
@ -1,6 +1,6 @@
|
|||||||
require 'faye/websocket/client'
|
require 'faye/websocket/client'
|
||||||
|
|
||||||
class ContainerConnection
|
class RunnerConnection
|
||||||
EVENTS = %i[start output exit stdout stderr].freeze
|
EVENTS = %i[start output exit stdout stderr].freeze
|
||||||
|
|
||||||
def initialize(url)
|
def initialize(url)
|
Reference in New Issue
Block a user