Skip to content

Commit

Permalink
Get health monitor working (#1208)
Browse files Browse the repository at this point in the history
- Because this app is on Rails 7.2 we can use a newer version of the health check gem
- Add HealthMonitor engine to routes.rb
- Remove Honeybadger alert
  • Loading branch information
maxkadel authored Nov 14, 2024
1 parent 7b34e1e commit e177035
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ gem "kaminari"
gem "high_voltage"

# health monitor
gem "health-monitor-rails", "12.4.0"
gem "health-monitor-rails", "~> 12.4"

gem "honeybadger"

Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ GEM
globalid (1.2.1)
activesupport (>= 6.1)
hashie (5.0.0)
health-monitor-rails (12.4.0)
health-monitor-rails (12.4.1)
railties (>= 6.1)
high_voltage (4.0.0)
honeybadger (5.15.5)
Expand Down Expand Up @@ -422,7 +422,7 @@ DEPENDENCIES
ed25519
factory_bot_rails
faker
health-monitor-rails (= 12.4.0)
health-monitor-rails (~> 12.4)
high_voltage
honeybadger
jbuilder
Expand Down
12 changes: 7 additions & 5 deletions config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,26 @@

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"
namespace :application do
# 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"
raise "You must run this command on no more than half the 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

# 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
Expand Down
1 change: 0 additions & 1 deletion config/initializers/health_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Rails.application.routes.draw do
root "welcome#index"
get "welcome/index"
mount HealthMonitor::Engine, at: "/"

devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }

Expand Down
22 changes: 22 additions & 0 deletions spec/requests/health_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe "Health Check", type: :request do
describe "GET /health" do
it "has a health check" do
get "/health.json"
expect(response).to be_successful
end

it "errors when there's a failure to a critical service" do
allow_any_instance_of(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter).to receive(:execute) do |instance|
raise StandardError if database.blank? || instance.pool.db_config.name == database.to_s
end

get "/health.json"

expect(response).not_to be_successful
expect(response).to have_http_status :service_unavailable
end
end
end

0 comments on commit e177035

Please sign in to comment.