Keeping It Running
Sprout is live. At 3 a.m. the Node process crashes on an input nobody expected, or it keeps running but loses its database connection and fails every request. Nobody is awake. Production needs layers that notice problems and recover on their own, and that wake a person only when they cannot.
The rule
Restart what crashes, test what runs, alert a human when both fail.
Layer 1: restart crashes. On Linux, the service manager systemd starts Sprout's API and watches it. These are the key lines of its unit file, sprout-api.service:
[Service]
ExecStart=/usr/bin/node /srv/sprout/server/src/index.js
EnvironmentFile=/etc/sprout/api.env
User=sprout
Restart=alwaysRestart=always starts the process again whenever it exits. User=sprout runs it as an ordinary user, not root. Everything the app prints goes to the system journal, where journalctl -u sprout-api shows it.
Layer 2: test what runs. A running process is not the same as a working one. A health route checks the things the app really depends on:
app.get('/api/health', async (req, res) => {
try {
await pool.query('SELECT 1');
res.json({ ok: true });
} catch {
res.status(503).json({ ok: false });
}
});Layer 3: watch and alert. Monit is a small Linux tool that checks services on a schedule, restarts them when a check fails, and sends an alert when restarting does not help:
check process sprout-api matching "src/index.js"
start program = "/usr/bin/systemctl start sprout-api"
stop program = "/usr/bin/systemctl stop sprout-api"
if failed port 3000 protocol http request "/api/health" then restart
if 3 restarts within 5 cycles then alertEach cycle (every 30 seconds, say), Monit calls the health route. A failure earns a restart. Three restarts in five cycles means something deeper is wrong, such as the database being down, and Monit emails you. Monit can also watch disk space, memory and CPU, which catches the slow failures, like logs filling a disk, before they become outages.