Skip to content

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

Moves the event emission to #perform

Marcello Rocha8b92d83511bf
diff --git a/modules/storages/app/common/storages/errors.rb b/modules/storages/app/common/storages/errors.rb
index 00d5fb82415..0150d948d25 100644
--- a/modules/storages/app/common/storages/errors.rb
+++ b/modules/storages/app/common/storages/errors.rb
@@ -48,25 +48,17 @@ module Storages
end
end
- class IntegrationJobError < BaseError
- attr_reader :errors, :storage
-
- def initialize(storage:, errors:)
- super(errors.log_message)
- @storage = storage
- @errors = errors
- end
- end
+ class IntegrationJobError < BaseError; end
def self.registry_error_for(key)
- case key.split('.')
- in [storage, 'contracts', model]
+ case key.split(".")
+ in [storage, "contracts", model]
MissingContract.new("No #{model} contract defined for provider: #{storage.camelize}")
- in [storage, 'commands' | 'queries' => type, operation]
+ in [storage, "commands" | "queries" => type, operation]
OperationNotSupported.new(
"#{type.singularize.capitalize} #{operation} not supported by provider: #{storage.camelize}"
)
- in [storage, 'models', object]
+ in [storage, "models", object]
MissingModel.new("Model #{object} not registered for provider: #{storage.camelize}")
else
ResolverStandardError.new("Cannot resolve key #{key}.")
diff --git a/modules/storages/app/workers/storages/manage_storage_integrations_job.rb b/modules/storages/app/workers/storages/manage_storage_integrations_job.rb
index 1588ab8b08c..93c3d2a066d 100644
--- a/modules/storages/app/workers/storages/manage_storage_integrations_job.rb
+++ b/modules/storages/app/workers/storages/manage_storage_integrations_job.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
@@ -29,17 +31,8 @@
module Storages
class ManageStorageIntegrationsJob < ApplicationJob
include GoodJob::ActiveJobExtensions::Concurrency
- using ::Storages::Peripherals::ServiceResultRefinements
-
- retry_on ::Storages::Errors::IntegrationJobError, attempts: 5 do |job, errors|
- if job.executions >= 5
- OpenProject::Notifications.send(
- OpenProject::Events::STORAGE_TURNED_UNHEALTHY,
- storage: errors.storage,
- reason: errors.errors.to_s
- )
- end
- end
+
+ retry_on ::Storages::Errors::IntegrationJobError, attempts: 5
good_job_control_concurrency_with(
total_limit: 2,
@@ -94,33 +87,45 @@ module Storages
end
def perform
- find_storages do |storage|
+ failed_sync = []
+ storage_scope.find_each do |storage|
next unless storage.configured?
- result = service_for(storage).call(storage)
- result.match(
- on_success: ->(_) { OpenProject::Notifications.send(OpenProject::Events::STORAGE_TURNED_HEALTHY, storage:) },
- on_failure: ->(errors) do
- raise ::Storages::Errors::IntegrationJobError.new(storage:, errors:)
- end
- )
+ result = sync_service_result_for(storage)
+ result.on_success { OpenProject::Notifications.send(OpenProject::Events::STORAGE_TURNED_HEALTHY, storage:) }
+ result.on_failure { |failed| failed_sync << { storage:, errors: failed.errors } }
end
+
+ return emit_error_events(failed_sync) if executions >= 5
+
+ raise Errors::IntegrationJobError, failed_sync.first[:errors].to_s if failed_sync.any?
end
private
- def find_storages(&)
- ::Storages::Storage
- .automatic_management_enabled
- .includes(:oauth_client)
- .find_each(&)
+ def emit_error_events(failed_sync)
+ failed_sync.each do |hash|
+ OpenProject::Notifications.send(OpenProject::Events::STORAGE_TURNED_UNHEALTHY,
+ storage: hash[:storage], reason: hash[:errors].to_s)
+ end
end
- def service_for(storage)
- return NextcloudGroupFolderPropertiesSyncService if storage.provider_type_nextcloud?
- return OneDriveManagedFolderSyncService if storage.provider_type_one_drive?
+ def storage_scope
+ ::Storages::Storage.automatic_management_enabled.includes(:oauth_client)
+ end
- raise "Unknown Storage"
+ def sync_service_result_for(storage)
+ # For the sake of making this easier to expand, we should register these and
+ # use the registry to figure out which one to use
+
+ case storage.short_provider_type
+ when "nextcloud"
+ NextcloudGroupFolderPropertiesSyncService.call(storage)
+ when "one_drive"
+ OneDriveManagedFolderSyncService.call(storage)
+ else
+ raise "Unknown Storage Type"
+ end
end
end
end
diff --git a/modules/storages/spec/workers/storages/manage_storage_integrations_job_spec.rb b/modules/storages/spec/workers/storages/manage_storage_integrations_job_spec.rb
index cb46183921d..620e9d87dcf 100644
--- a/modules/storages/spec/workers/storages/manage_storage_integrations_job_spec.rb
+++ b/modules/storages/spec/workers/storages/manage_storage_integrations_job_spec.rb
@@ -65,8 +65,6 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
describe ".disable_cron_job_if_needed" do
before { ActiveJob::Base.disable_test_adapter }
- subject { described_class.disable_cron_job_if_needed }
-
context "when there is an active nextcloud project storage" do
shared_let(:storage1) { create(:nextcloud_storage, :as_automatically_managed) }
shared_let(:project_storage) { create(:project_storage, :as_automatically_managed, storage: storage1) }
@@ -78,7 +76,7 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
expect(good_job_setting.key).to eq("cron_keys_disabled")
expect(good_job_setting.value).to eq(["Storages::ManageStorageIntegrationsJob"])
- expect { subject }.not_to change(GoodJob::Setting, :count).from(1)
+ expect { described_class.disable_cron_job_if_needed }.not_to change(GoodJob::Setting, :count).from(1)
good_job_setting.reload
expect(good_job_setting.key).to eq("cron_keys_disabled")
@@ -88,7 +86,7 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
it "does nothing if the cron_job is not disabled" do
expect(GoodJob::Setting.cron_key_enabled?(described_class::CRON_JOB_KEY)).to be(true)
- expect { subject }.not_to change(GoodJob::Setting, :count).from(0)
+ expect { described_class.disable_cron_job_if_needed }.not_to change(GoodJob::Setting, :count).from(0)
expect(GoodJob::Setting.cron_key_enabled?(described_class::CRON_JOB_KEY)).to be(true)
end
@@ -96,7 +94,7 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
context "when there is no active nextcloud project storage" do
it "disables the cron job" do
- expect { subject }.to change(GoodJob::Setting, :count).from(0).to(1)
+ expect { described_class.disable_cron_job_if_needed }.to change(GoodJob::Setting, :count).from(0).to(1)
good_job_setting = GoodJob::Setting.first
expect(good_job_setting.key).to eq("cron_keys_disabled")
@@ -107,8 +105,9 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
describe ".perform" do
let(:storage1) { create(:nextcloud_storage_configured, :as_automatically_managed) }
+ let(:storage2) { create(:sharepoint_dev_drive_storage, :as_automatically_managed) }
- subject { described_class.new.perform }
+ subject(:job_instance) { described_class.new }
it "calls NextcloudGroupFolderPropertiesSyncService for each automatically managed storage" do
storage2 = create(:nextcloud_storage, :as_not_automatically_managed)
@@ -117,7 +116,7 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
allow(Storages::NextcloudGroupFolderPropertiesSyncService)
.to receive(:call).with(storage1).and_return(ServiceResult.success)
- subject
+ job_instance.perform
expect(Storages::NextcloudGroupFolderPropertiesSyncService).to have_received(:call).with(storage1).once
expect(Storages::NextcloudGroupFolderPropertiesSyncService).not_to have_received(:call).with(storage2)
@@ -130,7 +129,7 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
Timecop.freeze("2023-03-14T15:17:00Z") do
expect do
- subject
+ job_instance.perform
storage1.reload
end.to(
change(storage1, :health_changed_at).to(Time.now.utc)
@@ -175,7 +174,7 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
expect(described_class
.queue_adapter.performed_jobs
- .last.dig("exception_executions", "[Storages::Errors::IntegrationJobError]")).to eq(5)
+ .last.dig("exception_executions", "[Storages::Errors::IntegrationJobError]")).to eq(4)
end
it "sends a notification after the maximum number of attempts" do
@@ -189,6 +188,19 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
reason: "custom_error"
)
end
+
+ it "does not interrupt the processing" do
+ allow(Storages::NextcloudGroupFolderPropertiesSyncService)
+ .to receive(:call)
+ .with(storage1)
+ .and_return(ServiceResult.failure(errors: Storages::StorageError.new(code: :error, log_message: "Not working")))
+
+ allow(Storages::OneDriveManagedFolderSyncService).to receive(:call).with(storage2).and_return(ServiceResult.success)
+
+ expect { job_instance.perform }.to raise_error(Storages::Errors::IntegrationJobError)
+
+ expect(Storages::OneDriveManagedFolderSyncService).to have_received(:call).with(storage2)
+ end
end
end
end