Skip to content

Commit

Permalink
loadbalancer cap tasks (#1206)
Browse files Browse the repository at this point in the history
* 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 <mkadel@princeton.edu>

* Update config/initializers/health_monitor.rb

Co-authored-by: Max Kadel <mkadel@princeton.edu>

---------

Co-authored-by: Max Kadel <mkadel@princeton.edu>
  • Loading branch information
kayiwa and maxkadel authored Nov 13, 2024
1 parent ee569a5 commit 7b34e1e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -420,6 +422,7 @@ DEPENDENCIES
ed25519
factory_bot_rails
faker
health-monitor-rails (= 12.4.0)
high_voltage
honeybadger
jbuilder
Expand Down
32 changes: 31 additions & 1 deletion config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -168,4 +198,4 @@
# end
# end
# before "deploy:assets:precompile", "deploy:yarn_install"
end
# end
17 changes: 17 additions & 0 deletions config/initializers/health_monitor.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7b34e1e

Please sign in to comment.