Skip to content

Instantly share code, notes, and snippets.

@goncalvesjoao
Last active May 20, 2019 09:40
Show Gist options
  • Save goncalvesjoao/4a92300eab6804f147e64dac05b5b6fb to your computer and use it in GitHub Desktop.
Save goncalvesjoao/4a92300eab6804f147e64dac05b5b6fb to your computer and use it in GitHub Desktop.
Rack App to return your Rails app health status
# Your Ruby Rack App that returns your Rails App's Health
# Create the file
# "lib/rack/health_check.rb"
module Rack
class HealthCheck
def call(env)
status = {
redis: {
connected: redis_connected
},
postgres: {
connected: postgres_connected,
migrations_updated: postgres_migrations_updated
}
}
return [200, {}, [status.to_json]]
end
protected
def redis_connected
$redis.ping == 'PONG' rescue false
end
def postgres_connected
begin
ApplicationRecord.establish_connection
ApplicationRecord.connection
ApplicationRecord.connected?
rescue
false
end
end
def postgres_migrations_updated
return false unless postgres_connected
!ApplicationRecord.connection.migration_context.needs_migration?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment