-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deno exits child process when parent process exits denoland/deno#5501. Use runtime.connectNative().
- Loading branch information
1 parent
47303e7
commit fac9f9b
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env -S ./deno run --allow-run --unstable --v8-flags="--expose-gc,--jitless" | ||
async function getMessage() { | ||
const header = new Uint32Array(1); | ||
await Deno.stdin.read(header); | ||
const output = new Uint8Array(header[0]); | ||
await Deno.stdin.read(output); | ||
return output; | ||
} | ||
|
||
async function sendMessage(_) { | ||
let message = ''; | ||
let process = Deno.run({ | ||
cmd: ['pgrep', '-f', './deno_server.js'], | ||
stdout: 'piped', | ||
stderr: 'piped', | ||
}); | ||
let rawOutput = await process.output(); | ||
process.close(); | ||
if (rawOutput.length) { | ||
process = Deno.run({ | ||
cmd: ['pkill', '-f', './deno_server.js'], | ||
stdout: 'null', | ||
stderr: 'null', | ||
}); | ||
process.close(); | ||
message = '"Local server off."'; | ||
} else { | ||
process = Deno.run({ | ||
cmd: ['./deno_server.js'], | ||
stdout: 'null', | ||
stderr: 'null', | ||
}); | ||
message = '"Local server on."'; | ||
} | ||
message = new TextEncoder().encode(message); | ||
const header = Uint32Array.from( | ||
{ | ||
length: 4, | ||
}, | ||
(_, index) => (message.length >> (index * 8)) & 0xff | ||
); | ||
const output = new Uint8Array(header.length + message.length); | ||
output.set(header, 0); | ||
output.set(message, 4); | ||
await Deno.stdout.write(output.buffer); | ||
} | ||
|
||
async function main() { | ||
while (true) { | ||
const message = await getMessage(); | ||
await sendMessage(message); | ||
gc(); | ||
} | ||
} | ||
|
||
try { | ||
main(); | ||
} catch (e) { | ||
Deno.exit(); | ||
} |