Skip to content

Commit

Permalink
Feat(infra) : 10분단위 스케줄링 구현
Browse files Browse the repository at this point in the history
Spring Scheduler + Cron tab을 이용하여 구현하였습니다.
  • Loading branch information
hyunw9 committed Sep 3, 2024
1 parent a38e890 commit 9f5b7c8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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<Schedule> result = replyScheduleService.findSchedulesToNotify(now);
assertEquals(1, result.size());
assertEquals(schedule, result.getFirst());
}
}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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<Alarm> alarmTarget = alarmRepository.findAllByTime(time);

alarmTarget.stream()
.filter(Alarm::isDiaryAlarm)
.forEach(
it -> fcmService.sendDiaryAlarm(it.getFcmToken())
);
log.info("Alarm Successfully Sent");
}

}

0 comments on commit 9f5b7c8

Please sign in to comment.