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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Extract health checks into separate files
Oliver Günthere1ce114da0df
diff --git a/config/initializers/health_checks.rb b/config/initializers/health_checks.rbindex dd7d2a42b66..d0a09268c55 100644--- a/config/initializers/health_checks.rb+++ b/config/initializers/health_checks.rb@@ -1,95 +1,37 @@require 'ok_computer/ok_computer_controller'-class GoodJobCheck < OkComputer::Check- def initialize;end+Rails.application.configure do+ config.after_initialize do+ OkComputer::Registry.register "worker", OpenProject::HealthChecks::GoodJobCheck.new+ OkComputer::Registry.register "worker_backed_up", OpenProject::HealthChecks::GoodJobBackedUpCheck.new- def check- count = GoodJob::Process.active.count+ OkComputer::Registry.register "puma", OpenProject::HealthChecks::PumaCheck.new- if count.zero?- mark_failure- mark_message "No good_job processes are active."- else- mark_message "#{count} good_job processes are active."- end- end-end--class PumaCheck < OkComputer::Check- attr_reader :threshold-- def initialize(backlog_threshold)- @threshold = backlog_threshold.to_i- end+ # Make dj backed up optional due to bursts+ OkComputer.make_optional %w(worker_backed_up puma)- def check- stats = self.stats-- return mark_message "N/A as Puma is not used." if stats.nil?-- if stats[:running] > 0- mark_message "Puma is running"- else- mark_failure- mark_message "Puma is not running"+ # Register web worker check for web + database+ OkComputer::CheckCollection.new('web').tap do |collection|+ collection.register :default, OkComputer::Registry.fetch('default')+ collection.register :database, OkComputer::Registry.fetch('database')+ OkComputer::Registry.default_collection.register 'web', collectionend- if stats[:backlog] < threshold- mark_message "Backlog ok"- else- mark_failure- mark_message "Backlog congested"+ # Register full check for web + database + dj worker+ OkComputer::CheckCollection.new('full').tap do |collection|+ collection.register :default, OkComputer::Registry.fetch('default')+ collection.register :database, OkComputer::Registry.fetch('database')+ collection.register :mail, OpenProject::HealthChecks::SmtpCheck.new+ collection.register :worker, OkComputer::Registry.fetch('worker')+ collection.register :worker_backed_up, OkComputer::Registry.fetch('worker_backed_up')+ collection.register :puma, OkComputer::Registry.fetch('puma')+ OkComputer::Registry.default_collection.register 'full', collectionend- end-- def stats- return nil unless applicable?-- server = Puma::Server.current- return nil if server.nil?-- {- backlog: server.backlog || 0,- running: server.running || 0,- pool_capacity: server.pool_capacity || 0,- max_threads: server.max_threads || 0- }- end-- def applicable?- return @applicable unless @applicable.nil?- @applicable = Object.const_defined?("Puma::Server") && !Puma::Server.current.nil?+ # Check if authentication required+ authentication_password = OpenProject::Configuration.health_checks_authentication_password+ if authentication_password.present?+ OkComputer.require_authentication('health_checks', authentication_password)+ endendend--OkComputer::Registry.register "worker", GoodJobCheck.new--backlog_threshold = OpenProject::Configuration.health_checks_backlog_threshold-OkComputer::Registry.register "puma", PumaCheck.new(backlog_threshold)--# Make dj backed up optional due to bursts-OkComputer.make_optional %w(puma)--# Register web worker check for web + database-OkComputer::CheckCollection.new('web').tap do |collection|- collection.register :default, OkComputer::Registry.fetch('default')- collection.register :database, OkComputer::Registry.fetch('database')- OkComputer::Registry.default_collection.register 'web', collection-end--# Register full check for web + database + dj worker-OkComputer::CheckCollection.new('full').tap do |collection|- collection.register :default, OkComputer::Registry.fetch('default')- collection.register :database, OkComputer::Registry.fetch('database')- collection.register :mail, OkComputer::ActionMailerCheck.new- collection.register :worker, OkComputer::Registry.fetch('worker')- collection.register :puma, OkComputer::Registry.fetch('puma')- OkComputer::Registry.default_collection.register 'full', collection-end--# Check if authentication required-authentication_password = OpenProject::Configuration.health_checks_authentication_password-if authentication_password.present?- OkComputer.require_authentication('health_checks', authentication_password)-enddiff --git a/lib/open_project/health_checks/good_job_backed_up_check.rb b/lib/open_project/health_checks/good_job_backed_up_check.rbnew file mode 100644index 00000000000..9c7c131ea82--- /dev/null+++ b/lib/open_project/health_checks/good_job_backed_up_check.rb@@ -0,0 +1,48 @@+#-- 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 docs/COPYRIGHT.rdoc for more details.+#+++module OpenProject+ module HealthChecks+ class GoodJobBackedUpCheck < OkComputer::Check+ def initialize(threshold = OpenProject::Configuration.health_checks_jobs_never_ran_minutes_ago)+ @threshold = threshold.to_i+ super()+ end++ def check+ count = GoodJob::Job.queued.where('scheduled_at < ?', @threshold.minutes.ago).count++ if count > 0+ mark_message "#{count} jobs are waiting to be picked up for more than #{@threshold} minutes."+ mark_failure+ else+ mark_message "No jobs are waiting to be picked up."+ end+ end+ end+ end+enddiff --git a/lib/open_project/health_checks/good_job_check.rb b/lib/open_project/health_checks/good_job_check.rbnew file mode 100644index 00000000000..9a996572bd9--- /dev/null+++ b/lib/open_project/health_checks/good_job_check.rb@@ -0,0 +1,43 @@+#-- 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 docs/COPYRIGHT.rdoc for more details.+#+++module OpenProject+ module HealthChecks+ class GoodJobCheck < OkComputer::Check+ def check+ count = GoodJob::Process.active.count++ if count.zero?+ mark_failure+ mark_message "No good_job processes are active."+ else+ mark_message "#{count} good_job processes are active."+ end+ end+ end+ end+enddiff --git a/lib/open_project/health_checks/puma_check.rb b/lib/open_project/health_checks/puma_check.rbnew file mode 100644index 00000000000..583a4a00817--- /dev/null+++ b/lib/open_project/health_checks/puma_check.rb@@ -0,0 +1,78 @@+#-- 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 docs/COPYRIGHT.rdoc for more details.+#+++module OpenProject+ module HealthChecks+ class PumaCheck < OkComputer::Check+ attr_reader :threshold++ def initialize(threshold = OpenProject::Configuration.health_checks_backlog_threshold)+ @threshold = threshold.to_i+ @applicable = Object.const_defined?("Puma::Server") && !Puma::Server.current.nil?+ super()+ end++ def check+ stats = self.stats++ return mark_message "N/A as Puma is not used." if stats.nil?++ if stats[:running] > 0+ mark_message "Puma is running"+ else+ mark_failure+ mark_message "Puma is not running"+ end++ if stats[:backlog] < threshold+ mark_message "Backlog ok"+ else+ mark_failure+ mark_message "Backlog congested"+ end+ end++ def stats+ return nil unless applicable?++ server = Puma::Server.current+ return nil if server.nil?++ {+ backlog: server.backlog || 0,+ running: server.running || 0,+ pool_capacity: server.pool_capacity || 0,+ max_threads: server.max_threads || 0+ }+ end++ def applicable?+ !!@applicable+ end+ end+ end+enddiff --git a/lib/open_project/health_checks/smtp_check.rb b/lib/open_project/health_checks/smtp_check.rbnew file mode 100644index 00000000000..81b09ce4d1f--- /dev/null+++ b/lib/open_project/health_checks/smtp_check.rb@@ -0,0 +1,45 @@+#-- 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 docs/COPYRIGHT.rdoc for more details.+#+++module OpenProject+ module HealthChecks+ class SmtpCheck < OkComputer::ActionMailerCheck+ def check+ unless Setting.email_delivery_method == :smtp+ mark_message "Mail delivery method is not SMTP"+ return+ end++ # settings might change between calls+ @host = Setting.smtp_address+ @port = Setting.smtp_port++ super+ end+ end+ end+end