Health Check Endpoint

Create a controller for health check in app/controllers/health_controller.rb:

Controller

health_controller.rb
class HealthController < ApplicationController
  def index 
    database_status = Health.database ? 'OK' : 'FAILED'
    redis_status = Health.redis ? 'OK' : 'FAILED'

    render json: { database: database_status, redis: redis_status }
  end
end

Class

Create a class for health check:

health.rb
class Health
  def self.database
    ActiveRecord::Base.connection.active?
  end

  def self.redis
    Redis.new.ping == "PONG"

  rescue StandardError
    false
  end
end

Route

Define a route in config/routes.rb:

routes.rb
  get '/heartbeat', to: 'health#index'

Visit http://localhost:3000/heartbeat in your browser or send a GET request to that endpoint to see the health check results.