api: signaling process via ws

This commit is contained in:
Thomas Hobson 2021-07-17 00:22:55 +12:00
parent f58927d79a
commit 3436648add
No known key found for this signature in database
GPG Key ID: 9F1FD9D87950DB6F
2 changed files with 44 additions and 20 deletions

View File

@ -9,6 +9,8 @@ const { Job } = require('../job');
const package = require('../package'); const package = require('../package');
const logger = require('logplease').create('api/v2'); const logger = require('logplease').create('api/v2');
const SIGNALS = ["SIGABRT","SIGALRM","SIGBUS","SIGCHLD","SIGCLD","SIGCONT","SIGEMT","SIGFPE","SIGHUP","SIGILL","SIGINFO","SIGINT","SIGIO","SIGIOT","SIGKILL","SIGLOST","SIGPIPE","SIGPOLL","SIGPROF","SIGPWR","SIGQUIT","SIGSEGV","SIGSTKFLT","SIGSTOP","SIGTSTP","SIGSYS","SIGTERM","SIGTRAP","SIGTTIN","SIGTTOU","SIGUNUSED","SIGURG","SIGUSR1","SIGUSR2","SIGVTALRM","SIGXCPU","SIGXFSZ","SIGWINCH"]
// ref: https://man7.org/linux/man-pages/man7/signal.7.html
function get_job(body){ function get_job(body){
const { const {
@ -148,9 +150,10 @@ router.ws('/connect', async (ws, req) => {
try{ try{
const msg = JSON.parse(data); const msg = JSON.parse(data);
if(msg.type === "init"){ switch(msg.type){
case "init":
if(job === null){ if(job === null){
const job = await get_job(msg); job = await get_job(msg);
await job.prime(); await job.prime();
@ -167,8 +170,8 @@ router.ws('/connect', async (ws, req) => {
}else{ }else{
ws.close(4000, "Already Initialized"); ws.close(4000, "Already Initialized");
} }
break;
}else if(msg.type === "data"){ case "data":
if(job !== null){ if(job !== null){
if(msg.stream === "stdin"){ if(msg.stream === "stdin"){
eventBus.emit("stdin", msg.data) eventBus.emit("stdin", msg.data)
@ -178,7 +181,20 @@ router.ws('/connect', async (ws, req) => {
}else{ }else{
ws.close(4003, "Not yet initialized") ws.close(4003, "Not yet initialized")
} }
break;
case "signal":
if(job !== null){
if(SIGNALS.includes(msg.signal)){
eventBus.emit("signal", msg.signal)
}else{
ws.close(4005, "Invalid signal")
} }
}else{
ws.close(4003, "Not yet initialized")
}
break;
}
}catch(error){ }catch(error){
ws.send(JSON.stringify({type: "error", message: error.message})) ws.send(JSON.stringify({type: "error", message: error.message}))
ws.close(4002, "Notified Error") ws.close(4002, "Notified Error")

View File

@ -110,28 +110,36 @@ class Job {
eventBus.on("stdin", (data) => { eventBus.on("stdin", (data) => {
proc.stdin.write(data); proc.stdin.write(data);
}) })
eventBus.on("kill", (signal) => {
proc.kill(signal)
})
} }
const kill_timeout = set_timeout( const kill_timeout = set_timeout(
_ => proc.kill('SIGKILL'), _ => proc.kill('SIGKILL'),
timeout timeout
); );
proc.stderr.on('data', data => { proc.stderr.on('data', data => {
if (stderr.length > config.output_max_size) { if(eventBus !== null) {
eventBus.emit("stderr", data);
} else if (stderr.length > config.output_max_size) {
proc.kill('SIGKILL'); proc.kill('SIGKILL');
} else { } else {
if(eventBus !== null) eventBus.emit("stderr", data);
stderr += data; stderr += data;
output += data; output += data;
} }
}); });
proc.stdout.on('data', data => { proc.stdout.on('data', data => {
if (stdout.length > config.output_max_size) { if(eventBus !== null){
eventBus.emit("stdout", data);
} else if (stdout.length > config.output_max_size) {
proc.kill('SIGKILL'); proc.kill('SIGKILL');
} else { } else {
if(eventBus !== null) eventBus.emit("stdout", data);
stdout += data; stdout += data;
output += data; output += data;
} }