-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from Kusitms-POPTATO-DEV/feat/48-scheduler
Feat#48: 스케줄러 기능 구현
- Loading branch information
Showing
7 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/server/poptato/todo/application/TodoScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package server.poptato.todo.application; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import server.poptato.todo.domain.entity.Todo; | ||
import server.poptato.todo.domain.repository.TodoRepository; | ||
import server.poptato.todo.domain.value.TodayStatus; | ||
import server.poptato.todo.domain.value.Type; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TodoScheduler { | ||
private final TodoRepository todoRepository; | ||
@Scheduled(cron = "0 0 0 * * *") // 매일 자정에 실행 | ||
@Transactional | ||
public void updateTodoType() { | ||
// 1. TODAY 상태에서 INCOMPLETE인 할 일들을 YESTERDAY로 전환 (사용자별로 처리) | ||
Map<Long, List<Todo>> todayIncompleteTodosByUser = todoRepository.findByTypeAndTodayStatus(Type.TODAY, TodayStatus.INCOMPLETE) | ||
.stream() | ||
.collect(Collectors.groupingBy(Todo::getUserId)); // 사용자별로 그룹화 | ||
|
||
todayIncompleteTodosByUser.forEach((userId, todos) -> { | ||
Integer minBacklogOrder = todoRepository.findMinBacklogOrderByUserIdOrZero(userId); | ||
int startingOrder = minBacklogOrder - 1; | ||
|
||
for (Todo todo : todos) { | ||
todo.setType(Type.YESTERDAY); | ||
todo.setBacklogOrder(startingOrder--); | ||
} | ||
}); | ||
|
||
// 2. YESTERDAY 상태에서 INCOMPLETE인 할 일들을 BACKLOG로 전환 (BacklogOrder 유지) | ||
List<Todo> yesterdayIncompleteTodos = todoRepository.findByTypeAndTodayStatus(Type.YESTERDAY, TodayStatus.INCOMPLETE); | ||
yesterdayIncompleteTodos.forEach(todo -> { | ||
todo.setType(Type.BACKLOG); | ||
todo.setTodayStatus(null); | ||
}); | ||
|
||
// 3. 저장 | ||
for(Todo todo : todayIncompleteTodosByUser.values().stream().flatMap(List::stream).collect(Collectors.toList())){ | ||
todoRepository.save(todo); | ||
} | ||
for(Todo todo : yesterdayIncompleteTodos){ | ||
todoRepository.save(todo); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/test/java/server/poptato/todo/application/TodoSchedulerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package server.poptato.todo.application; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.scheduling.support.CronTrigger; | ||
import org.springframework.scheduling.support.SimpleTriggerContext; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
@SpringBootTest | ||
class TodoSchedulerTest { | ||
@Autowired | ||
TodoScheduler todoScheduler; | ||
@Test | ||
@DisplayName("updateType 메서드가 매일 자정에 실행되어야 한다") | ||
public void shouldTrigger_updateType_atEveryMidNight() throws ParseException { | ||
// Given - 상황 설정 | ||
String cronExpression = "0 0 0 * * *"; // 자정에 실행되는 cron 표현식 | ||
CronTrigger trigger = new CronTrigger(cronExpression); | ||
Date startTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2023/12/19 23:59:50"); | ||
SimpleTriggerContext context = new SimpleTriggerContext(); | ||
context.update(startTime, startTime, startTime); | ||
|
||
// 예상되는 실행 시간 목록 | ||
String expectedTime = "2023/12/20 00:00:00"; | ||
|
||
Date nextExecutionTime = trigger.nextExecutionTime(context); | ||
|
||
// Then - 결과 검증 | ||
String actualTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(nextExecutionTime); | ||
|
||
// 타입 불일치를 해결하기 위해 문자열 비교를 위한 Matcher 사용 | ||
Assertions.assertThat(actualTime).isEqualTo(expectedTime); // 여기서 `is`는 문자열을 비교할 때 사용 | ||
context.update(nextExecutionTime, nextExecutionTime, nextExecutionTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters