|
| 1 | +import config = require('../../config.js') |
| 2 | +import { cat, exec, mkdir, rm } from 'shelljs' |
| 3 | +import { SubmissionJob, SubmissionResult } from '../types/job' |
| 4 | +import * as path from 'path' |
| 5 | +import * as fs from 'fs' |
| 6 | + |
| 7 | +rm('-rf', config.RUNBOX.DIR) |
| 8 | +mkdir('-p', config.RUNBOX.DIR) |
| 9 | + |
| 10 | +async function execSubmission(job: SubmissionJob): Promise<SubmissionResult> { |
| 11 | + let currentJobDir = path.join(config.RUNBOX.DIR, job.id.toString()) |
| 12 | + mkdir('-p', currentJobDir) |
| 13 | + const LANG_CONFIG = config.LANGS[job.lang] |
| 14 | + |
| 15 | + fs.writeFileSync(path.join(currentJobDir, LANG_CONFIG.SOURCE_FILE), |
| 16 | + (new Buffer(job.source, 'base64')).toString('ascii')) |
| 17 | + |
| 18 | + exec(`docker run \\ |
| 19 | + --cpus="${LANG_CONFIG.CPU_SHARE}" \\ |
| 20 | + --memory="${LANG_CONFIG.MEM_LIMIT}" \\ |
| 21 | + --ulimit nofile=64:64 \\ |
| 22 | + --rm \\ |
| 23 | + --read-only \\ |
| 24 | + -v "${currentJobDir}":/usr/src/runbox \\ |
| 25 | + -w /usr/src/runbox \\ |
| 26 | + codingblocks/judge-worker-${job.lang} \\ |
| 27 | + /bin/judge.sh -t ${job.timelimit || 5} |
| 28 | + `) |
| 29 | + |
| 30 | + const stdout = exec(` |
| 31 | + head -c 65536 ${path.join(currentJobDir, 'run.stdout')} |
| 32 | + `) |
| 33 | + |
| 34 | + // Check for compile_stderr if can't find a stdout file ; stdout can be '' |
| 35 | + const compile_stderr = cat(path.join(currentJobDir, 'compile.stderr')).toString() |
| 36 | + const runguard_stderr = cat(path.join(currentJobDir, 'runguard.stderr')).toString() |
| 37 | + let stderr = runguard_stderr || compile_stderr || cat((path.join(currentJobDir, 'run.stderr')).toString()) |
| 38 | + |
| 39 | + rm('-rf', currentJobDir) |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +return { |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +export { |
| 48 | + execSubmission |
| 49 | +} |
0 commit comments