Skip to content

Commit

Permalink
Merge pull request #393 from SWM-NM/feat/#386
Browse files Browse the repository at this point in the history
♻️ [REFACTOR] 테스트 스케줄링 동시성 문제 해결
  • Loading branch information
aj4941 authored Oct 4, 2023
2 parents 2706ea3 + 502bab2 commit b7d9f14
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package swm_nm.morandi.domain.testDuring.dataLoader;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import swm_nm.morandi.domain.member.entity.Member;
import swm_nm.morandi.domain.member.repository.MemberRepository;
import swm_nm.morandi.domain.testDuring.dto.TestCheckDto;
import swm_nm.morandi.domain.testDuring.scheduler.TestMapManager;
import swm_nm.morandi.domain.testInfo.entity.Tests;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Component
@RequiredArgsConstructor
public class TestMapLoader implements CommandLineRunner {

private final MemberRepository memberRepository;

private final TestMapManager testMapManager;
@Override
public void run(String... args) {
List<Member> members = memberRepository.findAll();
members.stream().filter(member -> member.getCurrentTestId() != null)
.filter(member -> member.getCurrentTestId() != -1)
.map(member -> TestCheckDto.builder()
.testId(member.getCurrentTestId())
.bojId(member.getBojId())
.build()).forEach(testMapManager::addTest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
public class TestCheckDto {
private Long testId;
private String bojId;
private Long testTypeId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package swm_nm.morandi.domain.testDuring.scheduler;

import org.springframework.stereotype.Component;
import swm_nm.morandi.domain.testDuring.dto.TestCheckDto;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class TestMapManager {
private final ConcurrentHashMap<Long, TestCheckDto> testMap = new ConcurrentHashMap<>();
public ConcurrentHashMap<Long, TestCheckDto> getTestMap() {
return testMap;
}
public void addTest(TestCheckDto testCheckDto) {
testMap.put(testCheckDto.getTestId(), testCheckDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Component
@RequiredArgsConstructor
Expand All @@ -23,17 +24,18 @@ public class TestScheduler {
private final TestRepository testRepository;

private final CheckAttemptProblemService checkAttemptProblemService;
private final Set<TestCheckDto> testSet = new HashSet<>();

private final TestMapManager testMapManager;
public void addTest(TestCheckDto testCheckDto) {
log.info("Scheduler add testID : " + testCheckDto.getTestId());
testSet.add(testCheckDto);
testMapManager.addTest(testCheckDto);
}
@Scheduled(fixedRate = 60000)
public void callApiPeriodically() {
List<TestCheckDto> deleteList = new ArrayList<>();
testSet.forEach(testCheckDto -> {
log.info("Scheduler call testID : " + testCheckDto.getTestId());
Long testId = testCheckDto.getTestId();
ConcurrentHashMap<Long, TestCheckDto> testMap = testMapManager.getTestMap();
testMap.forEach((testId, testCheckDto) -> {
log.info("Scheduler call testID : " + testId);
Optional<Tests> result = testRepository.findById(testId);
if (result.isEmpty()) {
log.info("Scheduler Not Found testID : " + testCheckDto.getTestId());
Expand All @@ -42,21 +44,22 @@ public void callApiPeriodically() {
else {
Tests test = result.get();
if (test.getTestStatus() == TestStatus.COMPLETED) {
log.info("Scheduler Completed testID : " + testCheckDto.getTestId());
log.info("Scheduler Completed testID : " + testId);
deleteList.add(testCheckDto);
return;
}
Duration duration = Duration.between(test.getTestDate(), LocalDateTime.now());
Long minutes = duration.toMinutes();
if (minutes > test.getTestTime()) {
log.info("Scheduler exceed minutes testID : " + testCheckDto.getTestId());
log.info("Scheduler exceed minutes testID : " + testId);
deleteList.add(testCheckDto);
return;
}
checkAttemptProblemService.checkAttemptedProblemResult(test, testCheckDto.getBojId());
}
});

deleteList.forEach(testSet::remove);
deleteList.forEach(testCheckDto ->
testMap.remove(testCheckDto.getTestId(), testCheckDto));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@Component
@RequiredArgsConstructor
public class DataLoader implements CommandLineRunner {
public class TestTypeLoader implements CommandLineRunner {

private final TestTypeRepository testTypeRepository;
int size = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public Tests startTestByTestTypeId(TestType testType, Member member) {
Long testId = test.getTestId();
TestCheckDto testCheckDto = TestCheckDto.builder()
.testId(testId)
.testTypeId(testType.getTestTypeId())
.bojId(member.getBojId())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ private void extracted(Tests test) {
private void extracted(TestType testType, Member member, Tests test) {
TestCheckDto testCheckDto = TestCheckDto.builder()
.testId(test.getTestId())
.testTypeId(testType.getTestTypeId())
.bojId(member.getBojId())
.build();
testExitService.testExit(testCheckDto,member,test,testType);
Expand Down

0 comments on commit b7d9f14

Please sign in to comment.