From 9f5b7c8dfbe743583a85f383abd13e055b9f0f77 Mon Sep 17 00:00:00 2001 From: hyunw9 Date: Wed, 4 Sep 2024 01:18:50 +0900 Subject: [PATCH] =?UTF-8?q?Feat(infra)=20:=2010=EB=B6=84=EB=8B=A8=EC=9C=84?= =?UTF-8?q?=20=EC=8A=A4=EC=BC=80=EC=A4=84=EB=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Spring Scheduler + Cron tab을 이용하여 구현하였습니다. --- .../reply/ReplyScheduledServiceTest.java | 37 ------------------- .../clody/infra/config/SchedulingConfig.java | 10 +++++ .../models/alarm/DiaryNotifyService.java | 36 ++++++++++++++++++ 3 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 clody-infra/src/main/java/com/clody/infra/config/SchedulingConfig.java create mode 100644 clody-infra/src/main/java/com/clody/infra/models/alarm/DiaryNotifyService.java diff --git a/clody-batch/src/test/java/com/clody/clodybatch/reply/ReplyScheduledServiceTest.java b/clody-batch/src/test/java/com/clody/clodybatch/reply/ReplyScheduledServiceTest.java index e0449f1..a7f557b 100644 --- a/clody-batch/src/test/java/com/clody/clodybatch/reply/ReplyScheduledServiceTest.java +++ b/clody-batch/src/test/java/com/clody/clodybatch/reply/ReplyScheduledServiceTest.java @@ -1,51 +1,14 @@ package com.clody.clodybatch.reply; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.when; - -import com.clody.clodybatch.service.ReplyScheduleService; -import com.clody.domain.reply.ReplyType; -import com.clody.meta.Schedule; -import com.clody.meta.repository.ScheduleMetaRepository; -import java.time.LocalDateTime; -import java.time.temporal.ChronoUnit; -import java.util.Collections; -import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) public class ReplyScheduledServiceTest { - @Mock - private ScheduleMetaRepository scheduleMetaRepository; - - @InjectMocks - private ReplyScheduleService replyScheduleService; - - @Test public void 스케줄_알림_발송할_대상_탐색() { - //given - LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS); - - Long userId = 1L; - LocalDateTime yesterday = LocalDateTime.now().minusDays(1); - ReplyType replyType = ReplyType.DYNAMIC; - - Schedule schedule = Schedule.create(userId, yesterday, replyType); - - //when - when(scheduleMetaRepository.findSchedulesToNotify(now)).thenReturn( - Collections.singletonList(schedule)); - - //then - List result = replyScheduleService.findSchedulesToNotify(now); - assertEquals(1, result.size()); - assertEquals(schedule, result.getFirst()); } } diff --git a/clody-infra/src/main/java/com/clody/infra/config/SchedulingConfig.java b/clody-infra/src/main/java/com/clody/infra/config/SchedulingConfig.java new file mode 100644 index 0000000..5da75e3 --- /dev/null +++ b/clody-infra/src/main/java/com/clody/infra/config/SchedulingConfig.java @@ -0,0 +1,10 @@ +package com.clody.infra.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@EnableScheduling +public class SchedulingConfig { + +} diff --git a/clody-infra/src/main/java/com/clody/infra/models/alarm/DiaryNotifyService.java b/clody-infra/src/main/java/com/clody/infra/models/alarm/DiaryNotifyService.java new file mode 100644 index 0000000..3881703 --- /dev/null +++ b/clody-infra/src/main/java/com/clody/infra/models/alarm/DiaryNotifyService.java @@ -0,0 +1,36 @@ +package com.clody.infra.models.alarm; + +import com.clody.domain.alarm.Alarm; +import com.clody.domain.alarm.repository.AlarmRepository; +import com.clody.infra.external.fcm.FcmService; +import java.time.LocalTime; +import java.time.temporal.ChronoUnit; +import java.util.List; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Slf4j +public class DiaryNotifyService { + + private final AlarmRepository alarmRepository; + private final FcmService fcmService; + + @Scheduled(cron = "0 0/10 * * * *") + public void sendDiaryAlarms() { + LocalTime time = LocalTime.now().truncatedTo(ChronoUnit.SECONDS); + + List alarmTarget = alarmRepository.findAllByTime(time); + + alarmTarget.stream() + .filter(Alarm::isDiaryAlarm) + .forEach( + it -> fcmService.sendDiaryAlarm(it.getFcmToken()) + ); + log.info("Alarm Successfully Sent"); + } + +}