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

Move storage health_info logic into storage.rb.

Pavel Balashou472fc4458063
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
index fdbdb811166..c8fb5ef0382 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
@@ -66,7 +66,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
when Net::HTTPUnauthorized
Util.error(:unauthorized, 'Outbound request not authorized', error_data)
when Net::HTTPConflict
- Util.error(:conflict, Util.error_text_from_error_data(error_data), error_data)
+ Util.error(:conflict, Util.error_text_from_response(response), error_data)
else
Util.error(:error, 'Outbound request failed', error_data)
end
diff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rb
index f9d75a0a9d2..2fb2ec46e9d 100644
--- a/modules/storages/app/models/storages/nextcloud_storage.rb
+++ b/modules/storages/app/models/storages/nextcloud_storage.rb
@@ -65,26 +65,6 @@ module Storages
end
end
- def mark_as_unhealthy(reason: nil)
- return true if unhealthy?
-
- update(health_status: :unhealthy, health_changed_at: Time.now.utc, health_reason: reason)
- end
-
- def mark_as_healthy
- return true if healthy?
-
- update(health_status: :ok, health_changed_at: Time.now.utc, health_reason: nil)
- end
-
- def healthy?
- health_status == 'ok'
- end
-
- def unhealthy?
- not healthy?
- end
-
def provider_fields_defaults
PROVIDER_FIELDS_DEFAULTS
end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index 8572139c9e3..c414f173d56 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -40,41 +40,23 @@
# db/migrate/20220113144323_create_storage.rb "migration".
module Storages
class Storage < ApplicationRecord
+ PROVIDER_TYPES = [
+ PROVIDER_TYPE_NEXTCLOUD = 'Storages::NextcloudStorage',
+ PROVIDER_TYPE_ONE_DRIVE = 'Storages::OneDriveStorage'
+ ].freeze
+
self.inheritance_column = :provider_type
- # One Storage can have multiple FileLinks, representing external files.
- #
- # FileLink deletion is done:
- # - through a on_delete: :cascade at the database level when deleting a
- # Storage
- # - through a before_destroy hook at the application level when deleting a
- # ProjectStorage
has_many :file_links, class_name: 'Storages::FileLink'
- # Basically every OpenProject object has a creator
belongs_to :creator, class_name: 'User'
- # A project manager can enable/disable Storages per project.
has_many :project_storages, dependent: :destroy, class_name: 'Storages::ProjectStorage'
- # We can get the list of projects with this Storage enabled.
has_many :projects, through: :project_storages
- # The OAuth client credentials that OpenProject will use to obtain user specific
- # access tokens from the storage server, i.e a Nextcloud serer.
has_one :oauth_client, as: :integration, dependent: :destroy
has_one :oauth_application, class_name: '::Doorkeeper::Application', as: :integration, dependent: :destroy
- store_attribute :health_info, :status, :string, prefix: :health
- store_attribute :health_info, :changed_at, :datetime, prefix: :health
- store_attribute :health_info, :reason, :string, prefix: :health
-
- PROVIDER_TYPES = [
- PROVIDER_TYPE_NEXTCLOUD = 'Storages::NextcloudStorage',
- PROVIDER_TYPE_ONE_DRIVE = 'Storages::OneDriveStorage'
- ].freeze
-
validates_uniqueness_of :host, allow_nil: true
validates_uniqueness_of :name
- # Creates a scope of all storages, which belong to a project the user is a member
- # and has the permission ':view_file_links'
scope :visible, ->(user = User.current) do
if user.allowed_in_any_project?(:manage_storages_in_project)
all
@@ -91,6 +73,10 @@ module Storages
where.not(id: project.project_storages.pluck(:storage_id))
end
+ store_attribute :health_info, :status, :string, prefix: :health
+ store_attribute :health_info, :changed_at, :datetime, prefix: :health
+ store_attribute :health_info, :reason, :string, prefix: :health
+
def self.shorten_provider_type(provider_type)
case /Storages::(?'provider_name'.*)Storage/.match(provider_type)
in provider_name:
@@ -102,6 +88,26 @@ module Storages
end
end
+ def mark_as_unhealthy(reason: nil)
+ return true if unhealthy?
+
+ update(health_status: 'unhealthy', health_changed_at: Time.now.utc, health_reason: reason)
+ end
+
+ def mark_as_healthy
+ return true if healthy?
+
+ update(health_status: 'healthy', health_changed_at: Time.now.utc, health_reason: nil)
+ end
+
+ def healthy?
+ health_status == 'healthy'
+ end
+
+ def unhealthy?
+ health_status == 'unhealthy'
+ end
+
def configured?
configuration_checks.values.all?
end
diff --git a/modules/storages/app/workers/storages/manage_nextcloud_integration_cron_job.rb b/modules/storages/app/workers/storages/manage_nextcloud_integration_cron_job.rb
index 7340a614a1a..e871d9182cb 100644
--- a/modules/storages/app/workers/storages/manage_nextcloud_integration_cron_job.rb
+++ b/modules/storages/app/workers/storages/manage_nextcloud_integration_cron_job.rb
@@ -30,23 +30,10 @@
module Storages
class ManageNextcloudIntegrationCronJob < Cron::CronJob
- using Peripherals::ServiceResultRefinements
+ include ManageNextcloudIntegrationJobMixin
queue_with_priority :low
self.cron_expression = '*/5 * * * *'
-
- def perform
- OpenProject::Mutex.with_advisory_lock(NextcloudStorage, 'sync_all_group_folders', timeout_seconds: 0, transaction: false) do
- NextcloudStorage.automatically_managed.includes(:oauth_client).find_each do |storage|
- result = GroupFolderPropertiesSyncService.call(storage)
- result.match(
- on_success: -> { storage.mark_as_healthy },
- on_failure: ->(error) { storage.mark_as_unhealthy(reason: error.code) }
- )
- end
- true
- end
- end
end
end
diff --git a/modules/storages/app/workers/storages/manage_nextcloud_integration_events_job.rb b/modules/storages/app/workers/storages/manage_nextcloud_integration_events_job.rb
index 11a342db13f..99247f93a3f 100644
--- a/modules/storages/app/workers/storages/manage_nextcloud_integration_events_job.rb
+++ b/modules/storages/app/workers/storages/manage_nextcloud_integration_events_job.rb
@@ -26,23 +26,28 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Storages::ManageNextcloudIntegrationEventsJob < ApplicationJob
- DEBOUNCE_TIME = 5.seconds.freeze
+module Storages
+ class ManageNextcloudIntegrationEventsJob < ApplicationJob
+ include ManageNextcloudIntegrationJobMixin
- queue_with_priority :above_normal
+ DEBOUNCE_TIME = 5.seconds.freeze
- def self.debounce
- count = Delayed::Job
- .where("handler LIKE ?", "%job_class: #{self}%")
- .where(locked_at: nil)
- .where('run_at <= ?', DEBOUNCE_TIME.from_now)
- .delete_all
- Rails.logger.info("deleted: #{count} jobs")
- set(wait: DEBOUNCE_TIME).perform_later
- end
+ queue_with_priority :above_normal
+
+ def self.debounce
+ count = Delayed::Job
+ .where("handler LIKE ?", "%job_class: #{self}%")
+ .where(locked_at: nil)
+ .where('run_at <= ?', DEBOUNCE_TIME.from_now)
+ .delete_all
+ Rails.logger.info("deleted: #{count} jobs")
+ set(wait: DEBOUNCE_TIME).perform_later
+ end
- def perform
- result = Storages::NextcloudStorage.sync_all_group_folders
- self.class.debounce if result == false
+ def perform
+ lock_obtained = super
+ self.class.debounce unless lock_obtained
+ lock_obtained
+ end
end
end
diff --git a/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb b/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb
new file mode 100644
index 00000000000..01e8fd4025a
--- /dev/null
+++ b/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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
+ module ManageNextcloudIntegrationJobMixin
+ using Peripherals::ServiceResultRefinements
+
+ def perform
+ OpenProject::Mutex.with_advisory_lock(
+ ::Storages::NextcloudStorage,
+ 'sync_all_group_folders',
+ timeout_seconds: 0,
+ transaction: false
+ ) do
+ ::Storages::NextcloudStorage.automatically_managed.includes(:oauth_client).find_each do |storage|
+ result = GroupFolderPropertiesSyncService.call(storage)
+ result.match(
+ on_success: ->(_) do
+ storage.mark_as_healthy
+ end,
+ on_failure: ->(errors) do
+ storage.mark_as_unhealthy(reason: errors.to_s)
+ end
+ )
+ end
+ true
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/workers/storages/manage_nextcloud_integration_cron_job_spec.rb b/modules/storages/spec/workers/storages/manage_nextcloud_integration_cron_job_spec.rb
index 96d8b9ff990..5b3fdf686d9 100644
--- a/modules/storages/spec/workers/storages/manage_nextcloud_integration_cron_job_spec.rb
+++ b/modules/storages/spec/workers/storages/manage_nextcloud_integration_cron_job_spec.rb
@@ -31,12 +31,12 @@
require 'spec_helper'
require_module_spec_helper
-RSpec.describe Storages::ManageNextcloudIntegrationCronJob, type: :job do
+RSpec.describe Storages::ManageNextcloudIntegrationCronJob, :webmock, type: :job do
it 'has a schedule set' do
expect(described_class.cron_expression).to eq('*/5 * * * *')
end
- describe '.sync_all_group_folders' do
+ describe '.perform' do
subject { described_class.new.perform }
context 'when lock is free' do
@@ -44,17 +44,53 @@ RSpec.describe Storages::ManageNextcloudIntegrationCronJob, type: :job do
expect(subject).to be(true)
end
- it 'calls GroupFolderPropertiesSyncService for each appropriate storage' do
+ it 'calls GroupFolderPropertiesSyncService for each automatically managed storage' do
storage1 = create(:nextcloud_storage, :as_automatically_managed)
storage2 = create(:nextcloud_storage, :as_not_automatically_managed)
- allow(Storages::GroupFolderPropertiesSyncService).to receive(:call).and_call_original
+ allow(Storages::GroupFolderPropertiesSyncService)
+ .to receive(:call).with(storage1).and_return(ServiceResult.success)
expect(subject).to be(true)
expect(Storages::GroupFolderPropertiesSyncService).to have_received(:call).with(storage1).once
expect(Storages::GroupFolderPropertiesSyncService).not_to have_received(:call).with(storage2)
end
+
+ it 'marks storage as healthy if sync was successful' do
+ storage1 = create(:nextcloud_storage, :as_automatically_managed)
+
+ allow(Storages::GroupFolderPropertiesSyncService)
+ .to receive(:call).with(storage1).and_return(ServiceResult.success)
+
+ Timecop.freeze('2023-03-14T15:17:00Z') do
+ expect do
+ subject
+ storage1.reload
+ end.to(
+ change(storage1, :health_changed_at).from(nil).to(Time.now.utc)
+ .and(change(storage1, :health_status).from(nil).to('healthy'))
+ )
+ end
+ end
+
+ it 'marks storage as unhealthy if sync was unsuccessful' do
+ storage1 = create(:nextcloud_storage, :as_automatically_managed)
+
+ allow(Storages::GroupFolderPropertiesSyncService)
+ .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
+ subject
+ storage1.reload
+ end.to(
+ change(storage1, :health_changed_at).from(nil).to(Time.now.utc)
+ .and(change(storage1, :health_status).from(nil).to('unhealthy'))
+ .and(change(storage1, :health_reason).from(nil).to('not_found'))
+ )
+ end
+ end
end
context 'when lock is unfree' do
diff --git a/modules/storages/spec/workers/storages/manage_nextcloud_integration_events_job_spec.rb b/modules/storages/spec/workers/storages/manage_nextcloud_integration_events_job_spec.rb
index 56f76df73a6..8f87a9295a2 100644
--- a/modules/storages/spec/workers/storages/manage_nextcloud_integration_events_job_spec.rb
+++ b/modules/storages/spec/workers/storages/manage_nextcloud_integration_events_job_spec.rb
@@ -73,18 +73,20 @@ RSpec.describe Storages::ManageNextcloudIntegrationEventsJob, type: :job do
end
describe '#perform' do
- subject { described_class.new.perform }
+ it 'responds with true when parent perform responds with true' do
+ allow(OpenProject::Mutex).to receive(:with_advisory_lock).and_return(true)
+ allow(described_class).to receive(:debounce)
+
+ expect(described_class.new.perform).to be(true)
- it 'works out silently' do
- allow(Storages::NextcloudStorage).to receive(:sync_all_group_folders).and_return(true)
- subject
+ expect(described_class).not_to have_received(:debounce)
end
- it 'debounces itself when sync has been started by another process' do
- allow(Storages::NextcloudStorage).to receive(:sync_all_group_folders).and_return(false)
+ it 'debounces itself when parent perform responds with false' do
+ allow(OpenProject::Mutex).to receive(:with_advisory_lock).and_return(false)
allow(described_class).to receive(:debounce)
- subject
+ expect(described_class.new.perform).to be(false)
expect(described_class).to have_received(:debounce).once
end