Added max_memory_usage parameter (#238)
* Added max_memory_usage parameter * Added max_memory_usage description to readme
This commit is contained in:
parent
f2973f0536
commit
a49acc7dbe
|
@ -1,13 +1,14 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
const config = require('../config');
|
||||||
const runtime = require('../runtime');
|
const runtime = require('../runtime');
|
||||||
const {Job} = require("../job");
|
const {Job} = require("../job");
|
||||||
const package = require('../package')
|
const package = require('../package')
|
||||||
const logger = require('logplease').create('api/v1');
|
const logger = require('logplease').create('api/v1');
|
||||||
|
|
||||||
router.post('/execute', async function(req, res){
|
router.post('/execute', async function(req, res){
|
||||||
const {language, version, files, stdin, args, run_timeout, compile_timeout} = req.body;
|
const {language, version, files, stdin, args, run_timeout, compile_timeout, max_memory_usage} = req.body;
|
||||||
|
|
||||||
if(!language || typeof language !== "string")
|
if(!language || typeof language !== "string")
|
||||||
{
|
{
|
||||||
|
@ -46,6 +47,21 @@ router.post('/execute', async function(req, res){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (max_memory_usage) {
|
||||||
|
if (typeof max_memory_usage !== "number" || max_memory_usage < 0) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.send({
|
||||||
|
message: "if specified, max_memory_usage must be a non-negative number"
|
||||||
|
})
|
||||||
|
} else if (max_memory_usage > config.max_memory_usage) {
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.send({
|
||||||
|
message: "max_memory_usage cannot exceed the configured limit of " + config.max_memory_usage
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,7 +84,8 @@ router.post('/execute', async function(req, res){
|
||||||
timeouts: {
|
timeouts: {
|
||||||
run: run_timeout || 3000,
|
run: run_timeout || 3000,
|
||||||
compile: compile_timeout || 10000
|
compile: compile_timeout || 10000
|
||||||
}
|
},
|
||||||
|
max_memory_usage: max_memory_usage || config.max_memory_usage
|
||||||
});
|
});
|
||||||
|
|
||||||
await job.prime();
|
await job.prime();
|
||||||
|
|
|
@ -108,6 +108,12 @@ const options = [
|
||||||
default: 1000000, //1MB
|
default: 1000000, //1MB
|
||||||
validators: []
|
validators: []
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: 'max_memory_usage',
|
||||||
|
desc: 'Max memory usage in bytes',
|
||||||
|
default: 256000000, //256MB
|
||||||
|
validators: []
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: 'repo_url',
|
key: 'repo_url',
|
||||||
desc: 'URL of repo index',
|
desc: 'URL of repo index',
|
||||||
|
|
|
@ -19,7 +19,7 @@ let gid = 0;
|
||||||
|
|
||||||
class Job {
|
class Job {
|
||||||
|
|
||||||
constructor({ runtime, files, args, stdin, timeouts }) {
|
constructor({ runtime, files, args, stdin, timeouts, max_memory_usage }) {
|
||||||
this.uuid = uuidv4();
|
this.uuid = uuidv4();
|
||||||
this.runtime = runtime;
|
this.runtime = runtime;
|
||||||
this.files = files.map((file,i) => ({
|
this.files = files.map((file,i) => ({
|
||||||
|
@ -30,6 +30,7 @@ class Job {
|
||||||
this.args = args;
|
this.args = args;
|
||||||
this.stdin = stdin;
|
this.stdin = stdin;
|
||||||
this.timeouts = timeouts;
|
this.timeouts = timeouts;
|
||||||
|
this.max_memory_usage = max_memory_usage;
|
||||||
|
|
||||||
this.uid = config.runner_uid_min + uid;
|
this.uid = config.runner_uid_min + uid;
|
||||||
this.gid = config.runner_gid_min + gid;
|
this.gid = config.runner_gid_min + gid;
|
||||||
|
@ -75,7 +76,8 @@ class Job {
|
||||||
'prlimit',
|
'prlimit',
|
||||||
'--nproc=' + config.max_process_count,
|
'--nproc=' + config.max_process_count,
|
||||||
'--nofile=' + config.max_open_files,
|
'--nofile=' + config.max_open_files,
|
||||||
'--fsize=' + config.max_file_size
|
'--fsize=' + config.max_file_size,
|
||||||
|
'--as=' + this.max_memory_usage
|
||||||
];
|
];
|
||||||
|
|
||||||
const proc_call = [
|
const proc_call = [
|
||||||
|
@ -161,7 +163,8 @@ class Job {
|
||||||
compile = await this.safe_call(
|
compile = await this.safe_call(
|
||||||
path.join(this.runtime.pkgdir, 'compile'),
|
path.join(this.runtime.pkgdir, 'compile'),
|
||||||
this.files.map(x => x.name),
|
this.files.map(x => x.name),
|
||||||
this.timeouts.compile
|
this.timeouts.compile,
|
||||||
|
config.max_memory_usage
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +173,8 @@ class Job {
|
||||||
const run = await this.safe_call(
|
const run = await this.safe_call(
|
||||||
path.join(this.runtime.pkgdir, 'run'),
|
path.join(this.runtime.pkgdir, 'run'),
|
||||||
[this.files[0].name, ...this.args],
|
[this.files[0].name, ...this.args],
|
||||||
this.timeouts.run
|
this.timeouts.run,
|
||||||
|
this.max_memory_usage
|
||||||
);
|
);
|
||||||
|
|
||||||
this.state = job_states.EXECUTED;
|
this.state = job_states.EXECUTED;
|
||||||
|
|
|
@ -210,6 +210,7 @@ This endpoint requests execution of some arbitrary code.
|
||||||
- `args` (*optional*) The arguments to pass to the program. Must be an array or left out. Defaults to `[]`.
|
- `args` (*optional*) The arguments to pass to the program. Must be an array or left out. Defaults to `[]`.
|
||||||
- `compile_timeout` (*optional*) The maximum time allowed for the compile stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `10000` (10 seconds).
|
- `compile_timeout` (*optional*) The maximum time allowed for the compile stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `10000` (10 seconds).
|
||||||
- `run_timeout` (*optional*) The maximum time allowed for the run stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `3000` (3 seconds).
|
- `run_timeout` (*optional*) The maximum time allowed for the run stage to finish before bailing out in milliseconds. Must be a number or left out. Defaults to `3000` (3 seconds).
|
||||||
|
- `max_memory_usage` (*optional*) The maximum amount of memory the run stage is allowed to use. Must be a number or left out. Defaults to `256000000` (256 MB)
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
|
@ -228,7 +229,8 @@ This endpoint requests execution of some arbitrary code.
|
||||||
"3"
|
"3"
|
||||||
],
|
],
|
||||||
"compile_timeout": 10000,
|
"compile_timeout": 10000,
|
||||||
"run_timeout": 3000
|
"run_timeout": 3000,
|
||||||
|
"max_memory_usage": 256000000
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
A typical response upon successful execution will contain 1 or 2 keys `run` and `compile`.
|
A typical response upon successful execution will contain 1 or 2 keys `run` and `compile`.
|
||||||
|
|
Loading…
Reference in New Issue