Skip to content

Commit

Permalink
Add basic support for submission batch_create.
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanzdosilovic committed Jan 16, 2020
1 parent 9acefdb commit 6420156
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
53 changes: 38 additions & 15 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class SubmissionsController < ApplicationController
before_action :authorize_request, only: [:index, :destroy]
before_action :check_maintenance, only: [:create, :destroy]
before_action :check_requested_fields
before_action :check_wait, only: [:create] # Wait in batch_create is not allowed
before_action :check_queue_size, only: [:create, :batch_create]
before_action :check_requested_fields, except: [:batch_create] # Fields are ignored in batch_create
before_action :set_base64_encoded

def index
Expand Down Expand Up @@ -58,21 +60,10 @@ def show
end

def create
wait = params[:wait] == "true"
if wait && !Config::ENABLE_WAIT_RESULT
render json: { error: "wait not allowed" }, status: :bad_request
return
end

if Resque.size(ENV["JUDGE0_VERSION"]) >= Config::MAX_QUEUE_SIZE
render json: { error: "queue is full" }, status: :service_unavailable
return
end

submission = Submission.new(submission_params)
submission = Submission.new(submission_params(params))

if submission.save
if wait
if @wait
begin
IsolateJob.perform_now(submission)
render json: submission, status: :created, base64_encoded: @base64_encoded, fields: @requested_fields
Expand All @@ -88,9 +79,28 @@ def create
end
end

def batch_create
submissions = params[:_json].each.collect { |p| Submission.new(submission_params(p)) }

response = []
has_valid_submission = false

submissions.each do |submission|
if submission.save
IsolateJob.perform_later(submission)
response << { token: submission.token }
has_valid_submission = true
else
response << submission.errors
end
end

render json: response, status: has_valid_submission ? :created : :unprocessable_entity
end

private

def submission_params
def submission_params(params)
submission_params = params.permit(
:source_code,
:language_id,
Expand Down Expand Up @@ -124,6 +134,19 @@ def submission_params
submission_params
end

def check_wait
@wait = params[:wait] == "true"
if @wait && !Config::ENABLE_WAIT_RESULT
render json: { error: "wait not allowed" }, status: :bad_request
end
end

def check_queue_size
if Resque.size(ENV["JUDGE0_VERSION"]) >= Config::MAX_QUEUE_SIZE
render json: { error: "queue is full" }, status: :service_unavailable
end
end

def check_requested_fields
fields_service = Fields::Submission.new(params[:fields])
render json: { error: "invalid fields: [#{fields_service.invalid_fields.join(", ")}]" }, status: :bad_request if fields_service.has_invalid_fields?
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Rails.application.routes.draw do
root 'home#docs'

resources :submissions, only: [:index, :show, :create, :destroy], param: :token
resources :submissions, only: [:index, :show, :create, :destroy], param: :token do
post 'batch_create', to: 'submissions#batch_create', on: :collection
end

resources :languages, only: [:index, :show] do
get 'all', to: 'languages#all', on: :collection
Expand Down

0 comments on commit 6420156

Please sign in to comment.