Skip to content

Commit

Permalink
Avoid simultaneous reprocessing of a repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jorg-vr committed Oct 3, 2023
1 parent 12f2329 commit 9e5951f
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 37 deletions.
1 change: 1 addition & 0 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# description_nl_present :boolean default(FALSE)
# description_en_present :boolean default(FALSE)
# series_count :integer default(0), not null
# draft :boolean default(FALSE)
#

require 'pathname'
Expand Down
1 change: 1 addition & 0 deletions app/models/content_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# description_nl_present :boolean default(FALSE)
# description_en_present :boolean default(FALSE)
# series_count :integer default(0), not null
# draft :boolean default(FALSE)
#

class ContentPage < Activity
Expand Down
1 change: 1 addition & 0 deletions app/models/exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# description_nl_present :boolean default(FALSE)
# description_en_present :boolean default(FALSE)
# series_count :integer default(0), not null
# draft :boolean default(FALSE)
#

require 'pathname'
Expand Down
55 changes: 37 additions & 18 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
#
# Table name: repositories
#
# id :integer not null, primary key
# name :string(255)
# remote :string(255)
# path :string(255)
# judge_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# clone_status :integer default("queued"), not null
# featured :boolean default(FALSE)
# id :integer not null, primary key
# name :string(255)
# remote :string(255)
# path :string(255)
# judge_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# clone_status :integer default("queued"), not null
# featured :boolean default(FALSE)
# reprocess_queued :boolean default(FALSE)
# reprocess_running :boolean default(FALSE)
#
require 'open3'
require 'pathname'
Expand Down Expand Up @@ -113,18 +115,35 @@ def clone_repo
end

def process_activities_email_errors_delayed(kwargs = {})
delay(queue: 'git').process_activities_email_errors(kwargs)
return if reprocess_queued?

update(reprocess_queued: true)
if reprocess_running?
delay(queue: 'git', run_at: 1.minute.from_now).process_activities_email_errors(kwargs)

Check warning on line 122 in app/models/repository.rb

View check run for this annotation

Codecov / codecov/patch

app/models/repository.rb#L122

Added line #L122 was not covered by tests
else
delay(queue: 'git').process_activities_email_errors(kwargs)
end
end

def process_activities_email_errors(kwargs = {})
kwargs[:user] = admins.first if kwargs.empty? && admins.any?
kwargs[:email] = Rails.application.config.dodona_email if kwargs.empty?

process_activities
rescue AggregatedConfigErrors => e
ErrorMailer.json_error(e, **kwargs).deliver
rescue DodonaGitError => e
ErrorMailer.git_error(e, **kwargs).deliver
if reprocess_running?
delay(queue: 'git', run_at: 1.minute.from_now).process_activities_email_errors(kwargs)
return

Check warning on line 131 in app/models/repository.rb

View check run for this annotation

Codecov / codecov/patch

app/models/repository.rb#L130-L131

Added lines #L130 - L131 were not covered by tests
end

update(reprocess_queued: false, reprocess_running: true)
begin
kwargs[:user] = admins.first if kwargs.empty? && admins.any?
kwargs[:email] = Rails.application.config.dodona_email if kwargs.empty?

process_activities
rescue AggregatedConfigErrors => e
ErrorMailer.json_error(e, **kwargs).deliver
rescue DodonaGitError => e
ErrorMailer.git_error(e, **kwargs).deliver
ensure
update(reprocess_running: false)
end
end

def process_activities
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddReprocessStatusToRepositories < ActiveRecord::Migration[7.0]
def change
add_column :repositories, :reprocess_queued, :boolean, default: false
add_column :repositories, :reprocess_running, :boolean, default: false
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_08_10_105908) do
ActiveRecord::Schema[7.0].define(version: 2023_10_03_115520) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down Expand Up @@ -59,6 +59,7 @@
t.boolean "description_nl_present", default: false
t.boolean "description_en_present", default: false
t.integer "series_count", default: 0, null: false
t.boolean "draft", default: false
t.index ["judge_id"], name: "index_activities_on_judge_id"
t.index ["name_nl"], name: "index_activities_on_name_nl"
t.index ["path", "repository_id"], name: "index_activities_on_path_and_repository_id", unique: true
Expand Down Expand Up @@ -392,6 +393,8 @@
t.datetime "updated_at", precision: nil, null: false
t.integer "clone_status", default: 1, null: false
t.boolean "featured", default: false
t.boolean "reprocess_queued", default: false
t.boolean "reprocess_running", default: false
t.index ["judge_id"], name: "index_repositories_on_judge_id"
t.index ["name"], name: "index_repositories_on_name", unique: true
t.index ["path"], name: "index_repositories_on_path", unique: true
Expand Down
1 change: 1 addition & 0 deletions test/factories/content_pages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# description_nl_present :boolean default(FALSE)
# description_en_present :boolean default(FALSE)
# series_count :integer default(0), not null
# draft :boolean default(FALSE)
#

require "#{File.dirname(__FILE__)}/../testhelpers/stub_helper.rb"
Expand Down
1 change: 1 addition & 0 deletions test/factories/exercises.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# description_nl_present :boolean default(FALSE)
# description_en_present :boolean default(FALSE)
# series_count :integer default(0), not null
# draft :boolean default(FALSE)
#

require "#{File.dirname(__FILE__)}/../testhelpers/stub_helper.rb"
Expand Down
20 changes: 11 additions & 9 deletions test/factories/repositories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
#
# Table name: repositories
#
# id :integer not null, primary key
# name :string(255)
# remote :string(255)
# path :string(255)
# judge_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# clone_status :integer default("queued"), not null
# featured :boolean default(FALSE)
# id :integer not null, primary key
# name :string(255)
# remote :string(255)
# path :string(255)
# judge_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# clone_status :integer default("queued"), not null
# featured :boolean default(FALSE)
# reprocess_queued :boolean default(FALSE)
# reprocess_running :boolean default(FALSE)
#

require "#{File.dirname(__FILE__)}/../testhelpers/stub_helper.rb"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/exercises.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# description_nl_present :boolean default(FALSE)
# description_en_present :boolean default(FALSE)
# series_count :integer default(0), not null
# draft :boolean default(FALSE)
#

python_exercise:
Expand Down
1 change: 1 addition & 0 deletions test/models/activity_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# description_nl_present :boolean default(FALSE)
# description_en_present :boolean default(FALSE)
# series_count :integer default(0), not null
# draft :boolean default(FALSE)
#

require 'test_helper'
Expand Down
1 change: 1 addition & 0 deletions test/models/exercise_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# description_nl_present :boolean default(FALSE)
# description_en_present :boolean default(FALSE)
# series_count :integer default(0), not null
# draft :boolean default(FALSE)
#

require 'test_helper'
Expand Down
20 changes: 11 additions & 9 deletions test/models/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
#
# Table name: repositories
#
# id :integer not null, primary key
# name :string(255)
# remote :string(255)
# path :string(255)
# judge_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# clone_status :integer default("queued"), not null
# featured :boolean default(FALSE)
# id :integer not null, primary key
# name :string(255)
# remote :string(255)
# path :string(255)
# judge_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# clone_status :integer default("queued"), not null
# featured :boolean default(FALSE)
# reprocess_queued :boolean default(FALSE)
# reprocess_running :boolean default(FALSE)
#

require 'test_helper'
Expand Down

0 comments on commit 9e5951f

Please sign in to comment.