Skip to content

Commit

Permalink
Scope job concurrency keys by job class
Browse files Browse the repository at this point in the history
This was the underlying issue for the flaky test. Since only the id was
part of the key in tests they very easily overlapped. This could of course
also happen locally, but depending on usage that is very unlikely. Not
when just starting out though.
  • Loading branch information
Earlopain committed Nov 8, 2023
1 parent da953d8 commit 8f331cf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

class ApplicationJob < ActiveJob::Base
include GoodJob::ActiveJobExtensions::Concurrency
# Automatically scope keys to the job/queue. Can be simplified should
# https://github.com/bensheldon/good_job/pull/1145 be merged.
def _good_job_concurrency_key
key = super
return if key.nil?

"#{self.class.name}-#{queue_name}-#{key}"
end

retry_on StandardError, wait: :polynomially_longer, attempts: 25 do |_job, exception|
log_exception(exception)
end
Expand Down
18 changes: 18 additions & 0 deletions test/logical/good_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "test_helper"

class GoodJobTest < ActiveSupport::TestCase
test "current migrations are applied" do
assert_predicate GoodJob, :migrated?
end

test "concurrency keys are scoped by job class and queue" do
artist_url = create(:artist_url)
job = ScrapeArtistUrlJob.perform_later(artist_url)
assert_equal("ScrapeArtistUrlJob-scraping-#{artist_url.id}", job.good_job_concurrency_key)

job = SubmissionFileUpdateJob.perform_later(create(:submission_file))
assert_nil job.good_job_concurrency_key
end
end
2 changes: 0 additions & 2 deletions test/logical/job_stats_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

class JobStatsTest < ActiveSupport::TestCase
it "returns enqueued jobs" do
skip "flaky on CI" if ENV["CI"]

url1 = create(:artist_url)
submission1, submission2 = create_list(:artist_submission, 2, artist_url: url1)
file1, file2 = create_list(:submission_file, 2, artist_submission: submission1)
Expand Down

0 comments on commit 8f331cf

Please sign in to comment.