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
}
]
}

View File

@ -1,7 +1,5 @@
# frozen_string_literal: true
require 'runner_connection'
class Runner
BASE_URL = CodeOcean::Config.new(:code_ocean).read[:container_management][:url]
HEADERS = {"Content-Type" => "application/json"}
@ -10,19 +8,19 @@ class Runner
def initialize(execution_environment, time_limit = nil)
url = "#{BASE_URL}/runners"
body = {execution_environment_id: execution_environment.id}
body = {executionEnvironmentId: execution_environment.id}
if time_limit
body[:time_limit] = time_limit
body[:timeLimit] = time_limit
end
response = Faraday.post(url, body.to_json, HEADERS)
response = parse response
@id = response[:id]
@id = response[:runnerId]
end
def copy_files(files)
url = runner_url + "/files"
body = { files: files.map{ |filename, content| { filename: filename, content: content } } }
Faraday.post(url, body.to_json, HEADERS)
body = { files: files.map { |filename, content| { filepath: filename, content: content } } }
Faraday.patch(url, body.to_json, HEADERS)
end
def copy_submission_files(submission)
@ -42,7 +40,7 @@ class Runner
def execute_interactively(command)
starting_time = Time.now
websocket_url = execute_command(command)[:websocket_url]
websocket_url = execute_command(command)[:websocketUrl]
EventMachine.run do
socket = RunnerConnection.new(websocket_url)
yield(self, socket) if block_given?
@ -55,7 +53,9 @@ class Runner
end
def status
parse(Faraday.get(runner_url))[:status].to_sym
# parse(Faraday.get(runner_url))[:status].to_sym
# TODO return actual state retrieved via websocket
:timeouted
end
private

View File

@ -1,7 +1,9 @@
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)
@ -36,7 +38,11 @@ class RunnerConnection
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]