Skip to content

Commit

Permalink
fix: 문제 채점 경쟁 조건 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
rladydgn committed Dec 7, 2023
1 parent 256b750 commit 8e9f0dc
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,26 +303,43 @@ export class CompetitionService {
}

async saveScoreResult(scoreResultDto: ScoreResultDto) {
const submission = await this.submissionRepository.findOneBy({
id: scoreResultDto.submissionId,
});
if (!submission) throw new NotFoundException('제출 기록이 없습니다.');

let submission: Submission;
const result = {
testcaseId: scoreResultDto.testcaseId,
result: scoreResultDto.result,
timeUsage: scoreResultDto.timeUsage,
memoryUsage: scoreResultDto.memoryUsage,
};

submission.detail.push(result);
this.submissionRepository.save(submission);
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();

try {
submission = await queryRunner.manager.findOneBy(Submission, {
id: scoreResultDto.submissionId,
});
if (!submission) throw new NotFoundException('제출 기록이 없습니다.');

submission.detail.push(result);

await queryRunner.manager.update(Submission, submission.id, { detail: submission.detail });
await queryRunner.commitTransaction();
await queryRunner.release();
} catch (error) {
await queryRunner.rollbackTransaction();
await queryRunner.release();
throw error;
}

// 모두 제출했는지 확인
const testcaseNum: number = await this.problemService.getProblemTestcaseNum(
submission.problemId,
);
let totalResult: keyof typeof RESULT = 'CORRECT';
this.logger.debug(
`채점완료된 테스트 케이스 / 채점 해야할 테스트 케이스: ${submission.detail.length}/${testcaseNum}`,
);
if (testcaseNum === submission.detail.length) {
const user: User = await this.userRepository.findOneBy({ id: submission.userId });
const competition: Competition = await this.competitionRepository.findOneBy({
Expand Down

0 comments on commit 8e9f0dc

Please sign in to comment.