piston/api/src/runtime.js

170 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-02-20 23:39:03 +01:00
const logger = require('logplease').create('runtime');
const cp = require('child_process');
2021-02-20 23:39:03 +01:00
const config = require('./config');
const globals = require('./globals');
const fss = require('fs');
const path = require('path');
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
const runtimes = [];
2021-02-20 15:13:56 +01:00
2021-02-20 15:13:56 +01:00
class Runtime {
2021-10-02 14:08:36 +02:00
constructor({
2021-10-08 15:16:57 +02:00
language,
version,
aliases,
runtime,
run,
compile,
packageSupport,
flake_key,
2021-10-08 15:16:57 +02:00
timeouts,
memory_limits,
max_process_count,
max_open_files,
max_file_size,
output_max_size
2021-10-02 14:08:36 +02:00
}) {
2021-05-08 02:30:40 +02:00
this.language = language;
this.runtime = runtime;
2021-10-02 14:08:36 +02:00
this.timeouts = timeouts;
this.memory_limits = memory_limits;
this.max_process_count = max_process_count;
this.max_open_files = max_open_files;
this.max_file_size = max_file_size;
this.output_max_size = output_max_size;
this.aliases = aliases;
this.version = version;
this.run = run;
this.compile = compile;
this.flake_key = flake_key;
this.package_support = packageSupport;
2021-10-02 14:08:36 +02:00
}
2021-10-02 14:08:36 +02:00
2021-10-08 15:16:57 +02:00
static compute_single_limit(
language_name,
limit_name,
language_limit_overrides
) {
2021-10-02 14:08:36 +02:00
return (
2021-10-08 15:16:57 +02:00
(config.limit_overrides[language_name] &&
config.limit_overrides[language_name][limit_name]) ||
(language_limit_overrides &&
language_limit_overrides[limit_name]) ||
config[limit_name]
2021-10-02 14:08:36 +02:00
);
}
static compute_all_limits(language_name, language_limit_overrides) {
return {
timeouts: {
2021-10-08 15:16:57 +02:00
compile: this.compute_single_limit(
language_name,
'compile_timeout',
language_limit_overrides
),
run: this.compute_single_limit(
language_name,
'run_timeout',
language_limit_overrides
),
2021-10-02 14:08:36 +02:00
},
memory_limits: {
2021-10-08 15:16:57 +02:00
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
),
2021-10-02 14:08:36 +02:00
},
2021-10-08 15:16:57 +02:00
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
),
};
}
ensure_built(){
logger.info(`Ensuring ${this} is built`);
2021-05-08 02:30:40 +02:00
const flake_key = this.flake_key;
2021-05-08 02:30:40 +02:00
function _ensure_built(key){
const command = `nix build ${config.flake_path}#pistonRuntimes.${flake_key}.metadata.${key} --no-link`;
cp.execSync(command, {stdio: "pipe"})
2021-05-08 02:30:40 +02:00
}
_ensure_built("run");
if(this.compiled) _ensure_built("compile");
2021-02-20 15:13:56 +01:00
logger.debug(`Finished ensuring ${this} is installed`)
2021-02-20 15:13:56 +01:00
}
static load_runtime(flake_key){
logger.info(`Loading ${flake_key}`)
const metadata_command = `nix eval --json ${config.flake_path}#pistonRuntimes.${flake_key}.metadata`;
const metadata = JSON.parse(cp.execSync(metadata_command));
const this_runtime = new Runtime({
...metadata,
...Runtime.compute_all_limits(
metadata.language,
metadata.limit_overrides
),
flake_key
});
2021-03-13 06:01:04 +01:00
this_runtime.ensure_built();
2021-03-13 06:01:04 +01:00
runtimes.push(this_runtime);
logger.debug(`Package ${flake_key} was loaded`);
2021-03-13 06:01:04 +01:00
2021-02-20 15:13:56 +01:00
}
get compiled() {
return this.compile !== null;
}
get id(){
return runtimes.indexOf(this);
2021-05-08 02:30:40 +02:00
}
2021-05-08 02:20:21 +02:00
toString() {
return `${this.language}-${this.version}`;
2021-05-08 02:30:40 +02:00
}
2021-02-20 15:13:56 +01:00
}
2021-02-20 23:39:03 +01:00
module.exports = runtimes;
module.exports.Runtime = Runtime;
module.exports.load_runtime = Runtime.load_runtime;