Skip to content

Commit

Permalink
Make sure the polling url is scoped under the source project storage id
Browse files Browse the repository at this point in the history
  • Loading branch information
mereghost committed Mar 25, 2024
1 parent 5bdd1ca commit d25eb60
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
33 changes: 20 additions & 13 deletions modules/storages/app/workers/storages/copy_project_folders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,51 +36,54 @@ class CopyProjectFoldersJob < ApplicationJob
discard_on HTTPX::HTTPError

def perform(source:, target:, work_packages_map:)
@source = source
user = batch.properties[:user]

project_folder_result = results_from_polling || initiate_copy(source, target)
project_folder_result = results_from_polling || initiate_copy(target)

ProjectStorages::UpdateService.new(user:, model: target)
.call(project_folder_id: project_folder_result.result.id,
project_folder_mode: source.project_folder_mode)
.on_failure { |failed| log_failure(failed) and return failed }

FileLinks::CopyFileLinksService.call(source:, target:, user:, work_packages_map:)
FileLinks::CopyFileLinksService.call(source: @source, target:, user:, work_packages_map:)
end

private

def initiate_copy(source, target)
def initiate_copy(target)
ProjectStorages::CopyProjectFoldersService
.call(source:, target:)
.on_success { |success| prepare_polling(success.result, source) }
.call(source: @source, target:)
.on_success { |success| prepare_polling(success.result) }
end

def prepare_polling(result, source)
def prepare_polling(result)
return unless result.requires_polling?

batch.properties.merge!(polling_state: :ongoing, polling_url: result.polling_url)
batch.properties[:polling] ||= {}
batch.properties[:polling][@source.id.to_s] = { polling_state: :ongoing, polling_url: result.polling_url }
batch.save

raise Errors::PollingRequired, "Storage #{source.storage.name} requires polling"
raise Errors::PollingRequired, "Storage #{@source.storage.name} requires polling"
end

def polling?
batch.properties[:polling_state] == :ongoing
polling_info[:polling_url] == :ongoing
end

def results_from_polling
return unless batch.properties[:polling_url]
return unless polling_info

response = OpenProject.httpx.get(batch.properties[:polling_url]).json(symbolize_keys: true)
response = OpenProject.httpx.get(polling_info[:polling_url]).json(symbolize_keys: true)

if response[:status] != "completed"
batch.properties[:polling_state] == :ongoing
polling_info[:polling_state] == :ongoing
batch.save

raise(Errors::PollingRequired, "#{job_id} Polling not completed yet")
end

batch.properties[:polling_state] == :completed
polling_info[:polling_state] == :completed
batch.save

result = Peripherals::StorageInteraction::ResultData::CopyTemplateFolder.new(response[:resourceId], nil, false)
Expand All @@ -92,5 +95,9 @@ def log_failure(failed)

OpenProject.logger.warn failed.errors.inspect.to_s
end

def polling_info
batch.properties.dig(:polling, @source.id.to_s)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@
GoodJob.perform_inline
batch.reload

expect(batch.properties[:polling_url]).to eq(polling_url)
expect(batch.properties[:polling_state]).to eq(:ongoing)
expect(batch.properties.dig(:polling, source.id.to_s, :polling_url)).to eq(polling_url)
expect(batch.properties.dig(:polling, source.id.to_s, :polling_state)).to eq(:ongoing)
end

context "when the polling completes" do
Expand Down

0 comments on commit d25eb60

Please sign in to comment.