From 7b34e1e87db0773341b6bfee218d0cf79fcadd63 Mon Sep 17 00:00:00 2001 From: Francis Kayiwa Date: Wed, 13 Nov 2024 18:24:33 -0500 Subject: [PATCH] loadbalancer cap tasks (#1206) * add the health monitor gem we use this to monitor the health of an application * add the configuration for health-monitor we initialize health-monitor and configure it * Update config/deploy.rb Co-authored-by: Max Kadel * Update config/initializers/health_monitor.rb Co-authored-by: Max Kadel --------- Co-authored-by: Max Kadel --- Gemfile | 3 +++ Gemfile.lock | 3 +++ config/deploy.rb | 32 ++++++++++++++++++++++++++- config/initializers/health_monitor.rb | 17 ++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 config/initializers/health_monitor.rb diff --git a/Gemfile b/Gemfile index a16a5215..a4e2838e 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,9 @@ gem "kaminari" # static pages gem "high_voltage" +# health monitor +gem "health-monitor-rails", "12.4.0" + gem "honeybadger" group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index e44ef264..80e0ded9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -152,6 +152,8 @@ GEM globalid (1.2.1) activesupport (>= 6.1) hashie (5.0.0) + health-monitor-rails (12.4.0) + railties (>= 6.1) high_voltage (4.0.0) honeybadger (5.15.5) i18n (1.14.6) @@ -420,6 +422,7 @@ DEPENDENCIES ed25519 factory_bot_rails faker + health-monitor-rails (= 12.4.0) high_voltage honeybadger jbuilder diff --git a/config/deploy.rb b/config/deploy.rb index 2fb664a0..09119bce 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -157,6 +157,36 @@ after :finishing, 'deploy:cleanup' + # # You can/ should apply this command to a subset of hosts +# cap --hosts=lib-approvals-staging2.lib.princeton.edu staging application:remove_from_nginx +desc "Marks the server(s) to be removed from the loadbalancer" + task :remove_from_nginx do + count = 0 + on roles(:app) do + count += 1 + end + if count > (roles(:app).length / 2) + raise "You must run this command on individual servers utilizing the --hosts= switch" + end + on roles(:app) do + within release_path do + execute :touch, "public/remove-from-nginx" + end + end + end + # You can/ should apply this command to a subset of hosts + # cap --hosts=lib-approvals-staging2.lib.princeton.edu staging application:serve_from_nginx + desc "Marks the server(s) to be added back to the loadbalancer" + task :serve_from_nginx do + on roles(:app) do + within release_path do + execute :rm, "-f public/remove-from-nginx" + end + end + end +end + +# before "deploy:reverted", "deploy:assets:precompile" # # We shouldn't need this because it should be built in to Rails 5.1 # # see https://github.com/rails/webpacker/issues/1037 # desc 'Run yarn install' @@ -168,4 +198,4 @@ # end # end # before "deploy:assets:precompile", "deploy:yarn_install" -end +# end diff --git a/config/initializers/health_monitor.rb b/config/initializers/health_monitor.rb new file mode 100644 index 00000000..cca5a9ed --- /dev/null +++ b/config/initializers/health_monitor.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true +Rails.application.config.after_initialize do + HealthMonitor.configure do |config| + config.cache + + config.path = :health + + config.file_absence.configure do |file_config| + file_config.filename = "public/remove-from-nginx" + end + + config.error_callback = proc do |e| + Rails.logger.error "Health check failed with: #{e.message}" + Honeybadger.notify(e) unless e.is_a(HealthMonitor::Providers::FileAbsenceException) + end + end +end