bw-hspc-contest-env/shared/submission-runner/java.cts

115 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-05-07 16:30:42 -04:00
import { join } from 'path';
import { exec, spawn } from 'child_process';
2024-02-17 15:43:38 -05:00
import * as util from 'util';
import type { IRunner, IRunnerParams, IRunnerReturn, RunResult } from './types.cjs';
import { timeoutSeconds } from './settings.cjs';
import kill = require('tree-kill');
2023-05-07 16:30:42 -04:00
const execPromise = util.promisify(exec);
2024-02-17 15:43:38 -05:00
interface IRunnerParamsJava extends IRunnerParams {
srcDir: string;
mainFile: string;
mainClass: string;
input: string;
outputCallback?: (data: string) => void;
}
export const runJava: IRunner<IRunnerParamsJava> = async function (
params: IRunnerParamsJava
): Promise<IRunnerReturn> {
2024-02-17 15:43:38 -05:00
console.log(`- BUILD: ${params.mainFile}`);
const compileCommand = `javac -cp ${join(params.srcDir, 'src')} ${params.mainFile} -d ${join(params.srcDir, 'build')}`;
2023-05-07 16:30:42 -04:00
2023-10-16 15:32:26 -04:00
try {
await execPromise(compileCommand);
2024-02-17 15:43:38 -05:00
} catch (e) {
const buildErrorText = e?.toString() ?? 'Unknown build errors.';
console.log('Build errors: ' + buildErrorText);
return {
success: false,
runResult: { kind: 'CompileFailed', resultKindReason: buildErrorText }
};
2023-10-16 15:32:26 -04:00
}
2023-05-07 16:30:42 -04:00
2024-02-17 15:43:38 -05:00
console.log(`- RUN: ${params.mainClass}`);
const runCommand = `java -cp "${join(params.srcDir, 'build')}" ${params.mainClass}`;
2023-05-07 16:30:42 -04:00
2024-02-17 15:43:38 -05:00
try {
let outputBuffer = '';
const child = spawn(runCommand, { shell: true });
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
outputBuffer += data.toString();
params.outputCallback?.(data.toString());
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
outputBuffer += data.toString();
params.outputCallback?.(data.toString());
});
2024-02-17 15:43:38 -05:00
const runStartTime = performance.now();
child.stdin.write(params.input);
child.stdin.end();
2023-10-16 15:32:26 -04:00
2024-02-17 15:43:38 -05:00
let timeLimitExceeded = false;
let completedNormally = false;
2024-02-17 15:43:38 -05:00
return {
success: true,
runResult: new Promise<RunResult>((resolve) => {
child.on('close', () => {
completedNormally = !timeLimitExceeded;
2024-02-17 15:43:38 -05:00
const runEndTime = performance.now();
const runtimeMilliseconds = Math.floor(runEndTime - runStartTime);
2023-10-16 15:32:26 -04:00
2024-02-17 15:43:38 -05:00
if (completedNormally) {
clearTimeout(timeoutHandle);
resolve({
kind: 'Completed',
output: outputBuffer,
exitCode: child.exitCode!,
runtimeMilliseconds
});
} else {
console.log(`Process terminated, total sandbox time: ${runtimeMilliseconds}ms`);
resolve({
kind: 'TimeLimitExceeded',
output: outputBuffer,
resultKindReason: `Timeout after ${timeoutSeconds} seconds`
});
}
});
const timeoutHandle = setTimeout(() => {
if (completedNormally) {
return;
}
console.log(`Run timed out after ${timeoutSeconds} seconds, killing process...`);
timeLimitExceeded = true;
child.stdin.end();
child.stdin.destroy();
child.stdout.destroy();
child.stderr.destroy();
child.kill('SIGKILL');
}, timeoutSeconds * 1000);
}),
killFunc() {
if (child.pid !== undefined) {
if (!completedNormally && !timeLimitExceeded) {
kill(child.pid);
params.outputCallback?.('\n[Manually stopped]');
}
}
}
};
} catch (error) {
return { success: false, runResult: { kind: 'RunError' } };
}
};