Skip to content

Commit

Permalink
Reschedule failed jobs. Send only submission id to queue and not whol…
Browse files Browse the repository at this point in the history
…e submission.
  • Loading branch information
hermanzdosilovic committed Oct 1, 2020
1 parent c3b87d2 commit f7265cf
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem 'pry-rails', '~> 0.3.9'
gem 'puma', '~> 4.3.5'
gem 'rack-cors', '~> 1.1.1'
gem 'resque', '~> 2.0.0'
gem 'resque-scheduler', '~> 4.4'
gem 'will_paginate', '~> 3.2.1'

group :development do
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ GEM
activesupport
i18n
erubi (1.9.0)
et-orbi (1.2.4)
tzinfo
ffi (1.12.2)
fugit (1.3.9)
et-orbi (~> 1.1, >= 1.1.8)
raabro (~> 1.3)
globalid (0.4.2)
activesupport (>= 4.2.0)
httparty (0.17.3)
Expand Down Expand Up @@ -109,6 +114,7 @@ GEM
pry (>= 0.10.4)
puma (4.3.5)
nio4r (~> 2.0)
raabro (1.3.3)
rack (2.2.3)
rack-cors (1.1.1)
rack (>= 2.0.0)
Expand Down Expand Up @@ -153,7 +159,14 @@ GEM
redis-namespace (~> 1.6)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
resque-scheduler (4.4.0)
mono_logger (~> 1.0)
redis (>= 3.3)
resque (>= 1.26)
rufus-scheduler (~> 3.2)
ruby2_keywords (0.0.2)
rufus-scheduler (3.6.0)
fugit (~> 1.1, >= 1.1.6)
sinatra (2.0.8.1)
mustermann (~> 1.0)
rack (~> 2.0)
Expand Down Expand Up @@ -194,6 +207,7 @@ DEPENDENCIES
rack-cors (~> 1.1.1)
rails (~> 5.0)
resque (~> 2.0.0)
resque-scheduler (~> 4.4)
will_paginate (~> 3.2.1)

BUNDLED WITH
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative 'config/application'
require 'resque/tasks'
require 'resque/scheduler/tasks'

task 'resque:setup' => :environment

Expand Down
6 changes: 3 additions & 3 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ def create
if submission.save
if @wait
begin
IsolateJob.perform_now(submission)
IsolateJob.perform_now(submission.id)
render json: submission, status: :created, base64_encoded: @base64_encoded, fields: @requested_fields
rescue Encoding::UndefinedConversionError => e
render_conversion_error(:created, submission.token)
end
else
IsolateJob.perform_later(submission)
IsolateJob.perform_later(submission.id)
render json: submission, status: :created, fields: [:token]
end
else
Expand Down Expand Up @@ -139,7 +139,7 @@ def batch_create

submissions.each do |submission|
if submission.save
IsolateJob.perform_later(submission)
IsolateJob.perform_later(submission.id)
response << { token: submission.token }
has_valid_submission = true
else
Expand Down
7 changes: 5 additions & 2 deletions app/jobs/isolate_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class IsolateJob < ApplicationJob
retry_on RuntimeError, wait: 2.seconds, attempts: 100

queue_as ENV["JUDGE0_VERSION"].to_sym

STDIN_FILE_NAME = "stdin.txt"
Expand All @@ -12,8 +14,8 @@ class IsolateJob < ApplicationJob
:source_file, :stdin_file, :stdout_file,
:stderr_file, :metadata_file, :additional_files_archive_file

def perform(submission)
@submission = submission
def perform(submission_id)
@submission = Submission.find(submission_id)

time = []
memory = []
Expand All @@ -40,6 +42,7 @@ def perform(submission)
submission.save

rescue Exception => e
raise e.message unless submission
submission.finished_at ||= DateTime.now
submission.update(message: e.message, status: Status.boxerr)
cleanup(raise_exception = false)
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/run_submissions_in_queue.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace :judge0 do
ARGV.each { |a| task a.to_sym do ; end }
Submission.where(status_id: Status.queue).each do |s|
if ARGV[1].to_s == "now"
IsolateJob.perform_now(s)
IsolateJob.perform_now(s.id)
else
IsolateJob.perform_later(s)
IsolateJob.perform_later(s.id)
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions scripts/workers
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ source ./scripts/load-config

run_resque=1
resque_pid=0
scheduler_pid=0

date_now() {
echo -n $(date +"%Y-%m-%d-%H-%M-%S")
Expand All @@ -23,6 +24,12 @@ trap exit_gracefully SIGTERM SIGINT

mkdir -p tmp/pids &> /dev/null
while [[ $run_resque -eq 1 ]]; do
echo "[$(date_now)] Starting scheduler."
if ! ps -p $scheduler_pid &> /dev/null; then
rake resque:scheduler &
scheduler_pid=$!
fi

rm -rf tmp/pids/resque.pid &> /dev/null
echo "[$(date_now)] Starting workers."
rails resque:workers &
Expand Down

0 comments on commit f7265cf

Please sign in to comment.