mirror of
https://github.com/engineer-man/piston.git
synced 2025-04-21 04:26:28 +02:00
Piston lint
This commit is contained in:
parent
d61fb8ec5b
commit
f2c91acbe6
57 changed files with 1121 additions and 893 deletions
3
api/.gitignore
vendored
3
api/.gitignore
vendored
|
@ -1,2 +1 @@
|
|||
node_modules
|
||||
_piston
|
||||
_piston
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
node_modules
|
|
@ -1,3 +0,0 @@
|
|||
singleQuote: true
|
||||
tabWidth: 4
|
||||
arrowParens: avoid
|
28
api/package-lock.json
generated
28
api/package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "piston-api",
|
||||
"version": "3.0.0",
|
||||
"version": "3.1.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "piston-api",
|
||||
"version": "3.0.0",
|
||||
"version": "3.1.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.19.0",
|
||||
|
@ -20,9 +20,6 @@
|
|||
"semver": "^7.3.4",
|
||||
"uuid": "^8.3.2",
|
||||
"waitpid": "git+https://github.com/HexF/node-waitpid.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "2.2.1"
|
||||
}
|
||||
},
|
||||
"node_modules/accepts": {
|
||||
|
@ -409,18 +406,6 @@
|
|||
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
|
||||
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
|
||||
},
|
||||
"node_modules/prettier": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.2.1.tgz",
|
||||
"integrity": "sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"prettier": "bin-prettier.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.13.0"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-addr": {
|
||||
"version": "2.0.6",
|
||||
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz",
|
||||
|
@ -595,7 +580,8 @@
|
|||
}
|
||||
},
|
||||
"node_modules/waitpid": {
|
||||
"resolved": "git+ssh://git@github.com/HexF/node-waitpid.git#a08d116a5d993a747624fe72ff890167be8c34aa"
|
||||
"resolved": "git+ssh://git@github.com/HexF/node-waitpid.git#a08d116a5d993a747624fe72ff890167be8c34aa",
|
||||
"hasInstallScript": true
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "7.5.3",
|
||||
|
@ -913,12 +899,6 @@
|
|||
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
|
||||
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
|
||||
},
|
||||
"prettier": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.2.1.tgz",
|
||||
"integrity": "sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==",
|
||||
"dev": true
|
||||
},
|
||||
"proxy-addr": {
|
||||
"version": "2.0.6",
|
||||
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz",
|
||||
|
|
|
@ -16,11 +16,5 @@
|
|||
"uuid": "^8.3.2",
|
||||
"waitpid": "git+https://github.com/HexF/node-waitpid.git"
|
||||
},
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"lint": "prettier . --write"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "2.2.1"
|
||||
}
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -8,10 +8,49 @@ const { Job } = require('../job');
|
|||
const package = require('../package');
|
||||
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"]
|
||||
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) {
|
||||
let {
|
||||
language,
|
||||
version,
|
||||
|
@ -21,7 +60,7 @@ function get_job(body){
|
|||
compile_memory_limit,
|
||||
run_memory_limit,
|
||||
run_timeout,
|
||||
compile_timeout
|
||||
compile_timeout,
|
||||
} = body;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
@ -68,7 +107,7 @@ function get_job(body){
|
|||
}
|
||||
if (typeof constraint_value !== 'number') {
|
||||
return reject({
|
||||
message: `If specified, ${constraint_name} must be a number`
|
||||
message: `If specified, ${constraint_name} must be a number`,
|
||||
});
|
||||
}
|
||||
if (configured_limit <= 0) {
|
||||
|
@ -76,12 +115,12 @@ function get_job(body){
|
|||
}
|
||||
if (constraint_value > configured_limit) {
|
||||
return reject({
|
||||
message: `${constraint_name} cannot exceed the configured limit of ${configured_limit}`
|
||||
message: `${constraint_name} cannot exceed the configured limit of ${configured_limit}`,
|
||||
});
|
||||
}
|
||||
if (constraint_value < 0) {
|
||||
return reject({
|
||||
message: `${constraint_name} must be non-negative`
|
||||
message: `${constraint_name} must be non-negative`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -91,20 +130,22 @@ function get_job(body){
|
|||
run_timeout = run_timeout || rt.timeouts.run;
|
||||
compile_memory_limit = compile_memory_limit || rt.memory_limits.compile;
|
||||
run_timeout = run_timeout || rt.timeouts.run;
|
||||
resolve(new Job({
|
||||
runtime: rt,
|
||||
args: args || [],
|
||||
stdin: stdin || "",
|
||||
files,
|
||||
timeouts: {
|
||||
run: run_timeout,
|
||||
compile: compile_timeout,
|
||||
},
|
||||
memory_limits: {
|
||||
run: run_memory_limit,
|
||||
compile: compile_memory_limit,
|
||||
}
|
||||
}));
|
||||
resolve(
|
||||
new Job({
|
||||
runtime: rt,
|
||||
args: args || [],
|
||||
stdin: stdin || '',
|
||||
files,
|
||||
timeouts: {
|
||||
run: run_timeout,
|
||||
compile: compile_timeout,
|
||||
},
|
||||
memory_limits: {
|
||||
run: run_memory_limit,
|
||||
compile: compile_memory_limit,
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -123,88 +164,104 @@ router.use((req, res, next) => {
|
|||
});
|
||||
|
||||
router.ws('/connect', async (ws, req) => {
|
||||
|
||||
let job = null;
|
||||
let eventBus = new events.EventEmitter();
|
||||
|
||||
eventBus.on("stdout", (data) => ws.send(JSON.stringify({type: "data", stream: "stdout", data: data.toString()})))
|
||||
eventBus.on("stderr", (data) => ws.send(JSON.stringify({type: "data", stream: "stderr", data: data.toString()})))
|
||||
eventBus.on("stage", (stage)=> ws.send(JSON.stringify({type: "stage", stage})))
|
||||
eventBus.on("exit", (stage, status) => ws.send(JSON.stringify({type: "exit", stage, ...status})))
|
||||
eventBus.on('stdout', data =>
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'data',
|
||||
stream: 'stdout',
|
||||
data: data.toString(),
|
||||
})
|
||||
)
|
||||
);
|
||||
eventBus.on('stderr', data =>
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'data',
|
||||
stream: 'stderr',
|
||||
data: data.toString(),
|
||||
})
|
||||
)
|
||||
);
|
||||
eventBus.on('stage', stage =>
|
||||
ws.send(JSON.stringify({ type: 'stage', stage }))
|
||||
);
|
||||
eventBus.on('exit', (stage, status) =>
|
||||
ws.send(JSON.stringify({ type: 'exit', stage, ...status }))
|
||||
);
|
||||
|
||||
ws.on("message", async (data) => {
|
||||
|
||||
try{
|
||||
ws.on('message', async data => {
|
||||
try {
|
||||
const msg = JSON.parse(data);
|
||||
|
||||
switch(msg.type){
|
||||
case "init":
|
||||
if(job === null){
|
||||
switch (msg.type) {
|
||||
case 'init':
|
||||
if (job === null) {
|
||||
job = await get_job(msg);
|
||||
|
||||
await job.prime();
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
type: "runtime",
|
||||
language: job.runtime.language,
|
||||
version: job.runtime.version.raw
|
||||
}))
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'runtime',
|
||||
language: job.runtime.language,
|
||||
version: job.runtime.version.raw,
|
||||
})
|
||||
);
|
||||
|
||||
await job.execute_interactive(eventBus);
|
||||
|
||||
ws.close(4999, "Job Completed");
|
||||
|
||||
}else{
|
||||
ws.close(4000, "Already Initialized");
|
||||
ws.close(4999, 'Job Completed');
|
||||
} else {
|
||||
ws.close(4000, 'Already Initialized');
|
||||
}
|
||||
break;
|
||||
case "data":
|
||||
if(job !== null){
|
||||
if(msg.stream === "stdin"){
|
||||
eventBus.emit("stdin", msg.data)
|
||||
}else{
|
||||
ws.close(4004, "Can only write to stdin")
|
||||
case 'data':
|
||||
if (job !== null) {
|
||||
if (msg.stream === 'stdin') {
|
||||
eventBus.emit('stdin', msg.data);
|
||||
} else {
|
||||
ws.close(4004, 'Can only write to stdin');
|
||||
}
|
||||
} else {
|
||||
ws.close(4003, 'Not yet initialized');
|
||||
}
|
||||
}else{
|
||||
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")
|
||||
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');
|
||||
}
|
||||
}else{
|
||||
ws.close(4003, "Not yet initialized")
|
||||
}
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
}catch(error){
|
||||
ws.send(JSON.stringify({type: "error", message: error.message}))
|
||||
ws.close(4002, "Notified Error")
|
||||
} catch (error) {
|
||||
ws.send(JSON.stringify({ type: 'error', message: error.message }));
|
||||
ws.close(4002, 'Notified Error');
|
||||
// ws.close message is limited to 123 characters, so we notify over WS then close.
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
ws.on("close", async ()=>{
|
||||
if(job !== null){
|
||||
await job.cleanup()
|
||||
ws.on('close', async () => {
|
||||
if (job !== null) {
|
||||
await job.cleanup();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
setTimeout(()=>{
|
||||
setTimeout(() => {
|
||||
//Terminate the socket after 1 second, if not initialized.
|
||||
if(job === null)
|
||||
ws.close(4001, "Initialization Timeout");
|
||||
}, 1000)
|
||||
})
|
||||
if (job === null) ws.close(4001, 'Initialization Timeout');
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
router.post('/execute', async (req, res) => {
|
||||
|
||||
try{
|
||||
try {
|
||||
const job = await get_job(req.body);
|
||||
|
||||
await job.prime();
|
||||
|
@ -214,7 +271,7 @@ router.post('/execute', async (req, res) => {
|
|||
await job.cleanup();
|
||||
|
||||
return res.status(200).send(result);
|
||||
}catch(error){
|
||||
} catch (error) {
|
||||
return res.status(400).json(error);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -5,8 +5,7 @@ const logger = Logger.create('config');
|
|||
function parse_overrides(overrides) {
|
||||
try {
|
||||
return JSON.parse(overrides);
|
||||
}
|
||||
catch (e) {
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -16,15 +15,20 @@ function validate_overrides(overrides, options) {
|
|||
for (let key in overrides[language]) {
|
||||
if (
|
||||
![
|
||||
'max_process_count', 'max_open_files', 'max_file_size',
|
||||
'compile_memory_limit', 'run_memory_limit', 'compile_timeout',
|
||||
'run_timeout', 'output_max_size'
|
||||
'max_process_count',
|
||||
'max_open_files',
|
||||
'max_file_size',
|
||||
'compile_memory_limit',
|
||||
'run_memory_limit',
|
||||
'compile_timeout',
|
||||
'run_timeout',
|
||||
'output_max_size',
|
||||
].includes(key)
|
||||
) {
|
||||
logger.error(`Invalid overridden option: ${key}`);
|
||||
return false;
|
||||
}
|
||||
let option = options.find((o) => o.key === key);
|
||||
let option = options.find(o => o.key === key);
|
||||
let parser = option.parser;
|
||||
let raw = overrides[language][key];
|
||||
let value = parser(raw);
|
||||
|
@ -32,14 +36,19 @@ function validate_overrides(overrides, options) {
|
|||
for (let validator of validators) {
|
||||
let response = validator(value, raw);
|
||||
if (response !== true) {
|
||||
logger.error(`Failed to validate overridden option: ${key}`, response);
|
||||
logger.error(
|
||||
`Failed to validate overridden option: ${key}`,
|
||||
response
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
overrides[language][key] = value;
|
||||
}
|
||||
// Modifies the reference
|
||||
options[options.index_of(options.find((o) => o.key === 'limit_overrides'))] = overrides;
|
||||
options[
|
||||
options.index_of(options.find(o => o.key === 'limit_overrides'))
|
||||
] = overrides;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -135,32 +144,28 @@ const options = [
|
|||
},
|
||||
{
|
||||
key: 'compile_timeout',
|
||||
desc:
|
||||
'Max time allowed for compile stage in milliseconds',
|
||||
desc: 'Max time allowed for compile stage in milliseconds',
|
||||
default: 10000, // 10 seconds
|
||||
parser: parse_int,
|
||||
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
|
||||
},
|
||||
{
|
||||
key: 'run_timeout',
|
||||
desc:
|
||||
'Max time allowed for run stage in milliseconds',
|
||||
desc: 'Max time allowed for run stage in milliseconds',
|
||||
default: 3000, // 3 seconds
|
||||
parser: parse_int,
|
||||
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
|
||||
},
|
||||
{
|
||||
key: 'compile_memory_limit',
|
||||
desc:
|
||||
'Max memory usage for compile stage in bytes (set to -1 for no limit)',
|
||||
desc: 'Max memory usage for compile stage in bytes (set to -1 for no limit)',
|
||||
default: -1, // no limit
|
||||
parser: parse_int,
|
||||
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
|
||||
},
|
||||
{
|
||||
key: 'run_memory_limit',
|
||||
desc:
|
||||
'Max memory usage for run stage in bytes (set to -1 for no limit)',
|
||||
desc: 'Max memory usage for run stage in bytes (set to -1 for no limit)',
|
||||
default: -1, // no limit
|
||||
parser: parse_int,
|
||||
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
|
||||
|
@ -177,7 +182,7 @@ const options = [
|
|||
desc: 'Maximum number of concurrent jobs to run at one time',
|
||||
default: 64,
|
||||
parser: parse_int,
|
||||
validators: [(x) => x > 0 || `${x} cannot be negative`]
|
||||
validators: [x => x > 0 || `${x} cannot be negative`],
|
||||
},
|
||||
{
|
||||
key: 'limit_overrides',
|
||||
|
@ -187,10 +192,12 @@ const options = [
|
|||
default: {},
|
||||
parser: parse_overrides,
|
||||
validators: [
|
||||
(x) => !!x || `Invalid JSON format for the overrides\n${x}`,
|
||||
(overrides, _, options) => validate_overrides(overrides, options) || `Failed to validate the overrides`
|
||||
]
|
||||
}
|
||||
x => !!x || `Invalid JSON format for the overrides\n${x}`,
|
||||
(overrides, _, options) =>
|
||||
validate_overrides(overrides, options) ||
|
||||
`Failed to validate the overrides`,
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
logger.info(`Loading Configuration from environment`);
|
||||
|
|
|
@ -15,8 +15,6 @@ const logger = Logger.create('index');
|
|||
const app = express();
|
||||
expressWs(app);
|
||||
|
||||
|
||||
|
||||
(async () => {
|
||||
logger.info('Setting loglevel to', config.log_level);
|
||||
Logger.setLogLevel(config.log_level);
|
||||
|
|
166
api/src/job.js
166
api/src/job.js
|
@ -19,15 +19,12 @@ let gid = 0;
|
|||
let remainingJobSpaces = config.max_concurrent_jobs;
|
||||
let jobQueue = [];
|
||||
|
||||
|
||||
setInterval(()=>{
|
||||
setInterval(() => {
|
||||
// Every 10ms try resolve a new job, if there is an available slot
|
||||
if(jobQueue.length > 0 && remainingJobSpaces > 0){
|
||||
jobQueue.shift()()
|
||||
if (jobQueue.length > 0 && remainingJobSpaces > 0) {
|
||||
jobQueue.shift()();
|
||||
}
|
||||
}, 10)
|
||||
|
||||
|
||||
}, 10);
|
||||
|
||||
class Job {
|
||||
constructor({ runtime, files, args, stdin }) {
|
||||
|
@ -59,11 +56,11 @@ class Job {
|
|||
}
|
||||
|
||||
async prime() {
|
||||
if(remainingJobSpaces < 1){
|
||||
logger.info(`Awaiting job slot uuid=${this.uuid}`)
|
||||
await new Promise((resolve)=>{
|
||||
jobQueue.push(resolve)
|
||||
})
|
||||
if (remainingJobSpaces < 1) {
|
||||
logger.info(`Awaiting job slot uuid=${this.uuid}`);
|
||||
await new Promise(resolve => {
|
||||
jobQueue.push(resolve);
|
||||
});
|
||||
}
|
||||
|
||||
logger.info(`Priming job uuid=${this.uuid}`);
|
||||
|
@ -79,10 +76,15 @@ class Job {
|
|||
let file_path = path.join(this.dir, file.name);
|
||||
const rel = path.relative(this.dir, file_path);
|
||||
|
||||
if(rel.startsWith(".."))
|
||||
throw Error(`File path "${file.name}" tries to escape parent directory: ${rel}`)
|
||||
if (rel.startsWith('..'))
|
||||
throw Error(
|
||||
`File path "${file.name}" tries to escape parent directory: ${rel}`
|
||||
);
|
||||
|
||||
await fs.mkdir(path.dirname(file_path), {recursive: true, mode: 0o700})
|
||||
await fs.mkdir(path.dirname(file_path), {
|
||||
recursive: true,
|
||||
mode: 0o700,
|
||||
});
|
||||
await fs.chown(path.dirname(file_path), this.uid, this.gid);
|
||||
|
||||
await fs.write_file(file_path, file.content);
|
||||
|
@ -127,34 +129,33 @@ class Job {
|
|||
detached: true, //give this process its own process group
|
||||
});
|
||||
|
||||
if(eventBus === null){
|
||||
if (eventBus === null) {
|
||||
proc.stdin.write(this.stdin);
|
||||
proc.stdin.end();
|
||||
proc.stdin.destroy();
|
||||
}else{
|
||||
eventBus.on("stdin", (data) => {
|
||||
} else {
|
||||
eventBus.on('stdin', data => {
|
||||
proc.stdin.write(data);
|
||||
})
|
||||
});
|
||||
|
||||
eventBus.on("kill", (signal) => {
|
||||
proc.kill(signal)
|
||||
})
|
||||
eventBus.on('kill', signal => {
|
||||
proc.kill(signal);
|
||||
});
|
||||
}
|
||||
|
||||
const kill_timeout = set_timeout(
|
||||
async _ => {
|
||||
logger.info(`Timeout exceeded timeout=${timeout} uuid=${this.uuid}`)
|
||||
process.kill(proc.pid, 'SIGKILL')
|
||||
},
|
||||
timeout
|
||||
);
|
||||
const kill_timeout = set_timeout(async _ => {
|
||||
logger.info(
|
||||
`Timeout exceeded timeout=${timeout} uuid=${this.uuid}`
|
||||
);
|
||||
process.kill(proc.pid, 'SIGKILL');
|
||||
}, timeout);
|
||||
|
||||
proc.stderr.on('data', async data => {
|
||||
if(eventBus !== null) {
|
||||
eventBus.emit("stderr", data);
|
||||
if (eventBus !== null) {
|
||||
eventBus.emit('stderr', data);
|
||||
} else if (stderr.length > this.runtime.output_max_size) {
|
||||
logger.info(`stderr length exceeded uuid=${this.uuid}`)
|
||||
process.kill(proc.pid, 'SIGKILL')
|
||||
logger.info(`stderr length exceeded uuid=${this.uuid}`);
|
||||
process.kill(proc.pid, 'SIGKILL');
|
||||
} else {
|
||||
stderr += data;
|
||||
output += data;
|
||||
|
@ -162,11 +163,11 @@ class Job {
|
|||
});
|
||||
|
||||
proc.stdout.on('data', async data => {
|
||||
if(eventBus !== null){
|
||||
eventBus.emit("stdout", data);
|
||||
if (eventBus !== null) {
|
||||
eventBus.emit('stdout', data);
|
||||
} else if (stdout.length > this.runtime.output_max_size) {
|
||||
logger.info(`stdout length exceeded uuid=${this.uuid}`)
|
||||
process.kill(proc.pid, 'SIGKILL')
|
||||
logger.info(`stdout length exceeded uuid=${this.uuid}`);
|
||||
process.kill(proc.pid, 'SIGKILL');
|
||||
} else {
|
||||
stdout += data;
|
||||
output += data;
|
||||
|
@ -179,14 +180,14 @@ class Job {
|
|||
proc.stderr.destroy();
|
||||
proc.stdout.destroy();
|
||||
|
||||
await this.cleanup_processes()
|
||||
logger.debug(`Finished exit cleanup uuid=${this.uuid}`)
|
||||
await this.cleanup_processes();
|
||||
logger.debug(`Finished exit cleanup uuid=${this.uuid}`);
|
||||
};
|
||||
|
||||
proc.on('exit', async (code, signal) => {
|
||||
await exit_cleanup();
|
||||
|
||||
resolve({stdout, stderr, code, signal, output });
|
||||
resolve({ stdout, stderr, code, signal, output });
|
||||
});
|
||||
|
||||
proc.on('error', async err => {
|
||||
|
@ -243,7 +244,7 @@ class Job {
|
|||
};
|
||||
}
|
||||
|
||||
async execute_interactive(eventBus){
|
||||
async execute_interactive(eventBus) {
|
||||
if (this.state !== job_states.PRIMED) {
|
||||
throw new Error(
|
||||
'Job must be in primed state, current state: ' +
|
||||
|
@ -252,27 +253,27 @@ class Job {
|
|||
}
|
||||
|
||||
logger.info(
|
||||
`Interactively executing job uuid=${this.uuid} uid=${this.uid} gid=${
|
||||
this.gid
|
||||
} runtime=${this.runtime.toString()}`
|
||||
`Interactively executing job uuid=${this.uuid} uid=${
|
||||
this.uid
|
||||
} gid=${this.gid} runtime=${this.runtime.toString()}`
|
||||
);
|
||||
|
||||
if(this.runtime.compiled){
|
||||
eventBus.emit("stage", "compile")
|
||||
const {error, code, signal} = await this.safe_call(
|
||||
if (this.runtime.compiled) {
|
||||
eventBus.emit('stage', 'compile');
|
||||
const { error, code, signal } = await this.safe_call(
|
||||
path.join(this.runtime.pkgdir, 'compile'),
|
||||
this.files.map(x => x.name),
|
||||
this.runtime.timeouts.compile,
|
||||
this.runtime.memory_limits.compile,
|
||||
eventBus
|
||||
)
|
||||
);
|
||||
|
||||
eventBus.emit("exit", "compile", {error, code, signal})
|
||||
eventBus.emit('exit', 'compile', { error, code, signal });
|
||||
}
|
||||
|
||||
logger.debug('Running');
|
||||
eventBus.emit("stage", "run")
|
||||
const {error, code, signal} = await this.safe_call(
|
||||
eventBus.emit('stage', 'run');
|
||||
const { error, code, signal } = await this.safe_call(
|
||||
path.join(this.runtime.pkgdir, 'run'),
|
||||
[this.files[0].name, ...this.args],
|
||||
this.runtime.timeouts.run,
|
||||
|
@ -280,47 +281,50 @@ class Job {
|
|||
eventBus
|
||||
);
|
||||
|
||||
eventBus.emit("exit", "run", {error, code, signal})
|
||||
|
||||
eventBus.emit('exit', 'run', { error, code, signal });
|
||||
|
||||
this.state = job_states.EXECUTED;
|
||||
}
|
||||
|
||||
async cleanup_processes(dont_wait = []) {
|
||||
let processes = [1];
|
||||
logger.debug(`Cleaning up processes uuid=${this.uuid}`)
|
||||
logger.debug(`Cleaning up processes uuid=${this.uuid}`);
|
||||
|
||||
while (processes.length > 0) {
|
||||
processes = []
|
||||
processes = [];
|
||||
|
||||
const proc_ids = await fs.readdir('/proc');
|
||||
|
||||
const proc_ids = await fs.readdir("/proc");
|
||||
processes = await Promise.all(
|
||||
proc_ids.map(async proc_id => {
|
||||
if (isNaN(proc_id)) return -1;
|
||||
try {
|
||||
const proc_status = await fs.read_file(
|
||||
path.join('/proc', proc_id, 'status')
|
||||
);
|
||||
const proc_lines = proc_status.to_string().split('\n');
|
||||
const uid_line = proc_lines.find(line =>
|
||||
line.starts_with('Uid:')
|
||||
);
|
||||
const [_, ruid, euid, suid, fuid] =
|
||||
uid_line.split(/\s+/);
|
||||
|
||||
if (ruid == this.uid || euid == this.uid)
|
||||
return parse_int(proc_id);
|
||||
} catch {
|
||||
return -1;
|
||||
}
|
||||
|
||||
processes = await Promise.all(proc_ids.map(async (proc_id) => {
|
||||
if(isNaN(proc_id)) return -1;
|
||||
try{
|
||||
const proc_status = await fs.read_file(path.join("/proc",proc_id,"status"));
|
||||
const proc_lines = proc_status.to_string().split("\n")
|
||||
const uid_line = proc_lines.find(line=>line.starts_with("Uid:"))
|
||||
const [_, ruid, euid, suid, fuid] = uid_line.split(/\s+/);
|
||||
|
||||
if(ruid == this.uid || euid == this.uid)
|
||||
return parse_int(proc_id)
|
||||
|
||||
}catch{
|
||||
return -1
|
||||
}
|
||||
|
||||
return -1
|
||||
}))
|
||||
|
||||
processes = processes.filter(p => p > 0)
|
||||
|
||||
if(processes.length > 0)
|
||||
logger.debug(`Got processes to kill: ${processes} uuid=${this.uuid}`)
|
||||
return -1;
|
||||
})
|
||||
);
|
||||
|
||||
processes = processes.filter(p => p > 0);
|
||||
|
||||
if (processes.length > 0)
|
||||
logger.debug(
|
||||
`Got processes to kill: ${processes} uuid=${this.uuid}`
|
||||
);
|
||||
|
||||
for (const proc of processes) {
|
||||
// First stop the processes, but keep their resources allocated so they cant re-fork
|
||||
|
@ -339,12 +343,11 @@ class Job {
|
|||
// Could already be dead and just needs to be waited on
|
||||
}
|
||||
|
||||
if(!dont_wait.includes(proc))
|
||||
wait_pid(proc);
|
||||
if (!dont_wait.includes(proc)) wait_pid(proc);
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug(`Cleaned up processes uuid=${this.uuid}`)
|
||||
logger.debug(`Cleaned up processes uuid=${this.uuid}`);
|
||||
}
|
||||
|
||||
async cleanup_filesystem() {
|
||||
|
@ -382,7 +385,6 @@ class Job {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
Job,
|
||||
};
|
||||
|
|
|
@ -74,9 +74,8 @@ class Package {
|
|||
await new Promise((resolve, reject) => {
|
||||
read_stream.on('data', chunk => hash.update(chunk));
|
||||
read_stream.on('end', () => resolve());
|
||||
read_stream.on('error', error => reject(error))
|
||||
read_stream.on('error', error => reject(error));
|
||||
});
|
||||
|
||||
|
||||
const cs = hash.digest('hex');
|
||||
|
||||
|
|
|
@ -9,8 +9,17 @@ const runtimes = [];
|
|||
|
||||
class Runtime {
|
||||
constructor({
|
||||
language, version, aliases, pkgdir, runtime, timeouts, memory_limits, max_process_count,
|
||||
max_open_files, max_file_size, output_max_size
|
||||
language,
|
||||
version,
|
||||
aliases,
|
||||
pkgdir,
|
||||
runtime,
|
||||
timeouts,
|
||||
memory_limits,
|
||||
max_process_count,
|
||||
max_open_files,
|
||||
max_file_size,
|
||||
output_max_size,
|
||||
}) {
|
||||
this.language = language;
|
||||
this.version = version;
|
||||
|
@ -25,37 +34,67 @@ class Runtime {
|
|||
this.output_max_size = output_max_size;
|
||||
}
|
||||
|
||||
static compute_single_limit(language_name, limit_name, language_limit_overrides) {
|
||||
static compute_single_limit(
|
||||
language_name,
|
||||
limit_name,
|
||||
language_limit_overrides
|
||||
) {
|
||||
return (
|
||||
config.limit_overrides[language_name] && config.limit_overrides[language_name][limit_name]
|
||||
|| language_limit_overrides && language_limit_overrides[limit_name]
|
||||
|| config[limit_name]
|
||||
(config.limit_overrides[language_name] &&
|
||||
config.limit_overrides[language_name][limit_name]) ||
|
||||
(language_limit_overrides &&
|
||||
language_limit_overrides[limit_name]) ||
|
||||
config[limit_name]
|
||||
);
|
||||
}
|
||||
|
||||
static compute_all_limits(language_name, language_limit_overrides) {
|
||||
return {
|
||||
timeouts: {
|
||||
compile:
|
||||
this.compute_single_limit(language_name, 'compile_timeout', language_limit_overrides),
|
||||
run:
|
||||
this.compute_single_limit(language_name, 'run_timeout', language_limit_overrides)
|
||||
compile: this.compute_single_limit(
|
||||
language_name,
|
||||
'compile_timeout',
|
||||
language_limit_overrides
|
||||
),
|
||||
run: this.compute_single_limit(
|
||||
language_name,
|
||||
'run_timeout',
|
||||
language_limit_overrides
|
||||
),
|
||||
},
|
||||
memory_limits: {
|
||||
compile:
|
||||
this.compute_single_limit(language_name, 'compile_memory_limit', language_limit_overrides),
|
||||
run:
|
||||
this.compute_single_limit(language_name, 'run_memory_limit', language_limit_overrides)
|
||||
compile: this.compute_single_limit(
|
||||
language_name,
|
||||
'compile_memory_limit',
|
||||
language_limit_overrides
|
||||
),
|
||||
run: this.compute_single_limit(
|
||||
language_name,
|
||||
'run_memory_limit',
|
||||
language_limit_overrides
|
||||
),
|
||||
},
|
||||
max_process_count:
|
||||
this.compute_single_limit(language_name, 'max_process_count', language_limit_overrides),
|
||||
max_open_files:
|
||||
this.compute_single_limit(language_name, 'max_open_files', language_limit_overrides),
|
||||
max_file_size:
|
||||
this.compute_single_limit(language_name, 'max_file_size', language_limit_overrides),
|
||||
output_max_size:
|
||||
this.compute_single_limit(language_name, 'output_max_size', language_limit_overrides),
|
||||
}
|
||||
max_process_count: this.compute_single_limit(
|
||||
language_name,
|
||||
'max_process_count',
|
||||
language_limit_overrides
|
||||
),
|
||||
max_open_files: this.compute_single_limit(
|
||||
language_name,
|
||||
'max_open_files',
|
||||
language_limit_overrides
|
||||
),
|
||||
max_file_size: this.compute_single_limit(
|
||||
language_name,
|
||||
'max_file_size',
|
||||
language_limit_overrides
|
||||
),
|
||||
output_max_size: this.compute_single_limit(
|
||||
language_name,
|
||||
'output_max_size',
|
||||
language_limit_overrides
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
static load_package(package_dir) {
|
||||
|
@ -63,7 +102,14 @@ class Runtime {
|
|||
fss.read_file_sync(path.join(package_dir, 'pkg-info.json'))
|
||||
);
|
||||
|
||||
let { language, version, build_platform, aliases, provides, limit_overrides } = info;
|
||||
let {
|
||||
language,
|
||||
version,
|
||||
build_platform,
|
||||
aliases,
|
||||
provides,
|
||||
limit_overrides,
|
||||
} = info;
|
||||
version = semver.parse(version);
|
||||
|
||||
if (build_platform !== globals.platform) {
|
||||
|
@ -83,7 +129,10 @@ class Runtime {
|
|||
version,
|
||||
pkgdir: package_dir,
|
||||
runtime: language,
|
||||
...Runtime.compute_all_limits(lang.language, lang.limit_overrides)
|
||||
...Runtime.compute_all_limits(
|
||||
lang.language,
|
||||
lang.limit_overrides
|
||||
),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
@ -94,7 +143,7 @@ class Runtime {
|
|||
version,
|
||||
aliases,
|
||||
pkgdir: package_dir,
|
||||
...Runtime.compute_all_limits(language, limit_overrides)
|
||||
...Runtime.compute_all_limits(language, limit_overrides),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue