#Restart Deno process on crash

3 messages · Page 1 of 1 (latest)

light sand
#

Hello,
The following Node snippet allows a script to restart on crash :

import {
    spawn
} from 'node:child_process';
process.on(
    'uncaughtException',
    () => {
        process.once(
            'exit',
            () => spawn(
                process.argv.shift(),
                process.argv,
                {
                    cwd: process.cwd(),
                    detached: true,
                    stdio: 'inherit'
                }
            )
        );
        process.exit();
    }
);
```What's its Deno equivalent ?
Thanks
leaden eagle
#

For Deno, try pup! (And for node, you should really use something like PM2 instead of that hack fast_deno )

Short way of keeping a process alive

pup run -AC "deno run script.ts"

If you do not want to install pup

deno run -A https://deno.land/x/pup@1.0.0-beta.5/pup.ts run -AC "deno run script.ts"

Explanation: -A is --autostart and -C is --cmd

Full manual at https://hexagon.github.io/pup/

IF you really dont want to use a separate command, but rather relaunch your application internally, you can use Pup programmatically.

import { Configuration, Pup } from "https://deno.land/x/pup/mod.ts"

const configuration : Configuration = {
  "processes": [
    {
        "id": "server-task",
        "cmd": ["deno", "run","server.ts"],
        "autostart": true
    }
  ],
}

new Pup(configuration).init()

... and run this script with --allow-run

Docs for this at https://hexagon.github.io/pup/library.html

light sand
#

Thanks but I want the script to be able to restart itself, not depend on another script to do so.