From a5d5dde7a80147bcf83f17b4f87f94bad9410e4e Mon Sep 17 00:00:00 2001 From: Jan Renz Date: Thu, 23 Apr 2015 12:58:21 +0200 Subject: [PATCH] Debugging --- app/controllers/submissions_controller.rb | 24 ++++++++++++------ lib/docker_client.rb | 31 ++++++++++++----------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 5e0ff61e..fb14ff07 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -72,15 +72,23 @@ class SubmissionsController < ApplicationController with_server_sent_events do |server_sent_event| container_info_sent = false stderr = '' - output = @docker_client.execute_run_command(@submission, params[:filename]) do |stream, chunk| - unless container_info_sent - server_sent_event.write({id: @docker_client.container.try(:id), port_bindings: @docker_client.container.try(:port_bindings)}, event: 'info') - container_info_sent = true - end - server_sent_event.write({stream => chunk}, event: 'output') - stderr += chunk if stream == :stderr + output = @docker_client.execute_run_command(@submission, params[:filename]) + if output + server_sent_event.write({stdout: output[:stdout]}, event: 'output') + server_sent_event.write({stderr: output[:stderr]}, event: 'output') + output[:status] = :ok if output[:status] == 0 + server_sent_event.write({status: output[:status]}, event: 'status') end - server_sent_event.write(output, event: 'status') + #server_sent_event.write({stdout: output[:output][2], event: 'status') + # do |stream, chunk| + # unless container_info_sent + # server_sent_event.write({id: @docker_client.container.try(:id), port_bindings: @docker_client.container.try(:port_bindings)}, event: 'info') + # container_info_sent = true + # end + # server_sent_event.write({stream => chunk}, event: 'output') + # stderr += chunk if stream == :stderr + # end + # server_sent_event.write(output, event: 'status') if stderr.present? if hint = Whistleblower.new(execution_environment: @submission.execution_environment).generate_hint(stderr) server_sent_event.write(hint, event: 'hint') diff --git a/lib/docker_client.rb b/lib/docker_client.rb index ac124132..98918179 100644 --- a/lib/docker_client.rb +++ b/lib/docker_client.rb @@ -176,34 +176,35 @@ class DockerClient def send_command(command, container, &block) Timeout.timeout(@execution_environment.permitted_execution_time.to_i) do - stderr = [] - stdout = [] - container.exec(['bash', '-c', command]) do |stream, chunk| - block.call(stream, chunk) if block_given? - if stream == :stderr - stderr.push(chunk) - else - stdout.push(chunk) - end - end - {status: :ok, stderr: stderr.join, stdout: stdout.join} + output = container.exec(['bash', '-c', command]) + #do |stream, chunk| + # block.call(stream, chunk) if block_given? #this may issue + # if stream == :stderr + ## stderr.push(chunk) + # else + # stdout.push(chunk) + # end + #end + Rails.logger.info output + {status: output[2], stderr: output[1].join, stdout: output[0].join} end rescue Timeout::Error + timeout_occured = true Rails.logger.info('got timeout error for container ' + container.to_s) #container.restart if RECYCLE_CONTAINERS DockerContainerPool.remove_from_all_containers(container, @execution_environment) # destroy container - destroy_container(container) + self.class.destroy_container(container) if(RECYCLE_CONTAINERS) # create new container and add it to @all_containers. will be added to @containers on return_container - container = create_container(@execution_environment) + container = self.class.create_container(@execution_environment) DockerContainerPool.add_to_all_containers(container, @execution_environment) end - {status: :timeout} + {status: :timeout, stderr: '', stdout: ''} ensure - Rails.logger.info('after timeout error ensuring for' + container.to_s) + Rails.logger.info('send_command ensuring for' + container.to_s) RECYCLE_CONTAINERS ? return_container(container) : self.class.destroy_container(container) end private :send_command