2024-01-15 18:39:47 -05:00
|
|
|
import * as fs from 'fs-extra';
|
|
|
|
import { join } from 'path';
|
|
|
|
import os = require('os');
|
|
|
|
import { spawn } from 'child_process';
|
|
|
|
import kill from 'tree-kill';
|
|
|
|
import { RunResult, timeoutSeconds } from '../index.js';
|
|
|
|
import { IRunner, IRunnerReturn } from './types.js';
|
|
|
|
|
|
|
|
export const runCSharp: IRunner = async function (params: {
|
|
|
|
srcDir: string;
|
|
|
|
input: string;
|
|
|
|
outputCallback?: (data: string) => void;
|
|
|
|
}): IRunnerReturn {
|
|
|
|
console.log(`- RUN: ${params.srcDir}`);
|
|
|
|
const child = spawn('dotnet run', { shell: true, cwd: params.srcDir });
|
|
|
|
|
2024-02-17 15:06:49 -05:00
|
|
|
try {
|
|
|
|
let outputBuffer = '';
|
|
|
|
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-01-15 18:39:47 -05:00
|
|
|
|
2024-02-17 15:06:49 -05:00
|
|
|
let runStartTime = performance.now();
|
|
|
|
child.stdin.write(params.input);
|
|
|
|
child.stdin.end();
|
2024-01-15 18:39:47 -05:00
|
|
|
|
2024-02-17 15:06:49 -05:00
|
|
|
let timeLimitExceeded = false;
|
|
|
|
let completedNormally = false;
|
2024-01-15 18:39:47 -05:00
|
|
|
|
2024-02-17 15:06:49 -05:00
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
runResult: new Promise<RunResult>((resolve) => {
|
|
|
|
child.on('close', () => {
|
|
|
|
completedNormally = !timeLimitExceeded;
|
2024-01-15 18:39:47 -05:00
|
|
|
|
2024-02-17 15:06:49 -05:00
|
|
|
let runEndTime = performance.now();
|
|
|
|
const runtimeMilliseconds = Math.floor(runEndTime - runStartTime);
|
2024-01-15 18:39:47 -05:00
|
|
|
|
2024-02-17 15:06:49 -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`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2024-01-15 18:39:47 -05:00
|
|
|
|
2024-02-17 15:06:49 -05:00
|
|
|
let timeoutHandle = setTimeout(() => {
|
|
|
|
if (completedNormally) {
|
|
|
|
return;
|
|
|
|
}
|
2024-01-15 18:39:47 -05:00
|
|
|
|
2024-02-17 15:06:49 -05:00
|
|
|
console.log(`Run timed out after ${timeoutSeconds} seconds, killing process...`);
|
|
|
|
timeLimitExceeded = true;
|
2024-01-15 18:39:47 -05:00
|
|
|
|
2024-02-17 15:06:49 -05:00
|
|
|
child.stdin.end();
|
|
|
|
child.stdin.destroy();
|
|
|
|
child.stdout.destroy();
|
|
|
|
child.stderr.destroy();
|
|
|
|
if (child.pid !== undefined) {
|
|
|
|
kill(child.pid);
|
|
|
|
}
|
|
|
|
}, timeoutSeconds * 1000);
|
|
|
|
}),
|
|
|
|
killFunc() {
|
2024-01-15 18:39:47 -05:00
|
|
|
if (child.pid !== undefined) {
|
2024-02-17 15:06:49 -05:00
|
|
|
if (!completedNormally && !timeLimitExceeded) {
|
|
|
|
kill(child.pid);
|
|
|
|
params.outputCallback?.('\n[Manually stopped]');
|
|
|
|
}
|
2024-01-15 18:39:47 -05:00
|
|
|
}
|
|
|
|
}
|
2024-02-17 15:06:49 -05:00
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
return { success: false, runResult: { kind: 'RunError' } };
|
|
|
|
}
|
2024-01-15 18:39:47 -05:00
|
|
|
};
|