Auto-correct linting issues

This commit is contained in:
Konrad Hanff
2021-04-06 13:51:11 +02:00
committed by Sebastian Serth
parent 6e9562c9e1
commit 80932c0c40
3 changed files with 46 additions and 57 deletions

View File

@ -68,9 +68,7 @@ class SubmissionsController < ApplicationController
end end
def download def download
if @embed_options[:disable_download] raise Pundit::NotAuthorizedError if @embed_options[:disable_download]
raise Pundit::NotAuthorizedError
end
# files = @submission.files.map{ } # files = @submission.files.map{ }
# zipline( files, 'submission.zip') # zipline( files, 'submission.zip')
@ -112,9 +110,7 @@ class SubmissionsController < ApplicationController
end end
def download_file def download_file
if @embed_options[:disable_download] raise Pundit::NotAuthorizedError if @embed_options[:disable_download]
raise Pundit::NotAuthorizedError
end
if @file.native_file? if @file.native_file?
send_file(@file.native_file.path) send_file(@file.native_file.path)
@ -142,7 +138,7 @@ class SubmissionsController < ApplicationController
@output = '' @output = ''
socket.on :output do |data| socket.on :output do |data|
Rails.logger.info("#{Time.zone.now.getutc}: Docker sending: #{data}") Rails.logger.info("#{Time.zone.now.getutc}: Container sending: #{data}")
@output << data if @output.size + data.size <= max_output_buffer_size @output << data if @output.size + data.size <= max_output_buffer_size
end end
@ -161,38 +157,36 @@ 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)) + "\n"}) tubesock.send_data JSON.dump({cmd: :write, stream: :stdout, data: "#{t('exercises.implement.no_output', timestamp: l(Time.zone.now, format: :short))}\n"})
end end
tubesock.send_data JSON.dump({cmd: :write, stream: :stdout, data: t('exercises.implement.exit', exit_code: exit_code) + "\n"}) 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
tubesock.onmessage do |event| tubesock.onmessage do |event|
begin event = JSON.parse(event).deep_symbolize_keys
event = JSON.parse(event).deep_symbolize_keys case event[:cmd].to_sym
case event[:cmd].to_sym when :client_kill
when :client_kill EventMachine.stop_event_loop
EventMachine.stop_event_loop kill_socket(tubesock)
kill_socket(tubesock) container.destroy
container.destroy Rails.logger.debug('Client exited container.')
Rails.logger.debug('Client exited container.') when :result
when :result socket.send event[:data]
socket.send event[:data] else
else Rails.logger.info("Unknown command from client: #{event[:cmd]}")
Rails.logger.info("Unknown command from client: #{event[:cmd]}")
end
rescue JSON::ParserError => error
Rails.logger.debug { "Data received from client is not valid json: #{data}" }
Sentry.set_extras(data: data)
rescue TypeError => error
Rails.logger.debug { "JSON data received from client cannot be parsed to hash: #{data}" }
Sentry.set_extras(data: data)
end end
rescue JSON::ParserError => e
ails.logger.debug { "Data received from client is not valid json: #{data}" }
Sentry.set_extras(data: data)
rescue TypeError => e
Rails.logger.debug { "JSON data received from client cannot be parsed to hash: #{data}" }
Sentry.set_extras(data: data)
end end
end end
def run def run
# TODO do we need this thread? If so, how to fix double render? (to reproduce: remove .join and run) # TODO: do we need this thread? If so, how to fix double render? (to reproduce: remove .join and run)
Thread.new do Thread.new do
hijack do |tubesock| hijack do |tubesock|
if @embed_options[:disable_run] if @embed_options[:disable_run]
@ -208,7 +202,7 @@ class SubmissionsController < ApplicationController
ensure ensure
ActiveRecord::Base.connection_pool.release_connection ActiveRecord::Base.connection_pool.release_connection
end.join end.join
# TODO determine if this is necessary # TODO: determine if this is necessary
# unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive? # unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
# Thread.new do # Thread.new do
# EventMachine.run # EventMachine.run
@ -248,9 +242,7 @@ class SubmissionsController < ApplicationController
if @output.present? if @output.present?
@submission.exercise.execution_environment.error_templates.each do |template| @submission.exercise.execution_environment.error_templates.each do |template|
pattern = Regexp.new(template.signature).freeze pattern = Regexp.new(template.signature).freeze
if pattern.match(@output) results << StructuredError.create_from_template(template, @output, @submission) if pattern.match(@output)
results << StructuredError.create_from_template(template, @output, @submission)
end
end end
end end
results results
@ -288,7 +280,7 @@ class SubmissionsController < ApplicationController
# private :set_docker_client # private :set_docker_client
def set_file def set_file
@file = @files.detect {|file| file.name_with_extension == sanitize_filename } @file = @files.detect { |file| file.name_with_extension == sanitize_filename }
head :not_found unless @file head :not_found unless @file
end end
private :set_file private :set_file
@ -314,7 +306,7 @@ class SubmissionsController < ApplicationController
def statistics; end def statistics; end
# TODO is this needed? # TODO: is this needed?
# def test # def test
# Thread.new do # Thread.new do
# hijack do |tubesock| # hijack do |tubesock|
@ -332,7 +324,6 @@ class SubmissionsController < ApplicationController
# end # end
# end # end
def with_server_sent_events def with_server_sent_events
response.headers['Content-Type'] = 'text/event-stream' response.headers['Content-Type'] = 'text/event-stream'
server_sent_event = SSE.new(response.stream) server_sent_event = SSE.new(response.stream)

View File

@ -2,24 +2,22 @@
class Runner 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'}.freeze
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}/runners" url = "#{BASE_URL}/runners"
body = {executionEnvironmentId: execution_environment.id} body = {executionEnvironmentId: execution_environment.id}
if time_limit body[:timeLimit] = time_limit if time_limit
body[:timeLimit] = time_limit
end
response = Faraday.post(url, body.to_json, HEADERS) response = Faraday.post(url, body.to_json, HEADERS)
response = parse response response = parse response
@id = response[:runnerId] @id = response[:runnerId]
end end
def copy_files(files) def copy_files(files)
url = runner_url + "/files" url = "#{runner_url}/files"
body = { files: files.map { |filename, content| { filepath: filename, content: content } } } body = {files: files.map { |filename, content| {filepath: filename, content: content} }}
Faraday.patch(url, body.to_json, HEADERS) Faraday.patch(url, body.to_json, HEADERS)
end end
@ -32,20 +30,19 @@ class Runner
end end
def execute_command(command) def execute_command(command)
url = runner_url + "/execute" url = "#{runner_url}/execute"
response = Faraday.post(url, {command: command}.to_json, HEADERS) response = Faraday.post(url, {command: command}.to_json, HEADERS)
response = parse response parse response
response
end end
def execute_interactively(command) def execute_interactively(command)
starting_time = Time.now starting_time = Time.zone.now
websocket_url = execute_command(command)[:websocketUrl] websocket_url = execute_command(command)[:websocketUrl]
EventMachine.run do EventMachine.run do
socket = RunnerConnection.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.zone.now - starting_time # execution time
end end
def destroy def destroy

View File

@ -1,19 +1,21 @@
# frozen_string_literal: true
require 'faye/websocket/client' require 'faye/websocket/client'
require 'json_schemer' require 'json_schemer'
class RunnerConnection class RunnerConnection
EVENTS = %i[start output exit stdout stderr].freeze EVENTS = %i[start output exit stdout stderr].freeze
BACKEND_OUTPUT_SCHEMA = JSONSchemer.schema(JSON.parse(File.read("lib/runner/backend-output.schema.json"))) BACKEND_OUTPUT_SCHEMA = JSONSchemer.schema(JSON.parse(File.read('lib/runner/backend-output.schema.json')))
def initialize(url) def initialize(url)
@socket = Faye::WebSocket::Client.new(url, [], ping: 5) @socket = Faye::WebSocket::Client.new(url, [], ping: 5)
%i[open message error close].each do |event_type| %i[open message error close].each do |event_type|
@socket.on event_type do |event| __send__(:"on_#{event_type}", event) end @socket.on(event_type) { |event| __send__(:"on_#{event_type}", event) }
end end
EVENTS.each { |event_type| instance_variable_set(:"@#{event_type}_callback", lambda {|e|}) } EVENTS.each { |event_type| instance_variable_set(:"@#{event_type}_callback", ->(e) {}) }
@start_callback = lambda {} @start_callback = -> {}
@exit_code = 0 @exit_code = 0
end end
@ -42,7 +44,7 @@ class RunnerConnection
event = decode(event.data) event = decode(event.data)
# TODO handle other events like timeout # TODO: handle other events like timeout
case event[:type].to_sym case event[:type].to_sym
when :exit_code when :exit_code
@exit_code = event[:data] @exit_code = event[:data]
@ -57,14 +59,13 @@ class RunnerConnection
end end
end end
def on_open(event) def on_open(_event)
@start_callback.call @start_callback.call
end end
def on_error(event) def on_error(event); end
end
def on_close(event) def on_close(_event)
@exit_callback.call @exit_code @exit_callback.call @exit_code
end end
end end