piston/api/src/runtime.js

157 lines
4.4 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');
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
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_path,
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_path = flake_path;
this.package_support = packageSupport;
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_path = this.flake_path;
2021-05-08 02:30:40 +02:00
function _ensure_built(key) {
const command = `nix build ${flake_path}.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(language_name) {
logger.info(`Loading ${language_name}`);
const flake_path = `${config.flake_path}#pistonRuntimeSets.${config.runtime_set}.${language_name}`;
const metadata_command = `nix eval --json ${flake_path}.metadata`;
const metadata = JSON.parse(cp.execSync(metadata_command));
const this_runtime = new Runtime({
...metadata,
...Runtime.compute_all_limits(
metadata.language,
metadata.limitOverrides
),
flake_path,
});
2021-03-13 06:01:04 +01:00
this_runtime.ensure_built();
2021-03-13 06:01:04 +01:00
runtimes.push(this_runtime);
2021-03-13 06:01:04 +01:00
logger.debug(`Package ${language_name} was loaded`);
2021-02-20 15:13:56 +01:00
}
get compiled() {
return this.compile !== null;
}
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;