|
1 | 1 | import express from 'express';
|
2 | 2 | import fs from 'fs-extra';
|
| 3 | +import Promise from 'bluebird'; |
3 | 4 | import uuid from 'uuid';
|
4 | 5 | import path from 'path';
|
5 | 6 | import { GitHubApi } from '/apis';
|
6 | 7 | import { execute } from '/common/util';
|
7 | 8 | import webhook from '/common/webhook';
|
| 9 | +import { ImageBuilder, WorkerBuilder } from '/tracers'; |
| 10 | +import { memoryLimit, timeLimit } from '/common/config'; |
8 | 11 |
|
9 | 12 | const router = express.Router();
|
10 | 13 |
|
11 |
| -const repoPath = path.resolve(__dirname, '..', 'public', 'tracers'); |
12 |
| -const getCodesPath = (...args) => path.resolve(__dirname, '..', 'public', 'codes', ...args); |
13 |
| - |
14 |
| -const getJsWorker = (req, res, next) => { |
15 |
| - res.sendFile(path.resolve(repoPath, 'src', 'languages', 'js', 'tracers', 'build', 'tracers.js')); |
16 |
| -}; |
17 |
| - |
18 | 14 | const trace = lang => (req, res, next) => {
|
19 | 15 | const { code } = req.body;
|
20 |
| - const tempPath = getCodesPath(uuid.v4()); |
| 16 | + const tempPath = path.resolve(__dirname, '..', 'public', 'codes', uuid.v4()); |
| 17 | + const tracesPath = path.resolve(tempPath, 'traces.json'); |
21 | 18 | fs.outputFile(path.resolve(tempPath, `Main.${lang}`), code)
|
22 |
| - .then(() => execute(`./bin/compile ${lang} ${tempPath}`, repoPath, { stdout: null, stderr: null })) |
23 |
| - .then(() => execute(`./bin/run ${lang} ${tempPath}`, repoPath, { stdout: null, stderr: null })) |
24 |
| - .then(() => res.sendFile(path.resolve(tempPath, 'traces.json'))) |
| 19 | + .then(() => { |
| 20 | + const builder = builderMap[lang]; |
| 21 | + const containerName = uuid.v4(); |
| 22 | + let killed = false; |
| 23 | + const timer = setTimeout(() => { |
| 24 | + execute(`docker kill ${containerName}`).then(() => { |
| 25 | + killed = true; |
| 26 | + }); |
| 27 | + }, timeLimit); |
| 28 | + return execute([ |
| 29 | + 'docker run --rm', |
| 30 | + `--name=${containerName}`, |
| 31 | + '-w=/usr/tracer', |
| 32 | + `-v=${tempPath}:/usr/tracer:rw`, |
| 33 | + `-m=${memoryLimit}m`, |
| 34 | + builder.imageName, |
| 35 | + ].join(' ')).catch(error => { |
| 36 | + if (killed) throw new Error('Time Limit Exceeded'); |
| 37 | + throw error; |
| 38 | + }).finally(() => clearTimeout(timer)); |
| 39 | + }) |
| 40 | + .then(() => fs.pathExists(tracesPath)) |
| 41 | + .then(exists => { |
| 42 | + if (!exists) throw new Error('Traces Not Found'); |
| 43 | + res.sendFile(tracesPath); |
| 44 | + }) |
25 | 45 | .catch(next)
|
26 | 46 | .finally(() => fs.remove(tempPath));
|
27 | 47 | };
|
28 | 48 |
|
29 |
| -const buildRelease = release => ( |
30 |
| - fs.pathExistsSync(repoPath) ? |
31 |
| - execute(`git fetch && ! git diff-index --quiet ${release.target_commitish} -- ':!package-lock.json'`, repoPath) : |
32 |
| - execute(`git clone https://github.com/algorithm-visualizer/tracers.git ${repoPath}`, __dirname) |
33 |
| -).then(() => execute(`git reset --hard ${release.target_commitish} && npm install && npm run build && ./bin/build`, repoPath)); |
| 49 | +const builderMap = { |
| 50 | + js: new WorkerBuilder(), |
| 51 | + cpp: new ImageBuilder('cpp'), |
| 52 | + java: new ImageBuilder('java'), |
| 53 | +}; |
34 | 54 |
|
35 |
| -GitHubApi.getLatestRelease('algorithm-visualizer', 'tracers').then(buildRelease).catch(console.error); |
| 55 | +Promise.map(Object.keys(builderMap), lang => { |
| 56 | + const builder = builderMap[lang]; |
| 57 | + return GitHubApi.getLatestRelease('algorithm-visualizer', `tracers.${lang}`).then(builder.build); |
| 58 | +}).catch(console.error); |
36 | 59 |
|
37 |
| -webhook.on('tracers', (event, data) => { |
38 |
| - switch (event) { |
39 |
| - case 'release': |
40 |
| - buildRelease(data.release).catch(console.error); |
41 |
| - break; |
| 60 | +webhook.on('release', (repo, data) => { |
| 61 | + const result = /^tracers\.(\w+)$/.exec(repo); |
| 62 | + if (result) { |
| 63 | + const [, lang] = result; |
| 64 | + const builder = builderMap[lang]; |
| 65 | + builder.build(data.release).catch(console.error); |
42 | 66 | }
|
43 | 67 | });
|
44 | 68 |
|
45 |
| -router.route('/js') |
46 |
| - .get(getJsWorker); |
47 |
| - |
48 |
| -router.route('/java') |
49 |
| - .post(trace('java')); |
50 |
| - |
51 |
| -router.route('/cpp') |
52 |
| - .post(trace('cpp')); |
| 69 | +Object.keys(builderMap).forEach(lang => { |
| 70 | + const builder = builderMap[lang]; |
| 71 | + if (builder instanceof ImageBuilder) { |
| 72 | + router.post(`/${lang}`, trace(lang)); |
| 73 | + } else if (builder instanceof WorkerBuilder) { |
| 74 | + router.get(`/${lang}`, (req, res) => res.sendFile(builder.workerPath)); |
| 75 | + } |
| 76 | +}); |
53 | 77 |
|
54 | 78 | export default router;
|
0 commit comments