Skip to content

Commit

Permalink
Merge pull request #589 from SWM-Morandi/dev
Browse files Browse the repository at this point in the history
[FIX] 테스트 문제 추출, 스케줄링시 스레드풀 추가 및 수정
  • Loading branch information
aj4941 authored Dec 6, 2023
2 parents f66050d + a38b1dd commit 971e404
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.*;

@Component
@RequiredArgsConstructor
@Slf4j
@EnableAsync
public class TestScheduler {

private final TestRepository testRepository;
Expand All @@ -33,37 +31,41 @@ public void addTest(TestCheckDto testCheckDto) {
testMapManager.addTest(testCheckDto);
}
@Scheduled(fixedRate = 60000)
@Async
public Future<Void> callApiPeriodically() {
public void callApiPeriodically() {
List<TestCheckDto> deleteList = new ArrayList<>();
ConcurrentHashMap<Long, TestCheckDto> testMap = testMapManager.getTestMap();
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<CompletableFuture<Void>> futures = new ArrayList<>();
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());
deleteList.add(testCheckDto);
}
else {
Tests test = result.get();
if (test.getTestStatus() == TestStatus.COMPLETED) {
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 : " + testId);
deleteList.add(testCheckDto);
return;
}
checkAttemptProblemService.checkAttemptedProblemResult(test, testCheckDto.getBojId());
}
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
log.info("Scheduler call testID : " + testId);
Optional<Tests> result = testRepository.findById(testId);
if (result.isEmpty()) {
log.info("Scheduler Not Found testID : " + testCheckDto.getTestId());
deleteList.add(testCheckDto);
} else {
Tests test = result.get();
if (test.getTestStatus() == TestStatus.COMPLETED) {
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 : " + testId);
deleteList.add(testCheckDto);
return;
}
checkAttemptProblemService.checkAttemptedProblemResult(test, testCheckDto.getBojId());
}
}, executorService);
futures.add(future);
});

CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

deleteList.forEach(testCheckDto ->
testMap.remove(testCheckDto.getTestId(), testCheckDto));
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -34,6 +36,7 @@ public List<BojProblem> getProblemsByApi(TestType testType, String bojId) {
List<DifficultyRange> difficultyRanges = testType.getDifficultyRanges();
List<BojProblem> bojProblems = BojProblem.initBojProblems(difficultyRanges);
String apiUrl = "https://solved.ac/api/v3/search/problem";
ExecutorService executorService = Executors.newFixedThreadPool(5);

List<CompletableFuture<Void>> futures = bojProblems.stream()
.map(bojProblem -> CompletableFuture.runAsync(() -> {
Expand All @@ -60,7 +63,7 @@ public List<BojProblem> getProblemsByApi(TestType testType, String bojId) {
throw new MorandiException(TestErrorCode.JSON_PARSE_ERROR);
}
}
})).collect(Collectors.toList());
}, executorService)).collect(Collectors.toList());

CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ void testScheduler() {

// when
long startTime = System.currentTimeMillis();
Future<Void> future = testScheduler.callApiPeriodically();
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
testScheduler.callApiPeriodically();
long endTime = System.currentTimeMillis();

// then
Expand Down

0 comments on commit 971e404

Please sign in to comment.