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 HealthService for storages

Andreas Pfohlad895b631a48
diff --git a/lib/open_project/events.rb b/lib/open_project/events.rb
index 9320a7621ea..edf7d3eefee 100644
--- a/lib/open_project/events.rb
+++ b/lib/open_project/events.rb
@@ -67,7 +67,7 @@ module OpenProject
PROJECT_STORAGE_DESTROYED = "project_storage_destroyed".freeze
STORAGE_TURNED_UNHEALTHY = "storage_turned_unhealthy".freeze
- STORAGE_TURNED_HEALTHY = "storage_turned_unhealthy".freeze
+ STORAGE_TURNED_HEALTHY = "storage_turned_healthy".freeze
ROLE_UPDATED = "role_updated".freeze
ROLE_DESTROYED = "role_destroyed".freeze
diff --git a/modules/storages/app/mailers/storages/storages_mailer.rb b/modules/storages/app/mailers/storages/storages_mailer.rb
index 01555cb2a48..0e23f5954c6 100644
--- a/modules/storages/app/mailers/storages/storages_mailer.rb
+++ b/modules/storages/app/mailers/storages/storages_mailer.rb
@@ -36,8 +36,9 @@ module Storages
before_action :find_admins, only: %i[notify_unhealthy notify_healthy]
- def notify_unhealthy(storage)
+ def notify_unhealthy(storage, reason)
@storage = storage
+ @reason = reason
subject = "Storage turned unhealthy!"
send_mail_to_admins(storage, subject)
end
@@ -52,7 +53,7 @@ module Storages
def find_admins
@admins = User.where(admin: true)
- .where.not(email: [nil, ''])
+ .where.not(mail: [nil, ''])
end
def send_mail_to_admins(storage, subject)
diff --git a/modules/storages/app/services/storages/health/health_service.rb b/modules/storages/app/services/storages/health/health_service.rb
new file mode 100644
index 00000000000..bb1dd1a5e03
--- /dev/null
+++ b/modules/storages/app/services/storages/health/health_service.rb
@@ -0,0 +1,35 @@
+module Storages
+ module Health
+ class HealthService
+ def initialize(storage:)
+ @storage = storage
+ end
+
+ def healthy
+ was_unhealthy = @storage.health_unhealthy?
+
+ @storage.mark_as_healthy
+
+ ::Storages::StoragesMailer.notify_healthy(@storage).deliver_now if was_unhealthy
+ end
+
+ def unhealthy(reason:)
+ @storage.mark_as_unhealthy(reason:)
+
+ ::Storages::StoragesMailer.notify_unhealthy(@storage, reason).deliver_now
+
+ schedule_mail_job unless mail_job_exists?
+ end
+
+ private
+
+ def schedule_mail_job
+ ::Storages::HealthStatusMailerJob.schedule
+ end
+
+ def mail_job_exists?
+ Delayed::Job.where('handler LIKE ?', "%job_class: Storages::HealthStatusMailerJob%").any?
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/workers/storages/health_status_mailer_job.rb b/modules/storages/app/workers/storages/health_status_mailer_job.rb
index 71474ddbe78..d2c249544d0 100644
--- a/modules/storages/app/workers/storages/health_status_mailer_job.rb
+++ b/modules/storages/app/workers/storages/health_status_mailer_job.rb
@@ -1,23 +1,51 @@
# 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 HealthStatusMailerJob < ApplicationJob
def perform(storage)
- if storage.healthy?
+ if storage.health_healthy?
return
end
Storages::StoragesMailer.notify_unhealthy(storage).deliver_now
- reschedule
+ schedule
end
- private
-
- def reschedule
- next_run_time = Date.now.tomorrow.beginning_of_the_day + 2.hours
+ class << self
+ def schedule
+ next_run_time = Date.tomorrow.beginning_of_day + 2.hours
- self.class.set(wait_until: next_run_time).perform_later
+ HealthStatusMailerJob.set(wait_until: next_run_time).perform_later
+ end
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
index 661930c1e6b..667bd5f5aca 100644
--- a/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb
+++ b/modules/storages/app/workers/storages/manage_nextcloud_integration_job_mixin.rb
@@ -43,10 +43,10 @@ module Storages
result = service_for(storage).call(storage)
result.match(
on_success: ->(_) do
- storage.mark_as_healthy
+ OpenProject::Notifications.send(OpenProject::Events::STORAGE_TURNED_HEALTHY, storage:)
end,
on_failure: ->(errors) do
- storage.mark_as_unhealthy(reason: errors.to_s)
+ OpenProject::Notifications.send(OpenProject::Events::STORAGE_TURNED_UNHEALTHY, storage:, reason: errors.to_s)
end
)
end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index d76be966a89..26a8011caad 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -105,14 +105,14 @@ module OpenProject::Storages
OpenProject::Notifications.subscribe(
::OpenProject::Events::STORAGE_TURNED_UNHEALTHY
- ) do |storage|
- # send mail to admins
+ ) do |payload|
+ Storages::Health::HealthService.new(storage: payload[:storage]).unhealthy(reason: payload[:reason])
end
OpenProject::Notifications.subscribe(
::OpenProject::Events::STORAGE_TURNED_HEALTHY
- ) do |storage|
- # send mail to admins
+ ) do |payload|
+ Storages::Health::HealthService.new(storage: payload[:storage]).healthy
end
end
end