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

Added retry_on handling to ManageNextcloudIntegrationJob

Andreas Pfohl45e5cb078810
diff --git a/modules/storages/app/common/storages/errors.rb b/modules/storages/app/common/storages/errors.rb
index 3e1b2cefa93..00d5fb82415 100644
--- a/modules/storages/app/common/storages/errors.rb
+++ b/modules/storages/app/common/storages/errors.rb
@@ -48,6 +48,16 @@ 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
+
def self.registry_error_for(key)
case key.split('.')
in [storage, 'contracts', model]
diff --git a/modules/storages/app/workers/storages/manage_nextcloud_integration_job.rb b/modules/storages/app/workers/storages/manage_nextcloud_integration_job.rb
index 93484254b98..3ae88df231c 100644
--- a/modules/storages/app/workers/storages/manage_nextcloud_integration_job.rb
+++ b/modules/storages/app/workers/storages/manage_nextcloud_integration_job.rb
@@ -31,6 +31,16 @@ module Storages
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
+
good_job_control_concurrency_with(
total_limit: 2,
enqueue_limit: 1,
@@ -87,7 +97,7 @@ module Storages
result.match(
on_success: ->(_) { OpenProject::Notifications.send(OpenProject::Events::STORAGE_TURNED_HEALTHY, storage:) },
on_failure: ->(errors) do
- OpenProject::Notifications.send(OpenProject::Events::STORAGE_TURNED_UNHEALTHY, storage:, reason: errors.to_s)
+ raise ::Storages::Errors::IntegrationJobError.new(storage:, errors:)
end
)
end
diff --git a/modules/storages/spec/workers/storages/manage_nextcloud_integration_job_spec.rb b/modules/storages/spec/workers/storages/manage_nextcloud_integration_job_spec.rb
index 7e150dbed67..aecea8f61d3 100644
--- a/modules/storages/spec/workers/storages/manage_nextcloud_integration_job_spec.rb
+++ b/modules/storages/spec/workers/storages/manage_nextcloud_integration_job_spec.rb
@@ -134,12 +134,16 @@ RSpec.describe Storages::ManageNextcloudIntegrationJob, :webmock, type: :job do
storage1.reload
end.to(
change(storage1, :health_changed_at).to(Time.now.utc)
- .and(change(storage1, :health_status).from("pending").to("healthy"))
+ .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)
@@ -147,12 +151,42 @@ RSpec.describe Storages::ManageNextcloudIntegrationJob, :webmock, type: :job do
Timecop.freeze("2023-03-14T15:17:00Z") do
expect do
- subject
+ 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"))
+ .and(change(storage1, :health_status).from("pending").to("unhealthy"))
+ .and(change(storage1, :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(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(5)
+ 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
end