Skip to content

Commit

Permalink
fortify auto-restart against unexpected exit code types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruithne committed Jul 12, 2023
1 parent 71b6b13 commit 78bdd3f
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,39 @@ async function start_server() {
}
}

Bun.spawn(parse_command_line(config.run), {
const proc = Bun.spawn(parse_command_line(config.run), {
cwd: process.cwd(),
stdout: 'inherit',
stderr: 'pipe',

onExit: (proc, exitCode, signal) => {
log('server exited with code %s', exitCode);

if (exitCode !== null && exitCode > 0) {
if (proc.stderr !== undefined) {
const res = new Response(proc.stderr as ReadableStream);

res.text().then(async stderr => {
await dispatch_report('crash: server exited unexpectedly', [{
exitCode,
stderr: strip_color_codes(stderr).split(/\r?\n/)
}]);
});
} else {
dispatch_report('crash: service exited unexpectedly', [{
exitCode
}]);
}
}
stderr: 'pipe'
});

const auto_restart_ms = config.autoRestart;
if (auto_restart_ms > -1) {
log('restarting server in %dms', auto_restart_ms);
setTimeout(start_server, auto_restart_ms);
}
await proc.exited;

const proc_exit_code = proc.exitCode;
log('server exited with code %s', proc_exit_code);

if (proc_exit_code !== 0) {
if (proc.stderr !== undefined) {
const res = new Response(proc.stderr as ReadableStream);

res.text().then(async stderr => {
await dispatch_report('crash: server exited unexpectedly', [{
proc_exit_code,
stderr: strip_color_codes(stderr).split(/\r?\n/)
}]);
});
} else {
dispatch_report('crash: service exited unexpectedly', [{
proc_exit_code
}]);
}
});
}

const auto_restart_ms = config.autoRestart;
if (auto_restart_ms > -1) {
log('restarting server in %dms', auto_restart_ms);
setTimeout(start_server, auto_restart_ms);
}
}

await start_server();

0 comments on commit 78bdd3f

Please sign in to comment.