Code Monkey home page Code Monkey logo

Comments (4)

JumpLink avatar JumpLink commented on June 2, 2024

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 ...

from denon.

JumpLink avatar JumpLink commented on June 2, 2024

@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/[email protected]/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.

from denon.

ralyodio avatar ralyodio commented on June 2, 2024

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.

from denon.

Hexagon avatar Hexagon commented on June 2, 2024

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

from denon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.