KaKi87
KaKi8715mo ago

Restart Deno process on crash

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();
}
);
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
3 Replies
Hexagon
Hexagon15mo ago
For Deno, try pup! (And for node, you should really use something like PM2 instead of that hack <:fast_deno:1035517481189642271> ) 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()
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
KaKi87
KaKi8715mo ago
Thanks but I want the script to be able to restart itself, not depend on another script to do so.
MrKleeblatt
MrKleeblatt15mo ago
You could just wrap
let exit = false;
while(!exit){
try {
// do your thing
exit = true;
} catch(_e){}
}
let exit = false;
while(!exit){
try {
// do your thing
exit = true;
} catch(_e){}
}
around your main function