OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Make CopyProjectFolderJob reliant on GoodJob::Batch
Marcello Rocha7d9e0850002c
diff --git a/modules/storages/app/workers/storages/copy_project_folders_job.rb b/modules/storages/app/workers/storages/copy_project_folders_job.rbindex 3c71890ceb8..039b66843c6 100644--- a/modules/storages/app/workers/storages/copy_project_folders_job.rb+++ b/modules/storages/app/workers/storages/copy_project_folders_job.rb@@ -30,13 +30,15 @@module Storagesclass CopyProjectFoldersJob < ApplicationJob- retry_on Errors::PollingRequired, wait: 3, attempts: (Rails.env.test? ? 3 : :unlimited)+ include GoodJob::ActiveJobExtensions::Batches++ retry_on Errors::PollingRequired, wait: 3, attempts: :unlimiteddiscard_on HTTPX::HTTPError- def perform(user_id:, source_id:, target_id:, work_package_map:)+ def perform(source_id:, target_id:, work_package_map:)target = ProjectStorage.find(target_id)source = ProjectStorage.find(source_id)- user = User.find(user_id)+ user = batch.properties[:user]project_folder_result = results_from_polling || initiate_copy(source, target)@@ -59,25 +61,31 @@ module Storagesdef prepare_polling(result, source)return unless result.requires_polling?- Thread.current[job_id] = result.polling_url+ batch.properties.merge!(polling_state: :ongoing, polling_url: result.polling_url)+ batch.save+raise Errors::PollingRequired, "Storage #{source.storage.name} requires polling"enddef polling?- !!Thread.current[job_id]+ batch.properties[:polling_state] == :ongoingenddef results_from_pollingreturn unless polling?- response = OpenProject.httpx.get(Thread.current[job_id]).json(symbolize_keys: true)+ response = OpenProject.httpx.get(batch.properties[:polling_url]).json(symbolize_keys: true)- # FIXME: We may want to discard in case some other error is raised- raise(Errors::PollingRequired, "#{job_id} Polling not completed yet") if response[:status] != "completed"+ if response[:status] != "completed"+ batch.properties[:polling_state] == :ongoing+ batch.save+ raise(Errors::PollingRequired, "#{job_id} Polling not completed yet")+ end- Thread.current[job_id] = nil- result = Peripherals::StorageInteraction::ResultData::CopyTemplateFolder.new(response[:resourceId], nil, false)+ batch.properties[:polling_state] == :completed+ batch.save+ result = Peripherals::StorageInteraction::ResultData::CopyTemplateFolder.new(response[:resourceId], nil, false)ServiceResult.success(result:)enddiff --git a/modules/storages/spec/workers/storages/copy_project_folders_job_spec.rb b/modules/storages/spec/workers/storages/copy_project_folders_job_spec.rbindex b5b2a2d8311..134c0b349f5 100644--- a/modules/storages/spec/workers/storages/copy_project_folders_job_spec.rb+++ b/modules/storages/spec/workers/storages/copy_project_folders_job_spec.rb@@ -28,10 +28,10 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'spec_helper'+require "spec_helper"require_module_spec_helper-RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock do+RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock, with_good_job_batches: [Storages::CopyProjectFoldersJob] doinclude ActiveJob::TestHelperlet(:storage) { create(:nextcloud_storage, :as_automatically_managed) }@@ -51,7 +51,7 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock do.to_hend- let(:polling_url) { 'https://polling.url.de/cool/subresources' }+ let(:polling_url) { "https://polling.url.de/cool/subresources" }let(:copy_result) { Storages::Peripherals::StorageInteraction::ResultData::CopyTemplateFolder.new(nil, nil, false) }let(:target_deep_file_ids) do@@ -64,7 +64,7 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock dolet(:source_file_infos) dosource_file_links.map do |fl|Storages::StorageFileInfo.new(- status: 'ok',+ status: "ok",status_code: 200,id: fl.origin_id,name: fl.name,@@ -75,7 +75,7 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock dobefore do# Limit the number of retries on tests- described_class.retry_on Storages::Errors::PollingRequired, wait: 1, attempts: 3+ described_class.retry_on Storages::Errors::PollingRequired, wait: 0, attempts: 3source_file_linksend@@ -83,24 +83,28 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock dolet(:inverted_wp_map) { work_package_map.invert }before do- source.update(project_folder_mode: 'manual', project_folder_id: 'awesome-folder')+ source.update(project_folder_mode: "manual", project_folder_id: "awesome-folder")source.reloadend- it 'updates the target project storage project_folder_id to match the source' do- perform_enqueued_jobs(only: described_class) do- described_class.perform_now(source_id: source.id, target_id: target.id, work_package_map:, user_id: user.id)+ it "updates the target project storage project_folder_id to match the source" do+ GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source_id: source.id, target_id: target.id, work_package_map:)end+ GoodJob.perform_inlinetarget.reload+expect(target.project_folder_id).to eq(source.project_folder_id)end- it 'copies all the file link info on the corresponding work_package' do- perform_enqueued_jobs(only: described_class) do- described_class.perform_now(source_id: source.id, target_id: target.id, work_package_map:, user_id: user.id)+ it "copies all the file link info on the corresponding work_package" do+ GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source_id: source.id, target_id: target.id, work_package_map:)end+ GoodJob.perform_inline+WorkPackage.includes(:file_links).where(id: work_package_map.values).find_each do |target_wp|expect(target_wp.file_links.count).to eq(1)@@ -130,24 +134,26 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock doStorages::Peripherals::Registry.stub("#{storage.short_provider_type}.commands.copy_template_folder", ->(storage:, source_path:, destination_path:) {- ServiceResult.success(result: copy_result.with(id: 'copied-folder', polling_url:))+ ServiceResult.success(result: copy_result.with(id: "copied-folder", polling_url:))})end- it 'copies the folders from source to target' do- perform_enqueued_jobs(only: described_class) do- described_class.perform_now(source_id: source.id, target_id: target.id, work_package_map:, user_id: user.id)+ it "copies the folders from source to target" do+ GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source_id: source.id, target_id: target.id, work_package_map:)end+ GoodJob.perform_inlinetarget.reloadexpect(target.project_folder_mode).to eq(source.project_folder_mode)- expect(target.project_folder_id).to eq('copied-folder')+ expect(target.project_folder_id).to eq("copied-folder")end- it 'creates the file links pointing to the newly copied files' do- perform_enqueued_jobs(only: described_class) do- described_class.perform_now(source_id: source.id, target_id: target.id, work_package_map:, user_id: user.id)+ it "creates the file links pointing to the newly copied files" do+ GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source_id: source.id, target_id: target.id, work_package_map:)end+ GoodJob.perform_inlineStorages::FileLink.where(container: target_work_packages).find_each do |file_link|expect(file_link.origin_id).to eq(target_deep_file_ids["#{target.managed_project_folder_path}#{file_link.name}"])@@ -156,6 +162,14 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock doendcontext "when the storage requires polling" do+ let(:copy_incomplete_response) do+ { operation: "ItemCopy", percentageComplete: 27.8, status: "inProgress" }.to_json+ end++ let(:copy_complete_response) do+ { percentageComplete: 100.0, resourceId: "01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM", status: "completed" }.to_json+ end+before doStorages::Peripherals::Registry.stub("#{storage.short_provider_type}.commands.copy_template_folder", ->(storage:, source_path:, destination_path:) {@@ -171,63 +185,51 @@ RSpec.describe Storages::CopyProjectFoldersJob, :job, :webmock do.stub("#{storage.short_provider_type}.queries.files_info", ->(storage:, user:, file_ids:) {ServiceResult.success(result: source_file_infos)})- end- it 'raises a Storages::Errors::PollingRequired' do- perform_enqueued_jobs(only: described_class) do- expect do- described_class.perform_now(source_id: source.id, target_id: target.id, work_package_map:, user_id: user.id)- end.to raise_error Storages::Errors::PollingRequired- end+ stub_request(:get, polling_url)+ .and_return(status: 202, body: copy_complete_response, headers: { "Content-Type" => "application/json" })end- it 'stores the polling url on the current thread' do- job = described_class.new-- perform_enqueued_jobs(only: described_class) do- expect do- job.perform(source_id: source.id, target_id: target.id, work_package_map:, user_id: user.id)- end.to raise_error Storages::Errors::PollingRequired+ it "stores the polling url on the batch properties" do+ batch = GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source_id: source.id, target_id: target.id, work_package_map:)end+ GoodJob.perform_inline+ batch.reload- expect(Thread.current[job.job_id]).to eq(polling_url)+ expect(batch.properties[:polling_url]).to eq(polling_url)+ expect(batch.properties[:polling_state]).to eq(:ongoing)end- context 'when the polling completes' do- let(:copy_incomplete_response) do- { operation: "ItemCopy", percentageComplete: 27.8, status: "inProgress" }.to_json- end-- let(:copy_complete_response) do- { percentageComplete: 100.0, resourceId: "01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM", status: "completed" }.to_json- end-+ context "when the polling completes" dobefore dostub_request(:get, polling_url).and_return(- { status: 202, body: copy_incomplete_response, headers: { 'Content-Type' => 'application/json' } },- { status: 202, body: copy_complete_response, headers: { 'Content-Type' => 'application/json' } }+ { status: 202, body: copy_incomplete_response, headers: { "Content-Type" => "application/json" } },+ { status: 202, body: copy_complete_response, headers: { "Content-Type" => "application/json" } })end- it 'updates the storages' do- perform_enqueued_jobs(only: described_class) do- described_class.perform_now(source_id: source.id, target_id: target.id, work_package_map:, user_id: user.id)+ it "updates the storages" do+ GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source_id: source.id, target_id: target.id, work_package_map:)end+ GoodJob.perform_inlinetarget.reloadexpect(target.project_folder_mode).to eq(source.project_folder_mode)- expect(target.project_folder_id).to eq('01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM')+ expect(target.project_folder_id).to eq("01MOWKYVJML57KN2ANMBA3JZJS2MBGC7KM")end- it 'handles re-enqueues and polling' do- perform_enqueued_jobs(only: described_class) do- described_class.perform_now(source_id: source.id, target_id: target.id, work_package_map:, user_id: user.id)+ it "handles re-enqueues and polling" do+ GoodJob::Batch.enqueue(user:) do+ described_class.perform_later(source_id: source.id, target_id: target.id, work_package_map:)end+ GoodJob.perform_inline+ job = GoodJob::Job.order(:created_at).last- performed_job = ActiveJob::Base.queue_adapter.performed_jobs.find { |jobs| jobs['job_class'] == described_class.to_s }- expect(performed_job['exception_executions']['[Storages::Errors::PollingRequired]']).to eq(2)- expect(performed_job['executions']).to eq(1)+ expect(job.executions_count).to eq(3)+ expect(job.serialized_params["exception_executions"]["[Storages::Errors::PollingRequired]"]).to eq(2)endendenddiff --git a/spec/support/shared/with_good_job.rb b/spec/support/shared/with_good_job.rbindex 3d72211e303..c64d2fc3ac2 100644--- a/spec/support/shared/with_good_job.rb+++ b/spec/support/shared/with_good_job.rb@@ -49,4 +49,22 @@ RSpec.configure do |config|good_job_adapter&.shutdownendend++ config.around(:example, :with_good_job_batches) do |example|+ original_adapter = ActiveJob::Base.queue_adapter+ good_job_adapter = GoodJob::Adapter.new(execution_mode: :external)++ classes = Array(example.metadata[:with_good_job_batches])+ unless classes.all? { |cls| cls <= ApplicationJob }+ raise ArgumentError.new("Pass the ApplicationJob subclasses you want to disable the test adapter on.")+ end++ classes.each(&:disable_test_adapter)+ ActiveJob::Base.queue_adapter = good_job_adapter+ example.run+ ensure+ ActiveJob::Base.queue_adapter = original_adapter+ classes.each { |cls| cls.enable_test_adapter(original_adapter) }+ good_job_adapter.shutdown+ endend