Skip to content

Commit

Permalink
Merge pull request #261 from 6QuizOnTheBlock/be/feat/#255-refactorSch…
Browse files Browse the repository at this point in the history
…eduler

Be/feat/#255 refactor scheduler
  • Loading branch information
Henry-Cha authored May 12, 2024
2 parents 7af6e6c + 2259d29 commit 1b7f5cf
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 28 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ public interface ChallengeService {
ChallengeResponse getChallengeDetail(long id, Long groupId);

ChallengeSimpleResponse getChallengeSimple(long id);

void ChallengeClosing();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
import com.quiz.ourclass.global.exception.GlobalException;
import com.quiz.ourclass.global.util.AwsS3ObjectStorage;
import com.quiz.ourclass.global.util.UserAccessUtil;
import com.quiz.ourclass.global.util.scheduler.SchedulingService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -59,6 +62,7 @@ public class ChallengeServiceImpl implements ChallengeService {
private final ReportMapper reportMapper;
private final UserAccessUtil accessUtil;
private final AwsS3ObjectStorage awsS3ObjectStorage;
private final SchedulingService schedulingService;

@Override
public ChallengeSliceResponse getChallenges(ChallengeSliceRequest challengeSliceRequest) {
Expand All @@ -78,6 +82,8 @@ public long createChallenge(ChallengeRequest challengeRequest) {
.orElseThrow(() -> new GlobalException(ErrorCode.ORGANIZATION_NOT_FOUND));
challenge.setOrganization(organization);
challengeRepository.save(challenge);
schedulingService.scheduleTask(challenge, this::challengeClosing, challenge.getEndTime());

if (!challengeRequest.groups().isEmpty()) {
challengeRequest.groups().forEach(request -> {
ChallengeGroup group = challengeGroupMapper.groupMatchingRequestToChallengeGroup(
Expand Down Expand Up @@ -192,14 +198,17 @@ public ChallengeSimpleResponse getChallengeSimple(long id) {
return challengeMapper.challengeToChallengeSimpleResponse(challenge);
}

@Transactional
@Override
public void ChallengeClosing() {
protected void challengeClosing(Challenge challenge) {
challenge.setEndStatus(true);
challengeRepository.save(challenge);
}

@EventListener(ApplicationReadyEvent.class)
protected void challengeClosingReload() {
List<Challenge> challenges = challengeRepository.findAllByEndStatusIsFalse();
challenges.forEach(challenge -> {
if (challenge.getEndTime().isBefore(LocalDateTime.now())) {
challenge.setEndStatus(true);
challengeRepository.save(challenge);
challengeClosing(challenge);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package com.quiz.ourclass.global.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
@EnableScheduling
public class SchedulingConfig {

}
@Bean
public ThreadPoolTaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10); // 동시에 실행할 스레드 수 설정
scheduler.setThreadNamePrefix("scheduled-task-"); //스레드 이름 접두사
scheduler.setAwaitTerminationSeconds(60);
scheduler.setWaitForTasksToCompleteOnShutdown(true); //작업 완료까지 스프링종료를 대기
scheduler.initialize();
return scheduler;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.quiz.ourclass.global.util.scheduler;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.function.Consumer;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

@RequiredArgsConstructor
@Service
public class SchedulingService {

private final TaskScheduler taskScheduler;
private final TransactionTemplate transactionTemplate;

public <T> void scheduleTask(T target, Consumer<T> action, LocalDateTime executionTime) {
SchedulingTask<T> task = new SchedulingTask<>(target, action, transactionTemplate);
Instant executionDate = executionTime.atZone(ZoneId.systemDefault()).toInstant();

taskScheduler.schedule(task, executionDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.quiz.ourclass.global.util.scheduler;

import java.util.function.Consumer;
import lombok.AllArgsConstructor;
import org.springframework.transaction.support.TransactionTemplate;

@AllArgsConstructor
public class SchedulingTask<T> implements Runnable {

private final T target;
private final Consumer<T> task;
private final TransactionTemplate transactionTemplate;

@Override
public void run() {
transactionTemplate.execute(status -> {
task.accept(target);
return null;
});
}
}

0 comments on commit 1b7f5cf

Please sign in to comment.