piston/api/src/executor/routes.js

34 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-02-20 15:13:56 +01:00
// {"language":"python","version":"3.9.1","files":{"code.py":"print('hello world')"},"args":[],"stdin":"","compile_timeout":10, "run_timeout":3, "main": "code.py"}
// {"success":true, "run":{"stdout":"hello world", "stderr":"", "error_code":0},"compile":{"stdout":"","stderr":"","error_code":0}}
2021-02-20 23:39:03 +01:00
const { get_latest_runtime_matching_language_version } = require('../runtime');
const { Job } = require('./job');
2021-02-20 15:13:56 +01:00
module.exports = {
async run_job(req, res){
// POST /jobs
var errored = false;
2021-02-20 23:39:03 +01:00
['language', 'version',
'files', 'main',
'args', 'stdin',
'compile_timeout', 'run_timeout',
].forEach(key => {
if(req.body[key] == undefined) errored = errored || res.json_error(`${key} is required`, 400);
});
if(errored) return errored;
2021-02-20 15:13:56 +01:00
const runtime = get_latest_runtime_matching_language_version(req.body.language, req.body.version);
2021-02-20 23:39:03 +01:00
if(runtime == undefined) return res.json_error(`${req.body.language}-${req.body.version} runtime is unknown`, 400);
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
const job = new Job(runtime, req.body.files, req.body.args, req.body.stdin, {run: req.body.run_timeout, compile: req.body.compile_timeout}, req.body.main);
await job.prime();
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
const result = await job.execute();
res.json_success(result);
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
await job.cleanup();
2021-02-20 15:13:56 +01:00
}
2021-02-20 23:39:03 +01:00
};