How to have a working ‘/health’ endpoint while Rails is 500ring

goncalvesjoao
2 min readMay 17, 2019

--

So you wanted to help your DevOp friends and decided to created a sweet endpoint that returns the health status of your Rails App, like so:

But alas, your Rails App decides to throw a fit and returns 500 before that endpoint could return something.

How can you still make that endpoint behave correctly while Rails doesn’t?

How can you have an independent App from Rails but still have access to Rails objects?

Enter the Rack! (more info: https://thoughtbot.com/upcase/videos/rack)

So all we have to do is create a Rack App, map it to your path of choice (in this example /health) in the config.ru file, making sure that a request to that endpoint won’t even reach Rails.

Add the highlighted lines to your config.ru file

Naturally you can change this Rack App to your needs and add extra checks like, testing if you can connect to your API or other external connections you need to do inside Rails, like say, using a python module through PyCall or something.

The cool part about this is that although this Ruby Rack App is independent of Rails, because its being loaded in a config.ru that also loads your Rails App you have access to all your Rails objects 🤘

--

--

goncalvesjoao

Been developing for the web since 2007, went to London in 2016, moved to Tokyo in 2017 and now I’m back to Portugal.