denosaurs/denon

Is it possible to recover from an app crash?

ralyodio opened this issue · 4 comments

When my app crashes denon doesnj't restart it. it says "waiting for code changes"

iirc nodemon would handle a crash and restart the app

I would be interested in that too, that's the main reason why I tried denon. As I was hoping the app would also automatically restart in watch mode when it crashes, as this doesn't work with the normal --watch parameter either.

The app stops with this message:

app crashed - waiting for file changes before starting ...

@ralyodio Until there is a clean solution here is a workaround, I have write a simple script that checks if my deno webserver is still running, if not it restarts the app using pm2.

Maybe this is not a solution for you, because pm2 uses node.js instead of deno and you need a webserver in your app.

health-checker.ts:

const BACKEND_LOCAL_URL = "http://localhost:3000";
let interval: number | null = null;

const restart = () => {
    console.log("Restart app..");
    Deno.run({ cmd: ["pm2",  "restart", "your-pm2-instance-name"] });
    if (interval) clearInterval(interval);
    startWatch();
}

const startWatchInterval = (watchInterval: number) => {
    interval = setInterval(async () => {
        try {
            const state = await fetch(BACKEND_LOCAL_URL);
            if (!state.ok) {
                restart();
                
            } else {
                console.log(`ok, next check in ${watchInterval}ms..`);
            }
        } catch (error) {
            console.error(error);
            restart();
        }
        
    }, watchInterval);
}

const startWatch = (startDelay=15000, watchInterval=5000) => {
    console.log(`Start watcher in ${startDelay}ms..`);
    setTimeout(() => {
        startWatchInterval(watchInterval);
    }, startDelay)
}

startWatch();

Maybe you can replace the Deno.run command with something else.

pm2.config.js:

module.exports = {
  apps: [
    {
      name: "your-pm2-instance-name",
      script: "denon watch",
    },
  ],
};

Start this with:

pm2 start ./pm2.config.js

scripts.json:

{
  "$schema": "https://deno.land/x/denon@2.4.9/schema.json",
  "scripts": {
    "start": {
      "cmd": "deno run app.ts",
      "desc": "Start the webserver",
      "watch": false
    },
    "watch": {
      "cmd": "deno run app.ts",
      "desc": "Watch the webserver",
      "watch": true
    },
    "health-checker": {
      "cmd": "deno run health-checker.ts",
      "desc": "Run the health checker to restart the webserver on a crash",
      "allow": ["net", "run"],
      "watch": true
    }
  }
}

Start the health-checker with:

denon health-checker

pm2 will start the web server with denon watch for us and the health-checker restarts the web server using pm2.

I stopped using denon. systemd will recover itself when deno crashes so no need for denon in prod.

its handy for local dev but this package seems to be rather broken now for awhile.

If anyone looks for an up to date answer, have a look at http://github.com/hexagon/pup