Check whether or not there's a connection to a db; when not, notify a user

My Phoenix application normally shows a user a list of products retrieved from a database.

But whenever there happen to be a problem with a db connection, I want my application to continue to work as usual, but a user should be notified “no connection to a db” on the main page, and all other pages as well.

How would I do it?

What you are looking for is called fuse. There is library named exactly that in Hex.

2 Likes

I’m not looking for a library.

That sounds like optimizing for a pretty rare scenario. What kind of app are you building - SPA, SSR, Live View?

phoenix_ecto ships with a plug doing that:

You’ll likely need to copy and adjust though to match your requirements. This plug afaik is only meant for dev convenience.

3 Likes

“Checks if the storage is up (database is created) or if there are any pending migrations.
Both checks can raise an error if the conditions are not met.”

which isn’t what I want.

Adjust for my requirements - yes, but how?

Run a “healthcheck” query. Something as simple as SELECT 1; would work for PostgreSQL.

Those checks require a connection to the db. You asked for a check for a connection to the db. By my understanding this is what you asked for. If not you need to get a bit more concrete.

2 Likes