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

Introduces a job that runs on a per storage basis Cleans up the big job Update Event handling Extracts the debounce logic to a module, so it can be reused by other jobs

Marcello Rocha98084027f57e
diff --git a/app/workers/debounceable_job.rb b/app/workers/debounceable_job.rb
new file mode 100644
index 00000000000..1b4c94998ae
--- /dev/null
+++ b/app/workers/debounceable_job.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module DebounceableJob
+ # This module is generalizes the debounce logic that was originally used on {Storages::ManageStorageIntegrationsJob}
+ # Basically it ensures that a thread only queues one job per interval.
+
+ # it depends on the class method `key` being implemented. The method will receive all the arguments
+ # used to invoke the job to construct the RequestStore key.
+ SINGLE_THREAD_DEBOUNCE_TIME = 4.seconds
+
+ def debounce(*, **)
+ store_key = key(*, **)
+ timestamp = RequestStore.store[store_key]
+
+ return false if timestamp.present? && (timestamp + SINGLE_THREAD_DEBOUNCE_TIME) > Time.current
+
+ result = set(wait: 5.seconds).perform_later(*, **)
+ RequestStore.store[store_key] = Time.current
+ result
+ end
+end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index 2e4a06eeda7..07e02e575ab 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -83,6 +83,8 @@ module Storages
scope :automatic_management_enabled, -> { where("provider_fields->>'automatically_managed' = 'true'") }
+ scope :in_project, ->(project_id) { joins(project_storages: :project).where(project_storages: { project_id: }) }
+
enum health_status: {
pending: "pending",
healthy: "healthy",
diff --git a/modules/storages/app/services/storages/project_storages/create_service.rb b/modules/storages/app/services/storages/project_storages/create_service.rb
index 00d51b85bf1..1b508503b85 100644
--- a/modules/storages/app/services/storages/project_storages/create_service.rb
+++ b/modules/storages/app/services/storages/project_storages/create_service.rb
@@ -39,7 +39,8 @@ module Storages::ProjectStorages
add_historical_data(service_call) if project_folder_mode != :inactive
OpenProject::Notifications.send(
OpenProject::Events::PROJECT_STORAGE_CREATED,
- project_folder_mode:
+ project_folder_mode:,
+ storage: project_storage.storage
)
service_call
diff --git a/modules/storages/app/services/storages/project_storages/delete_service.rb b/modules/storages/app/services/storages/project_storages/delete_service.rb
index 6538a78e45e..afd7cbb34d9 100644
--- a/modules/storages/app/services/storages/project_storages/delete_service.rb
+++ b/modules/storages/app/services/storages/project_storages/delete_service.rb
@@ -50,7 +50,8 @@ module Storages::ProjectStorages
delete_associated_file_links
OpenProject::Notifications.send(
OpenProject::Events::PROJECT_STORAGE_DESTROYED,
- project_folder_mode: deletion_result.result.project_folder_mode.to_sym
+ project_folder_mode: deletion_result.result.project_folder_mode.to_sym,
+ storage: deletion_result.result.storage
)
end
end
diff --git a/modules/storages/app/services/storages/project_storages/update_service.rb b/modules/storages/app/services/storages/project_storages/update_service.rb
index da5f97e1045..5f39e3ea30b 100644
--- a/modules/storages/app/services/storages/project_storages/update_service.rb
+++ b/modules/storages/app/services/storages/project_storages/update_service.rb
@@ -40,7 +40,8 @@ module Storages::ProjectStorages
add_historical_data(service_call) if project_folder_mode != :inactive
OpenProject::Notifications.send(
OpenProject::Events::PROJECT_STORAGE_UPDATED,
- project_folder_mode:
+ project_folder_mode:,
+ storage: project_storage.storage
)
service_call
diff --git a/modules/storages/app/workers/storages/automatically_managed_storage_sync_job.rb b/modules/storages/app/workers/storages/automatically_managed_storage_sync_job.rb
new file mode 100644
index 00000000000..ef942bd6790
--- /dev/null
+++ b/modules/storages/app/workers/storages/automatically_managed_storage_sync_job.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages
+ class AutomaticallyManagedStorageSyncJob < ApplicationJob
+ include GoodJob::ActiveJobExtensions::Concurrency
+ extend ::DebounceableJob
+
+ queue_with_priority :above_normal
+
+ good_job_control_concurrency_with(
+ total_limit: 2,
+ enqueue_limit: 1,
+ perform_limit: 1,
+ key: -> { "StorageSyncJob-#{arguments.last.short_provider_type}-#{arguments.last.id}" }
+ )
+
+ retry_on GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError, wait: 5, attempts: 10
+
+ retry_on Errors::IntegrationJobError, attempts: 5 do |job, error|
+ if job.executions >= 5
+ OpenProject::Notifications.send(
+ OpenProject::Events::STORAGE_TURNED_UNHEALTHY, storage: job.arguments.last, reason: error.message
+ )
+ end
+ end
+
+ def self.key(storage) = "sync-#{storage.short_provider_type}-#{storage.id}"
+
+ def perform(storage)
+ return unless storage.configured? && storage.automatically_managed?
+
+ sync_result = case storage.short_provider_type
+ when "nextcloud"
+ NextcloudGroupFolderPropertiesSyncService.call(storage)
+ when "one_drive"
+ OneDriveManagedFolderSyncService.call(storage)
+ else
+ raise "Unknown Storage Type"
+ end
+
+ sync_result.on_failure { raise Errors::IntegrationJobError, sync_result.errors.to_s }
+ sync_result.on_success { OpenProject::Notifications.send(OpenProject::Events::STORAGE_TURNED_HEALTHY, storage:) }
+ end
+ end
+end
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 93c3d2a066d..48e9d5129c4 100644
--- a/modules/storages/app/workers/storages/manage_storage_integrations_job.rb
+++ b/modules/storages/app/workers/storages/manage_storage_integrations_job.rb
@@ -31,8 +31,7 @@
module Storages
class ManageStorageIntegrationsJob < ApplicationJob
include GoodJob::ActiveJobExtensions::Concurrency
-
- retry_on ::Storages::Errors::IntegrationJobError, attempts: 5
+ extend ::DebounceableJob
good_job_control_concurrency_with(
total_limit: 2,
@@ -41,35 +40,15 @@ module Storages
)
retry_on GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError,
- wait: 5.minutes,
- attempts: 3
+ wait: 5,
+ attempts: 20
- SINGLE_THREAD_DEBOUNCE_TIME = 4.seconds.freeze
KEY = :manage_nextcloud_integration_job_debounce_happened_at
CRON_JOB_KEY = :"Storages::ManageStorageIntegrationsJob"
queue_with_priority :above_normal
class << self
- def debounce
- if debounce_happened_in_current_thread_recently?
- false
- else
- # TODO:
- # Why there is 5 seconds delay?
- # it is like that because for 1 thread and if there is no delay more than
- # SINGLE_THREAD_DEBOUNCE_TIME(4.seconds)
- # then some events can be lost
- #
- # Possibly "true" solutions are:
- # 1. have after_request middleware to schedule one job after a request cycle
- # 2. use concurrent ruby to have 'true' debounce.
- result = set(wait: 5.seconds).perform_later
- RequestStore.store[KEY] = Time.current
- result
- end
- end
-
def disable_cron_job_if_needed
if ::Storages::ProjectStorage.active_automatically_managed.exists?
GoodJob::Setting.cron_key_enable(CRON_JOB_KEY) unless GoodJob::Setting.cron_key_enabled?(CRON_JOB_KEY)
@@ -78,53 +57,12 @@ module Storages
end
end
- private
-
- def debounce_happened_in_current_thread_recently?
- timestamp = RequestStore.store[KEY]
- timestamp.present? && (timestamp + SINGLE_THREAD_DEBOUNCE_TIME) > Time.current
- end
+ def key = KEY
end
def perform
- failed_sync = []
- storage_scope.find_each do |storage|
- next unless storage.configured?
-
- 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 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 storage_scope
- ::Storages::Storage.automatic_management_enabled.includes(:oauth_client)
- end
-
- 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"
+ Storage.automatic_management_enabled.find_each do |storage|
+ AutomaticallyManagedStorageSyncJob.perform_later(storage)
end
end
end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index df79dca94ec..a0115a9ffa4 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -55,14 +55,23 @@ module OpenProject::Storages
[
OpenProject::Events::MEMBER_CREATED,
OpenProject::Events::MEMBER_UPDATED,
- OpenProject::Events::MEMBER_DESTROYED,
- OpenProject::Events::PROJECT_UPDATED,
- OpenProject::Events::PROJECT_RENAMED,
- OpenProject::Events::PROJECT_ARCHIVED,
- OpenProject::Events::PROJECT_UNARCHIVED
+ OpenProject::Events::MEMBER_DESTROYED
].each do |event|
- OpenProject::Notifications.subscribe(event) do |_payload|
- ::Storages::ManageStorageIntegrationsJob.debounce
+ OpenProject::Notifications.subscribe(event) do |payload|
+ ::Storages::Storage.in_project(payload[:member].project_id).find_each do |storage|
+ ::Storages::AutomaticallyManagedStorageSyncJob.debounce(storage)
+ end
+ end
+ end
+
+ [OpenProject::Events::PROJECT_UPDATED,
+ OpenProject::Events::PROJECT_RENAMED,
+ OpenProject::Events::PROJECT_ARCHIVED,
+ OpenProject::Events::PROJECT_UNARCHIVED].each do |event|
+ OpenProject::Notifications.subscribe(event) do |payload|
+ ::Storages::Storage.in_project(payload[:project].id).find_each do |storage|
+ ::Storages::AutomaticallyManagedStorageSyncJob.debounce(storage)
+ end
end
end
@@ -97,7 +106,7 @@ module OpenProject::Storages
].each do |event|
OpenProject::Notifications.subscribe(event) do |payload|
if payload[:project_folder_mode] == :automatic
- ::Storages::ManageStorageIntegrationsJob.debounce
+ ::Storages::AutomaticallyManagedStorageSyncJob.debounce(payload[:storage])
::Storages::ManageStorageIntegrationsJob.disable_cron_job_if_needed
end
end
@@ -106,13 +115,13 @@ module OpenProject::Storages
OpenProject::Notifications.subscribe(
::OpenProject::Events::STORAGE_TURNED_UNHEALTHY
) do |payload|
- Storages::HealthService.new(storage: payload[:storage]).unhealthy(reason: payload[:reason])
+ ::Storages::HealthService.new(storage: payload[:storage]).unhealthy(reason: payload[:reason])
end
OpenProject::Notifications.subscribe(
::OpenProject::Events::STORAGE_TURNED_HEALTHY
) do |payload|
- Storages::HealthService.new(storage: payload[:storage]).healthy
+ ::Storages::HealthService.new(storage: payload[:storage]).healthy
end
end
end
diff --git a/modules/storages/spec/services/storages/project_storages/shared_event_gun_examples.rb b/modules/storages/spec/services/storages/project_storages/shared_event_gun_examples.rb
index 7039fbf212a..d32691286ca 100644
--- a/modules/storages/spec/services/storages/project_storages/shared_event_gun_examples.rb
+++ b/modules/storages/spec/services/storages/project_storages/shared_event_gun_examples.rb
@@ -41,7 +41,7 @@ RSpec.shared_examples "an event gun" do |event|
subject
expect(OpenProject::Notifications).to(
- have_received(:send).with(event, project_folder_mode: mode)
+ have_received(:send).with(event, project_folder_mode: mode, storage: model_instance.storage)
)
end
end
diff --git a/modules/storages/spec/workers/storages/automatically_managed_storage_sync_job_spec.rb b/modules/storages/spec/workers/storages/automatically_managed_storage_sync_job_spec.rb
new file mode 100644
index 00000000000..81a961f2b4c
--- /dev/null
+++ b/modules/storages/spec/workers/storages/automatically_managed_storage_sync_job_spec.rb
@@ -0,0 +1,138 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require_module_spec_helper
+
+RSpec.describe Storages::AutomaticallyManagedStorageSyncJob, type: :job do
+ let(:managed_nextcloud) { create(:nextcloud_storage_configured, :as_automatically_managed) }
+
+ describe ".debounce" do
+ context "when has been debounced by other thread" do
+ it "does not change the number of enqueued jobs" do
+ expect(performed_jobs.count).to eq(0)
+ expect(described_class.debounce(managed_nextcloud).successfully_enqueued?).to be(true)
+ expect(described_class.debounce(managed_nextcloud)).to be(false)
+ expect(enqueued_jobs.count).to eq(1)
+
+ expect { described_class.debounce(managed_nextcloud) }.not_to change(enqueued_jobs, :count)
+ end
+ end
+
+ context "when has not been debounced by other thread" do
+ before { RequestStore.delete("sync-nextcloud-#{managed_nextcloud.id}") }
+
+ it "schedules a job" do
+ expect { described_class.debounce(managed_nextcloud) }.to change(enqueued_jobs, :count).from(0).to(1)
+ end
+ end
+ end
+
+ describe ".perform" do
+ subject(:job_instance) { described_class.new }
+
+ it "only runs for automatically managed storages" do
+ unmanaged_nextcloud = create(:nextcloud_storage_configured, :as_not_automatically_managed)
+
+ allow(Storages::NextcloudGroupFolderPropertiesSyncService)
+ .to receive(:call).with(managed_nextcloud).and_return(ServiceResult.success)
+
+ job_instance.perform(managed_nextcloud)
+ job_instance.perform(unmanaged_nextcloud)
+
+ expect(Storages::NextcloudGroupFolderPropertiesSyncService).to have_received(:call).with(managed_nextcloud)
+ expect(Storages::NextcloudGroupFolderPropertiesSyncService).not_to have_received(:call).with(unmanaged_nextcloud)
+ end
+
+ it "marks storage as healthy if sync was successful" do
+ allow(Storages::NextcloudGroupFolderPropertiesSyncService)
+ .to receive(:call).with(managed_nextcloud).and_return(ServiceResult.success)
+
+ Timecop.freeze("2023-03-14T15:17:00Z") do
+ expect do
+ job_instance.perform(managed_nextcloud)
+ managed_nextcloud.reload
+ end.to(
+ change(managed_nextcloud, :health_changed_at).to(Time.now.utc)
+ .and(change(managed_nextcloud, :health_status).from("pending").to("healthy"))
+ )
+ end
+ end
+
+ it "marks storage as unhealthy if sync was unsuccessful" do
+ job = class_double(Storages::HealthStatusMailerJob)
+ allow(Storages::HealthStatusMailerJob).to receive(:set).and_return(job)
+ allow(job).to receive(:perform_later)
+
+ allow(Storages::NextcloudGroupFolderPropertiesSyncService)
+ .to receive(:call)
+ .with(managed_nextcloud)
+ .and_return(ServiceResult.failure(errors: Storages::StorageError.new(code: :not_found)))
+
+ Timecop.freeze("2023-03-14T15:17:00Z") do
+ expect do
+ perform_enqueued_jobs { described_class.perform_later(managed_nextcloud) }
+ managed_nextcloud.reload
+ end.to(
+ change(managed_nextcloud, :health_changed_at).to(Time.now.utc)
+ .and(change(managed_nextcloud, :health_status).from("pending").to("unhealthy"))
+ .and(change(managed_nextcloud, :health_reason).from(nil).to("not_found"))
+ )
+ end
+ end
+
+ context "when Storages::Errors::IntegrationJobError is raised" do
+ before do
+ allow(Storages::NextcloudGroupFolderPropertiesSyncService)
+ .to receive(:call).with(managed_nextcloud)
+ .and_return(ServiceResult.failure(errors: Storages::StorageError.new(code: :custom_error)))
+
+ allow(OpenProject::Notifications).to receive(:send)
+ end
+
+ it "retries the job" do
+ perform_enqueued_jobs { described_class.perform_later(managed_nextcloud) }
+ performed_jobs = described_class.queue_adapter.performed_jobs
+
+ expect(performed_jobs.last.dig("exception_executions", "[Storages::Errors::IntegrationJobError]")).to eq(5)
+ end
+
+ it "sends a notification after the maximum number of attempts" do
+ perform_enqueued_jobs { described_class.perform_later(managed_nextcloud) }
+
+ expect(OpenProject::Notifications).to have_received(:send).with(
+ OpenProject::Events::STORAGE_TURNED_UNHEALTHY,
+ storage: managed_nextcloud,
+ reason: "custom_error"
+ )
+ 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 620e9d87dcf..5ae11b91dd1 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
@@ -31,7 +31,7 @@
require "spec_helper"
require_module_spec_helper
-RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
+RSpec.describe Storages::ManageStorageIntegrationsJob, type: :job do
describe ".debounce" do
context "when has been debounced by other thread" do
before { ActiveJob::Base.disable_test_adapter }
@@ -104,103 +104,14 @@ RSpec.describe Storages::ManageStorageIntegrationsJob, :webmock, type: :job do
end
describe ".perform" do
- let(:storage1) { create(:nextcloud_storage_configured, :as_automatically_managed) }
- let(:storage2) { create(:sharepoint_dev_drive_storage, :as_automatically_managed) }
-
- subject(:job_instance) { described_class.new }
-
- it "calls NextcloudGroupFolderPropertiesSyncService for each automatically managed storage" do
- storage2 = create(:nextcloud_storage, :as_not_automatically_managed)
- storage3 = create(:nextcloud_storage, :as_automatically_managed)
-
- allow(Storages::NextcloudGroupFolderPropertiesSyncService)
- .to receive(:call).with(storage1).and_return(ServiceResult.success)
-
- job_instance.perform
-
- expect(Storages::NextcloudGroupFolderPropertiesSyncService).to have_received(:call).with(storage1).once
- expect(Storages::NextcloudGroupFolderPropertiesSyncService).not_to have_received(:call).with(storage2)
- expect(Storages::NextcloudGroupFolderPropertiesSyncService).not_to have_received(:call).with(storage3)
- end
-
- it "marks storage as healthy if sync was successful" do
- allow(Storages::NextcloudGroupFolderPropertiesSyncService)
- .to receive(:call).with(storage1).and_return(ServiceResult.success)
-
- Timecop.freeze("2023-03-14T15:17:00Z") do
- expect do
- job_instance.perform
- storage1.reload
- end.to(
- change(storage1, :health_changed_at).to(Time.now.utc)
- .and(change(storage1, :health_status).from("pending").to("healthy"))
- )
- end
- end
-
- it "marks storage as unhealthy if sync was unsuccessful" do
- job = class_double(Storages::HealthStatusMailerJob)
- allow(Storages::HealthStatusMailerJob).to receive(:set).and_return(job)
- allow(job).to receive(:perform_later)
-
- allow(Storages::NextcloudGroupFolderPropertiesSyncService)
- .to receive(:call)
- .with(storage1)
- .and_return(ServiceResult.failure(errors: Storages::StorageError.new(code: :not_found)))
-
- Timecop.freeze("2023-03-14T15:17:00Z") do
- expect do
- perform_enqueued_jobs { described_class.perform_later }
- storage1.reload
- end.to(
- change(storage1, :health_changed_at).to(Time.now.utc)
- .and(change(storage1, :health_status).from("pending").to("unhealthy"))
- .and(change(storage1, :health_reason).from(nil).to("not_found"))
- )
- end
+ before do
+ create(:nextcloud_storage_configured, :as_automatically_managed)
+ create(:nextcloud_storage, :as_not_automatically_managed)
+ create(:sharepoint_dev_drive_storage, :as_automatically_managed)
end
- context "when Storages::Errors::IntegrationJobError is raised" do
- before do
- allow(Storages::NextcloudGroupFolderPropertiesSyncService)
- .to receive(:call).with(storage1)
- .and_return(ServiceResult.failure(errors: Storages::StorageError.new(code: :custom_error)))
- end
-
- it "retries the job" do
- allow(OpenProject::Notifications).to receive(:send)
-
- perform_enqueued_jobs { described_class.perform_later }
-
- expect(described_class
- .queue_adapter.performed_jobs
- .last.dig("exception_executions", "[Storages::Errors::IntegrationJobError]")).to eq(4)
- end
-
- it "sends a notification after the maximum number of attempts" do
- allow(OpenProject::Notifications).to receive(:send)
-
- perform_enqueued_jobs { described_class.perform_later }
-
- expect(OpenProject::Notifications).to have_received(:send).with(
- OpenProject::Events::STORAGE_TURNED_UNHEALTHY,
- storage: storage1,
- 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
+ it "enqueues a job for each automatically managed storage" do
+ expect { described_class.perform_now }.to change(enqueued_jobs, :count).by(2)
end
end
end
diff --git a/spec/lib/open_project/events_spec.rb b/spec/lib/open_project/events_spec.rb
index da7d20b7251..3ee1fcb8ec9 100644
--- a/spec/lib/open_project/events_spec.rb
+++ b/spec/lib/open_project/events_spec.rb
@@ -38,6 +38,7 @@ RSpec.describe OpenProject::Events do
before do
allow(Storages::ManageStorageIntegrationsJob).to receive(:debounce)
+ allow(Storages::AutomaticallyManagedStorageSyncJob).to receive(:debounce)
end
%w[
@@ -53,16 +54,16 @@ RSpec.describe OpenProject::Events do
it do
subject
- expect(Storages::ManageStorageIntegrationsJob).not_to have_received(:debounce)
+ expect(Storages::AutomaticallyManagedStorageSyncJob).not_to have_received(:debounce)
end
end
context "when payload contains automatic project_folder_mode" do
- let(:payload) { { project_folder_mode: :automatic } }
+ let(:payload) { { project_folder_mode: :automatic, storage: create(:nextcloud_storage) } }
it do
subject
- expect(Storages::ManageStorageIntegrationsJob).to have_received(:debounce)
+ expect(Storages::AutomaticallyManagedStorageSyncJob).to have_received(:debounce).with(payload[:storage])
end
it do
@@ -78,19 +79,36 @@ RSpec.describe OpenProject::Events do
MEMBER_CREATED
MEMBER_UPDATED
MEMBER_DESTROYED
- PROJECT_UPDATED
- PROJECT_RENAMED
- PROJECT_ARCHIVED
- PROJECT_UNARCHIVED
].each do |event|
describe(event) do
+ let(:project_role) { create(:existing_project_role) }
+ let(:project_storage) { create(:project_storage) }
+ let(:member) { create(:work_package_member, roles: [project_role], project: project_storage.project) }
+
+ let(:payload) { { member: } }
+
subject { fire_event(event) }
- let(:payload) { {} }
+ it do
+ subject
+ expect(Storages::AutomaticallyManagedStorageSyncJob).to have_received(:debounce).with(project_storage.storage)
+ end
+ end
+ end
+
+ %w[PROJECT_UPDATED
+ PROJECT_RENAMED
+ PROJECT_ARCHIVED
+ PROJECT_UNARCHIVED].each do |event|
+ describe(event) do
+ let(:project_storage) { create(:project_storage, :as_automatically_managed) }
+ let(:payload) { { project: project_storage.project } }
+
+ subject { fire_event(event) }
it do
subject
- expect(Storages::ManageStorageIntegrationsJob).to have_received(:debounce)
+ expect(Storages::AutomaticallyManagedStorageSyncJob).to have_received(:debounce).with(project_storage.storage)
end
end
end