Skip to content

fix: a part of #15 is fixed #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Worker } from '../src/CodeExecutor';
import { Worker, languages } from '../src/CodeExecutor';
import logger from '../src/utils/logger';

/**
* name, redis, folderPath, default folderPath is /tmp/code-exec
Expand All @@ -8,6 +9,8 @@ import { Worker } from '../src/CodeExecutor';
const worker = new Worker('myExecutor', 'redis://127.0.0.1:6379');

async function main() {
logger.info(languages);

/* array of languages is optional argument */
await worker.build(['Python', 'Bash']);

Expand All @@ -16,6 +19,8 @@ async function main() {
worker.pause();

worker.resume();

// worker.stop();
}

main();
12 changes: 8 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
},
"homepage": "https://github.com/csivitu/code-executor#readme",
"devDependencies": {
"@types/dockerode": "^2.5.34",
"@types/randomstring": "^1.1.6",
"@types/uuid": "^8.3.0",
"@types/bull": "^3.14.2",
"@types/node": "^14.6.3",
"@typescript-eslint/eslint-plugin": "^4.0.1",
Expand All @@ -50,9 +53,6 @@
}
},
"dependencies": {
"@types/dockerode": "^2.5.34",
"@types/randomstring": "^1.1.6",
"@types/uuid": "^8.3.0",
"bull": "^3.18.0",
"del": "^6.0.0",
"dockerode": "^3.2.1",
Expand Down
5 changes: 5 additions & 0 deletions src/CodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { v4 as uuid } from 'uuid';

import { CodeParams, Result } from './models/models';
import logger from './utils/logger';
import { extension } from './utils/findExtension';

const languages = Object.keys(extension);

export default class CodeExecutor {
private queue: Bull.Queue;
Expand Down Expand Up @@ -44,3 +47,5 @@ export default class CodeExecutor {
}

export { default as Worker } from './Worker';

export { languages };
80 changes: 28 additions & 52 deletions src/Runner.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import Docker from 'dockerode';
import path from 'path';
import { performance } from 'perf_hooks';
import {
performance,
} from 'perf_hooks';
import del from 'del';
import writeToFile from './utils/writeToFile';
import generateFolder from './utils/generateFolder';
import decodeBase64 from './utils/decodeBase64';
import logger from './utils/logger';
import findExtension from './utils/findExtension';
import {
TestCase,
Result,
Tests,
} from './models/models';
import getOutput from './utils/getOutput';
import saveCode from './utils/saveCode';

interface RunnerOpts {
id: string;
Expand All @@ -31,30 +30,6 @@ export default class Runner {
this.docker = docker;
}

private static async saveCode(
folderPath: string,
code: string,
testCases: TestCase[],
base64: boolean,
language: string,
): Promise < string > {
const folder = await generateFolder(folderPath);
const extension = findExtension(language);
const promisesToKeep = [(base64)
? writeToFile(path.join(folder, `code.${extension}`), decodeBase64(code))
: writeToFile(path.join(folder, `code.${extension}`), code),
];
for (let i = 0; i < testCases.length; i += 1) {
const [input, output] = (base64)
? [decodeBase64(testCases[i].input), decodeBase64(testCases[i].output)]
: [testCases[i].input, testCases[i].output];
promisesToKeep.push(writeToFile(path.join(folder, `in${i}.txt`), input));
promisesToKeep.push(writeToFile(path.join(folder, `out${i}.txt`), output));
}
await Promise.all(promisesToKeep);
return folder;
}

async run({
id,
tag,
Expand All @@ -65,37 +40,38 @@ export default class Runner {
language,
timeout,
}: RunnerOpts): Promise < Result > {
const Path = await Runner.saveCode(folderPath, code, testCases, base64, language);
const container = await this.docker.createContainer({
Image: tag,
Cmd: ['bash', '/start.sh', `${testCases.length}`, `${timeout}`],
HostConfig: {
Mounts: [{
Source: Path,
Target: '/app',
Type: 'bind',
}],
},
});

const Paths = await saveCode(folderPath, code, testCases, base64, language);
const promisesToKeep: Array<Promise<Array<object>>> = [];
for (let i = 0; i < Paths.length; i += 1) {
promisesToKeep.push(this.docker.run(tag, ['bash', '/start.sh', `${i}`, `${timeout}`], null, {
HostConfig: {
AutoRemove: true,
Mounts: [{
Source: Paths[i],
Target: '/app',
Type: 'bind',
}],
},
}));
}
logger.info(`Starting process ${id}`);

const t0 = performance.now();
await container.start();
await container.wait();
await Promise.all(promisesToKeep);
const t1 = performance.now();
logger.info(`Process ${id} completed in ${(t1 - t0) / 1000} seconds`);

logger.info(`Process ${id} completed in ${t1 / 1000 - t0 / 1000} seconds`);

container.remove();
const [output, runTime, error, exitCodes] = await getOutput(Path, testCases.length);
del(Path, {
force: true,
const [output, runTime, error, exitCodes] = await getOutput(Paths, testCases.length);
Paths.forEach((Path) => {
del(Path, {
force: true,
});
});

const tests: Tests[] = [];
for (let i = 0; i < testCases.length; i += 1) {
const expectedOutput = testCases[i].output;
const expectedOutput = (base64)
? decodeBase64(testCases[i].output)
: testCases[i].output;
const obtainedOutput = output[i].toString();
const time = runTime[i].toString().split('\n');
const exitCode = parseInt(exitCodes[i].toString(), 10);
Expand Down
4 changes: 4 additions & 0 deletions src/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ export default class Worker {
resume() {
this.queue.resume();
}

stop() {
this.queue.close();
}
}
9 changes: 3 additions & 6 deletions src/langs/Bash/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | bash /app/code.sh) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "(cat /app/in$i.txt | bash /app/Main.sh) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/C/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | gcc -o /app/code /app/code.c -lm && ./app/code ) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "( cat /app/in$i.txt | ( gcc -o /app/Main /app/Main.c -pthread -lm && /app/Main ) ) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/Cplusplus/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | g++ -o /app/code /app/code.cpp && ./app/code ) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "( cat /app/in$i.txt | ( g++ -o /app/Main /app/Main.cpp -pthread -lm && /app/Main ) ) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/Java/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | javac /app/Code.java) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "( cat /app/in$i.txt | ( javac /app/Main.java && java Main ) ) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/Javascript/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | node /app/code.js) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "(cat /app/in$i.txt | node /app/Main.js) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/Perl/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | perl /app/code.pl) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "(cat /app/in$i.txt | perl /app/Main.pl) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/Python/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | python3 /app/code.py) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "(cat /app/in$i.txt | python3 /app/Main.py) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/Ruby/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | ruby /app/code.rb) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "(cat /app/in$i.txt | ruby /app/Main.rb) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/Rust/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | rust /app/code.rs) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "(cat /app/in$i.txt | rust /app/Main.rs) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
9 changes: 3 additions & 6 deletions src/langs/Swift/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
END=$1
for ((i=0;i<END;i++))
do
i=$1
date +%s%N > /app/time$i.txt
timeout $2 bash -c "(cat /app/in$i.txt | swift /app/code.swift) 2> /app/error$i.txt 1> /app/output$i.txt"
timeout $2 bash -c "(cat /app/in$i.txt | swift /app/Main.swift) 2> /app/error$i.txt 1> /app/output$i.txt"
echo $? > /app/exitCode$i.txt
date +%s%N >> /app/time$i.txt
done
date +%s%N >> /app/time$i.txt
10 changes: 5 additions & 5 deletions src/utils/getOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import path from 'path';

const readFileAsync = util.promisify(fs.readFile);

export default async function getOutput(Path: string, len: number) {
export default async function getOutput(Paths: string[], len: number) {
const output = [];
const runTime = [];
const error = [];
const exitCodes = [];
for (let i = 0; i < len; i += 1) {
output.push(readFileAsync(path.join(Path, `output${i}.txt`)));
runTime.push(readFileAsync(path.join(Path, `time${i}.txt`)));
error.push(readFileAsync(path.join(Path, `error${i}.txt`)));
exitCodes.push(readFileAsync(path.join(Path, `exitCode${i}.txt`)));
output.push(readFileAsync(path.join(Paths[i], `output${i}.txt`)));
runTime.push(readFileAsync(path.join(Paths[i], `time${i}.txt`)));
error.push(readFileAsync(path.join(Paths[i], `error${i}.txt`)));
exitCodes.push(readFileAsync(path.join(Paths[i], `exitCode${i}.txt`)));
}
return Promise.all([Promise.all(output),
Promise.all(runTime),
Expand Down
Loading