'How to automatically restart Heroku dynos when application is failing?

Platforms such as Kubernetes have support for liveness and readiness probes: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes

Basically, the (web) application needs to provide a specific http endpoint that is called every few seconds by the platform. If the application is unhealthy (typically indicated by a 5xx error) the platform restarts the application.

How does Heroku handle this?



Solution 1:[1]

You can read about heroku's auto-restart policy here: Does heroku restart NodeJS server if application crashes

If you want to restart by force as soon as it crashes, you can do so via the dashboard - app management (https://dashboard.heroku.com/apps/[APP_NAME]) then on the top right: More> Restart all dynos: enter image description here

You can also do this using heroku cli: https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-dyno-restart-dyno


In addition, I built a small Node.js app (can run on a separate free heroku account ... ?) that monitors the app every 50 seconds, and as soon as status 200 does not return, restarts all dynos via heroku's api platform ... Maybe it will inspire you or suit you, maybe with small changes. https://github.com/ShlomoCode/Heroku-dynos-restarter

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shlomo