Documentation, signal names, reported time in ms

This commit is contained in:
Omar Brikaa 2024-08-31 21:45:43 +03:00
parent 2e00325163
commit 09ff4ca79e
6 changed files with 115 additions and 90 deletions

View file

@ -6,50 +6,9 @@ const events = require('events');
const runtime = require('../runtime');
const { Job } = require('../job');
const package = require('../package');
const globals = require('../globals');
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) {
let {
language,
@ -250,7 +209,9 @@ router.ws('/connect', async (ws, req) => {
break;
case 'signal':
if (job !== null) {
if (SIGNALS.includes(msg.signal)) {
if (
Object.values(globals.SIGNALS).includes(msg.signal)
) {
event_bus.emit('signal', msg.signal);
} else {
ws.close(4005, 'Invalid signal');

View file

@ -7,6 +7,70 @@ const platform = `${is_docker() ? 'docker' : 'baremetal'}-${fs
.split('\n')
.find(x => x.startsWith('ID'))
.replace('ID=', '')}`;
const SIGNALS = {
1: 'SIGHUP',
2: 'SIGINT',
3: 'SIGQUIT',
4: 'SIGILL',
5: 'SIGTRAP',
6: 'SIGABRT',
7: 'SIGBUS',
8: 'SIGFPE',
9: 'SIGKILL',
10: 'SIGUSR1',
11: 'SIGSEGV',
12: 'SIGUSR2',
13: 'SIGPIPE',
14: 'SIGALRM',
15: 'SIGTERM',
16: 'SIGSTKFLT',
17: 'SIGCHLD',
18: 'SIGCONT',
19: 'SIGSTOP',
20: 'SIGTSTP',
21: 'SIGTTIN',
22: 'SIGTTOU',
23: 'SIGURG',
24: 'SIGXCPU',
25: 'SIGXFSZ',
26: 'SIGVTALRM',
27: 'SIGPROF',
28: 'SIGWINCH',
29: 'SIGIO',
30: 'SIGPWR',
31: 'SIGSYS',
34: 'SIGRTMIN',
35: 'SIGRTMIN+1',
36: 'SIGRTMIN+2',
37: 'SIGRTMIN+3',
38: 'SIGRTMIN+4',
39: 'SIGRTMIN+5',
40: 'SIGRTMIN+6',
41: 'SIGRTMIN+7',
42: 'SIGRTMIN+8',
43: 'SIGRTMIN+9',
44: 'SIGRTMIN+10',
45: 'SIGRTMIN+11',
46: 'SIGRTMIN+12',
47: 'SIGRTMIN+13',
48: 'SIGRTMIN+14',
49: 'SIGRTMIN+15',
50: 'SIGRTMAX-14',
51: 'SIGRTMAX-13',
52: 'SIGRTMAX-12',
53: 'SIGRTMAX-11',
54: 'SIGRTMAX-10',
55: 'SIGRTMAX-9',
56: 'SIGRTMAX-8',
57: 'SIGRTMAX-7',
58: 'SIGRTMAX-6',
59: 'SIGRTMAX-5',
60: 'SIGRTMAX-4',
61: 'SIGRTMAX-3',
62: 'SIGRTMAX-2',
63: 'SIGRTMAX-1',
64: 'SIGRTMAX',
};
module.exports = {
data_directories: {
@ -16,4 +80,5 @@ module.exports = {
platform,
pkg_installed_file: '.ppman-installed', //Used as indication for if a package was installed
clean_directories: ['/dev/shm', '/run/lock', '/tmp', '/var/tmp'],
SIGNALS,
};

View file

@ -4,6 +4,7 @@ const cp = require('child_process');
const path = require('path');
const config = require('./config');
const fs = require('fs/promises');
const globals = require('./globals');
const job_states = {
READY: Symbol('Ready to be primed'),
@ -245,18 +246,15 @@ class Job {
});
const data = await new Promise((res, rej) => {
proc.on('close', () => {
proc.on('exit', (_, signal) => {
res({
stdout,
stderr,
signal,
});
});
proc.on('error', err => {
rej({
error: err,
stdout,
stderr,
});
});
});
@ -277,31 +275,13 @@ class Job {
}
switch (key) {
case 'cg-mem':
try {
memory = parse_int(value);
} catch (e) {
throw new Error(
`Failed to parse memory usage, received value: ${value}`
);
}
memory = parse_int(value);
break;
case 'exitcode':
try {
code = parse_int(value);
} catch (e) {
throw new Error(
`Failed to parse exit code, received value: ${value}`
);
}
code = parse_int(value);
break;
case 'exitsig':
try {
signal = parse_int(value);
} catch (e) {
throw new Error(
`Failed to parse exit signal, received value: ${value}`
);
}
signal = globals.SIGNALS[parse_int(value)] ?? null;
break;
case 'message':
message = message || value;
@ -310,22 +290,10 @@ class Job {
status = value;
break;
case 'time':
try {
cpu_time_stat = parse_float(value);
} catch (e) {
throw new Error(
`Failed to parse cpu time, received value: ${value}`
);
}
cpu_time_stat = parse_float(value) * 1000;
break;
case 'time-wall':
try {
wall_time_stat = parse_float(value);
} catch (e) {
throw new Error(
`Failed to parse wall time, received value: ${value}`
);
}
wall_time_stat = parse_float(value) * 1000;
break;
default:
break;
@ -339,6 +307,8 @@ class Job {
return {
...data,
stdout,
stderr,
code,
signal,
output,