Validate json

This commit is contained in:
Konrad Hanff
2021-04-06 09:43:33 +02:00
committed by Sebastian Serth
parent cf1e4d6edf
commit 6e9562c9e1
5 changed files with 70 additions and 9 deletions

View File

@ -0,0 +1,44 @@
{
"$schema": "http://json-schema.org/schema#",
"title": "event",
"type": "object",
"oneOf": [
{
"properties": {
"type": {
"const": "exit",
"required": true
},
"data": {
"type": "integer",
"required": true,
"minimum": 0,
"maximum": 255
}
},
"additionalProperties": false
},
{
"properties": {
"type": {
"enum": [ "stdout", "stderr", "error" ],
"required": true
},
"data": {
"type": "string",
"required": true
}
},
"additionalProperties": false
},
{
"properties": {
"type": {
"enum": [ "start", "timeout" ],
"required": true
}
},
"additionalProperties": false
}
]
}

70
lib/runner/runner.rb Normal file
View File

@ -0,0 +1,70 @@
# frozen_string_literal: true
class Runner
BASE_URL = CodeOcean::Config.new(:code_ocean).read[:container_management][:url]
HEADERS = {"Content-Type" => "application/json"}
attr_accessor :waiting_time
def initialize(execution_environment, time_limit = nil)
url = "#{BASE_URL}/runners"
body = {executionEnvironmentId: execution_environment.id}
if time_limit
body[:timeLimit] = time_limit
end
response = Faraday.post(url, body.to_json, HEADERS)
response = parse response
@id = response[:runnerId]
end
def copy_files(files)
url = runner_url + "/files"
body = { files: files.map { |filename, content| { filepath: filename, content: content } } }
Faraday.patch(url, body.to_json, HEADERS)
end
def copy_submission_files(submission)
files = {}
submission.collect_files.each do |file|
files[file.name_with_extension] = file.content
end
copy_files(files)
end
def execute_command(command)
url = runner_url + "/execute"
response = Faraday.post(url, {command: command}.to_json, HEADERS)
response = parse response
response
end
def execute_interactively(command)
starting_time = Time.now
websocket_url = execute_command(command)[:websocketUrl]
EventMachine.run do
socket = RunnerConnection.new(websocket_url)
yield(self, socket) if block_given?
end
Time.now - starting_time # execution time
end
def destroy
Faraday.delete runner_url
end
def status
# parse(Faraday.get(runner_url))[:status].to_sym
# TODO return actual state retrieved via websocket
:timeouted
end
private
def runner_url
"#{BASE_URL}/runners/#{@id}"
end
def parse(response)
JSON.parse(response.body).deep_symbolize_keys
end
end

View File

@ -0,0 +1,70 @@
require 'faye/websocket/client'
require 'json_schemer'
class RunnerConnection
EVENTS = %i[start output exit stdout stderr].freeze
BACKEND_OUTPUT_SCHEMA = JSONSchemer.schema(JSON.parse(File.read("lib/runner/backend-output.schema.json")))
def initialize(url)
@socket = Faye::WebSocket::Client.new(url, [], ping: 5)
%i[open message error close].each do |event_type|
@socket.on event_type do |event| __send__(:"on_#{event_type}", event) end
end
EVENTS.each { |event_type| instance_variable_set(:"@#{event_type}_callback", lambda {|e|}) }
@start_callback = lambda {}
@exit_code = 0
end
def on(event, &block)
return unless EVENTS.include? event
instance_variable_set(:"@#{event}_callback", block)
end
def send(data)
@socket.send(encode(data))
end
private
def decode(event)
JSON.parse(event).deep_symbolize_keys
end
def encode(data)
data
end
def on_message(event)
return unless BACKEND_OUTPUT_SCHEMA.valid?(JSON.parse(event.data))
event = decode(event.data)
# TODO handle other events like timeout
case event[:type].to_sym
when :exit_code
@exit_code = event[:data]
when :stderr
@stderr_callback.call event[:data]
@output_callback.call event[:data]
when :stdout
@stdout_callback.call event[:data]
@output_callback.call event[:data]
else
:error
end
end
def on_open(event)
@start_callback.call
end
def on_error(event)
end
def on_close(event)
@exit_callback.call @exit_code
end
end