Skip to content

Commit

Permalink
(wip) dump
Browse files Browse the repository at this point in the history
  • Loading branch information
shine00chang committed Oct 7, 2024
1 parent a0ebad9 commit bf66df7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/api-server/src/services/submission.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SubmissionStatus,
type Submission,
type Problem
} from '@argoncs/types'
} from '@argoncs/types' /*=*/
import { rabbitMQ, judgerExchange, judgerTasksKey, submissionCollection, fetchDomainProblem, fetchContestProblem } from '@argoncs/common'
import { languageConfigs } from '../../configs/language.configs.js'

Expand Down Expand Up @@ -45,6 +45,7 @@ async function createSubmission ({ submission, userId, target }: { submission: N

const task: CompilingTask = {
submissionId,
problemId: problem.id,
type: JudgerTaskType.Compiling,
source: submission.source,
language: submission.language,
Expand Down
50 changes: 44 additions & 6 deletions packages/judge-daemon/src/services/compile.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ import { type CompilingTask, SandboxStatus, type CompileSucceeded, type CompileF
import { minio } from '@argoncs/common'
import { languageConfigs } from '../../configs/language.configs.js'

//=
/*=*/

export async function compileSubmission ({ task, boxId }: { task: CompilingTask, boxId: number }): Promise<CompileSucceeded | CompileFailed> {
const workDir = `/var/local/lib/isolate/${boxId}/box`
const config = languageConfigs[task.language]
const srcPath = path.join(workDir, config.srcFile)
const binaryPath = path.join(workDir, config.binaryFile)
const logPath = path.join(workDir, 'log.txt')
await fs.writeFile(srcPath, task.source)
let command = config.compileCommand
command = command.replaceAll('{src_path}', config.srcFile)
command = command.replaceAll('{binary_path}', config.binaryFile)

const command = config.compileCommand
.replaceAll('{src_path}', config.srcFile)
.replaceAll('{binary_path}', config.binaryFile)

console.log('command:', command);
console.log(workDir, config, srcPath, binaryPath, logPath);
console.log(workDir, config, srcPath, binaryPath);

const result = await runInSandbox(
{
task: {
constraints: task.constraints,
command,
stderrPath: 'log.txt',
stderrPath: logPath,
env: 'PATH=/bin:/usr/local/bin:/usr/bin'
},
boxId
Expand All @@ -47,3 +49,39 @@ export async function compileSubmission ({ task, boxId }: { task: CompilingTask,
}
}
}

export async function compileChecker ({ task, boxId }: { task: CompilingTask, boxId: number }): Promise<CompileSucceeded | CompileFailed> {

const workDir = `/var/local/lib/isolate/${boxId}/box`
const srcPath = path.join(workDir, 'checker.cpp')
const binaryPath = path.join(workDir, 'checker')
const logPath = path.join(workDir, 'checker-log.txt')
await fetchCheckerSource(srcPath);

const command = '/usr/bin/g++ -o2 -w -fmax-errors=3 -std=c++17 checker.cpp -lm -o checker'

console.log('command:', command);
console.log(workDir, srcPath, binaryPath);

const result = await runInSandbox(
{
task: {
constraints: {},
command,
stderrPath: logPath,
env: 'PATH=/bin:/usr/local/bin:/usr/bin'
},
boxId
})

console.log('compiled')
if (result.status !== SandboxStatus.Succeeded) {
return {
status: CompilingStatus.Failed,
log: (await fs.readFile(logPath)).toString()
}
}

await minio.fPutObject('checkers', task.problemId, binaryPath)
return { status: CompilingStatus.Succeeded }
}
7 changes: 7 additions & 0 deletions packages/judge-daemon/src/services/grading.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from './sandbox.services.js'

import { fetchBinary, fetchTestcase } from './storage.services.js'
import {NotFoundError} from 'http-errors-enhanced'

export async function gradeSubmission ({ task, boxId }: { task: GradingTask, boxId: number }): Promise<GradingResult> {

Expand All @@ -18,11 +19,16 @@ export async function gradeSubmission ({ task, boxId }: { task: GradingTask, box
const binaryPath = path.join(workDir, config.binaryFile)
const inputPath = path.join(workDir, 'in.txt')
const answerPath = path.join(workDir, 'ans.txt')
const checkerPath = path.join(workDir, 'checker');

await fetchBinary({ objectName: task.submissionId, destPath: binaryPath })
await fetchTestcase({ objectName: task.testcase.input.objectName, versionId: task.testcase.input.versionId, destPath: inputPath })
await fetchTestcase({ objectName: task.testcase.output.objectName, versionId: task.testcase.output.versionId, destPath: answerPath })
await makeExecutable(path.join(workDir, config.binaryFile))
await fetchChecker({
objectName: task.checker.objectName,
versionId: task.checker.versionId,
destPath: checkerPath })

let command = config.executeCommand
command = command.replaceAll('{binary_path}', config.binaryFile)
Expand All @@ -38,6 +44,7 @@ export async function gradeSubmission ({ task, boxId }: { task: GradingTask, box
},
boxId
})

//return new Promise((resolve, reject) => resolve({ message: '', status: GradingStatus.Accepted, memory: 1, time: 1, wallTime: 1}));

if (sandboxResult.status === SandboxStatus.Succeeded) {
Expand Down
3 changes: 0 additions & 3 deletions packages/judge-daemon/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ export async function startJudger (): Promise<void> {
submissionId: task.submissionId,
testcaseIndex: task.testcaseIndex
}
// send grade message
} else if (task.type === JudgerTaskType.Compiling) {
console.log('compiling')
result = {
type: JudgerResultType.Compiling,
result: (await compileSubmission({ task, boxId })),
submissionId: task.submissionId
}

// send grade message
} else {
throw Error('Invalid task type')
}
Expand Down
2 changes: 1 addition & 1 deletion packages/result-handler/src/services/result.services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fetchContestProblem, fetchDomainProblem, fetchSubmission, judgerExchange, judgerTasksKey, rabbitMQ, ranklistRedis, recalculateTeamTotalScore, submissionCollection, teamScoreCollection } from '@argoncs/common'
import { type CompilingResult, CompilingStatus, type GradingResult, GradingStatus, type GradingTask, JudgerTaskType, type Problem, SubmissionStatus } from '@argoncs/types'
import { type CompilingResult, CompilingStatus, type GradingResult, GradingStatus, type GradingTask, JudgerTaskType, type Problem, SubmissionStatus } from '@argoncs/types' /*=*/
import { NotFoundError } from 'http-errors-enhanced'
import path from 'path'

Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/types/compilation.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ export interface CompilingResultMessage {
submissionId: string
result: CompilingResult
}

export interface CheckerCompileTask {
type: JudgerTaskType.CheckerCompile,
source: string,
problemId: string,
}

0 comments on commit bf66df7

Please sign in to comment.