Fix some non-autocorrectable linting issues
Many functions in submission_controller.rb still are very long and have a high complexity. Because the logic for handling execution of submissions will probably move elsewhere (when switching to ActionCable), this is fine for now.
This commit is contained in:

committed by
Sebastian Serth

parent
80932c0c40
commit
575057acd3
@ -10,7 +10,6 @@ class SubmissionsController < ApplicationController
|
|||||||
|
|
||||||
before_action :set_submission,
|
before_action :set_submission,
|
||||||
only: %i[download download_file render_file run score extract_errors show statistics test]
|
only: %i[download download_file render_file run score extract_errors show statistics test]
|
||||||
# before_action :set_docker_client, only: %i[run test]
|
|
||||||
before_action :set_files, only: %i[download download_file render_file show run]
|
before_action :set_files, only: %i[download download_file render_file show run]
|
||||||
before_action :set_file, only: %i[download_file render_file run]
|
before_action :set_file, only: %i[download_file render_file run]
|
||||||
before_action :set_mime_type, only: %i[download_file render_file]
|
before_action :set_mime_type, only: %i[download_file render_file]
|
||||||
@ -135,7 +134,7 @@ class SubmissionsController < ApplicationController
|
|||||||
|
|
||||||
def handle_websockets(tubesock, container, socket)
|
def handle_websockets(tubesock, container, socket)
|
||||||
tubesock.send_data JSON.dump({'cmd' => 'status', 'status' => :container_running})
|
tubesock.send_data JSON.dump({'cmd' => 'status', 'status' => :container_running})
|
||||||
@output = ''
|
@output = String.new
|
||||||
|
|
||||||
socket.on :output do |data|
|
socket.on :output do |data|
|
||||||
Rails.logger.info("#{Time.zone.now.getutc}: Container sending: #{data}")
|
Rails.logger.info("#{Time.zone.now.getutc}: Container sending: #{data}")
|
||||||
@ -176,18 +175,16 @@ class SubmissionsController < ApplicationController
|
|||||||
else
|
else
|
||||||
Rails.logger.info("Unknown command from client: #{event[:cmd]}")
|
Rails.logger.info("Unknown command from client: #{event[:cmd]}")
|
||||||
end
|
end
|
||||||
rescue JSON::ParserError => e
|
rescue JSON::ParserError
|
||||||
ails.logger.debug { "Data received from client is not valid json: #{data}" }
|
Rails.logger.debug { "Data received from client is not valid json: #{data}" }
|
||||||
Sentry.set_extras(data: data)
|
Sentry.set_extras(data: data)
|
||||||
rescue TypeError => e
|
rescue TypeError
|
||||||
Rails.logger.debug { "JSON data received from client cannot be parsed to hash: #{data}" }
|
Rails.logger.debug { "JSON data received from client cannot be parsed to hash: #{data}" }
|
||||||
Sentry.set_extras(data: 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)
|
|
||||||
Thread.new do
|
|
||||||
hijack do |tubesock|
|
hijack do |tubesock|
|
||||||
if @embed_options[:disable_run]
|
if @embed_options[:disable_run]
|
||||||
kill_socket(tubesock)
|
kill_socket(tubesock)
|
||||||
@ -199,17 +196,6 @@ class SubmissionsController < ApplicationController
|
|||||||
save_run_output
|
save_run_output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
|
||||||
ActiveRecord::Base.connection_pool.release_connection
|
|
||||||
end.join
|
|
||||||
# TODO: determine if this is necessary
|
|
||||||
# unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
|
|
||||||
# Thread.new do
|
|
||||||
# EventMachine.run
|
|
||||||
# ensure
|
|
||||||
# ActiveRecord::Base.connection_pool.release_connection
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def kill_socket(tubesock)
|
def kill_socket(tubesock)
|
||||||
@ -306,23 +292,22 @@ class SubmissionsController < ApplicationController
|
|||||||
|
|
||||||
def statistics; end
|
def statistics; end
|
||||||
|
|
||||||
# TODO: is this needed?
|
# TODO: make this run, but with the test command
|
||||||
# def test
|
# def test
|
||||||
# Thread.new do
|
|
||||||
# hijack do |tubesock|
|
# hijack do |tubesock|
|
||||||
# if @embed_options[:disable_run]
|
# unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
|
||||||
# kill_socket(tubesock)
|
# Thread.new do
|
||||||
# return
|
# EventMachine.run
|
||||||
# end
|
|
||||||
# @container_request_time = Time.now
|
|
||||||
# @submission.run_tests(sanitize_filename) do |container|
|
|
||||||
# handle_websockets(tubesock, container)
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
# ensure
|
# ensure
|
||||||
# ActiveRecord::Base.connection_pool.release_connection
|
# ActiveRecord::Base.connection_pool.release_connection
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
# output = @docker_client.execute_test_command(@submission, sanitize_filename)
|
||||||
|
# # tubesock is the socket to the client
|
||||||
|
# tubesock.send_data JSON.dump(output)
|
||||||
|
# tubesock.send_data JSON.dump('cmd' => 'exit')
|
||||||
|
# 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'
|
||||||
|
@ -50,8 +50,7 @@ class Runner
|
|||||||
end
|
end
|
||||||
|
|
||||||
def status
|
def status
|
||||||
# parse(Faraday.get(runner_url))[:status].to_sym
|
# TODO: return actual state retrieved via websocket
|
||||||
# TODO return actual state retrieved via websocket
|
|
||||||
:timeouted
|
:timeouted
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -43,20 +43,7 @@ class RunnerConnection
|
|||||||
return unless BACKEND_OUTPUT_SCHEMA.valid?(JSON.parse(event.data))
|
return unless BACKEND_OUTPUT_SCHEMA.valid?(JSON.parse(event.data))
|
||||||
|
|
||||||
event = decode(event.data)
|
event = decode(event.data)
|
||||||
|
__send__("handle_#{event[:type]}", event)
|
||||||
# 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
|
end
|
||||||
|
|
||||||
def on_open(_event)
|
def on_open(_event)
|
||||||
@ -68,4 +55,26 @@ class RunnerConnection
|
|||||||
def on_close(_event)
|
def on_close(_event)
|
||||||
@exit_callback.call @exit_code
|
@exit_callback.call @exit_code
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_exit(event)
|
||||||
|
@exit_code = event[:data]
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_stdout(event)
|
||||||
|
@stdout_callback.call event[:data]
|
||||||
|
@output_callback.call event[:data]
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_stderr(event)
|
||||||
|
@stderr_callback.call event[:data]
|
||||||
|
@output_callback.call event[:data]
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_error(event) end
|
||||||
|
|
||||||
|
def handle_start(event) end
|
||||||
|
|
||||||
|
def handle_timeout(event)
|
||||||
|
# TODO: set the runner state
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user