From 5d5ae6bcb171aa092b2dffe53593daf067f7a5c0 Mon Sep 17 00:00:00 2001 From: Kevin De Pelseneer Date: Wed, 13 Nov 2024 17:28:05 +0100 Subject: [PATCH] Enqueue `SampleTypeUpdateJob` after performing `UpdateSampleMetadataJob` --- app/jobs/update_sample_metadata_job.rb | 4 ++++ .../unit/jobs/update_sample_metadata_job_test.rb | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/jobs/update_sample_metadata_job.rb b/app/jobs/update_sample_metadata_job.rb index c1e8015163..4b25fa147a 100644 --- a/app/jobs/update_sample_metadata_job.rb +++ b/app/jobs/update_sample_metadata_job.rb @@ -4,6 +4,10 @@ class UpdateSampleMetadataJob < TaskJob queue_with_priority 1 queue_as QueueNames::SAMPLES + after_perform do |job| + SampleTypeUpdateJob.perform_later(job.arguments.first, true) + end + def perform(sample_type, user, attribute_changes = []) @sample_type = sample_type @user = user diff --git a/test/unit/jobs/update_sample_metadata_job_test.rb b/test/unit/jobs/update_sample_metadata_job_test.rb index 0ccfe05e51..8f82b593a1 100644 --- a/test/unit/jobs/update_sample_metadata_job_test.rb +++ b/test/unit/jobs/update_sample_metadata_job_test.rb @@ -10,16 +10,12 @@ def setup (1..10).each do |_i| FactoryBot.create(:sample, sample_type: @sample_type, contributor: @person) end - Rails.cache.clear - end - - def teardown - Rails.cache.clear end test 'perform' do User.with_current_user(@person.user) do UpdateSampleMetadataJob.perform_now(@sample_type, @person.user, []) + assert_enqueued_with(job: SampleTypeUpdateJob, args: [@sample_type, true]) end end @@ -58,6 +54,9 @@ def teardown end test 'perform with unexpected error' do + # Clears the queue of all jobs + # Queue has already multiple SampleTypeUpdateJob jobs from the setup + clear_enqueued_jobs job = UpdateSampleMetadataJob.new(@sample_type, @person.user, 'bad_attribute_change_map') assert_enqueued_with(job: UpdateSampleMetadataJob, args: [@sample_type, @person.user, 'bad_attribute_change_map']) do job.enqueue @@ -66,9 +65,10 @@ def teardown perform_enqueued_jobs do job.perform_now - rescue StandardError - # The sample type should be unlocked even if an error occurs - refute @sample_type.locked? end + # The sample type should be unlocked even if an error occurs + refute @sample_type.locked? + # SampleTypeUpdateJob should not be enqueued when an error occurs + assert_no_enqueued_jobs only: SampleTypeUpdateJob end end