Skip to content

Commit

Permalink
Merge pull request #217 from Link-MIND/feature/#213
Browse files Browse the repository at this point in the history
[Fix] ํƒ€์ด๋จธ ์Šค์ผ€์ค„ ๋ถ€ํ•˜ ํ•ด๊ฒฐ
  • Loading branch information
mmihye committed Jan 20, 2024
2 parents b641452 + 06daefa commit 20b9362
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
package com.app.toaster.external.client.fcm;

import com.app.toaster.domain.Category;
import com.app.toaster.domain.Reminder;
import com.app.toaster.exception.Error;
import com.app.toaster.exception.model.NotFoundException;
import com.app.toaster.external.client.sqs.SqsProducer;
import com.app.toaster.infrastructure.TimerRepository;
import com.app.toaster.infrastructure.ToastRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PessimisticLockException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.PessimisticLockingFailureException;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Component
Expand All @@ -22,24 +43,115 @@ public class FCMScheduler {

private final ObjectMapper objectMapper; // FCM์˜ body ํ˜•ํƒœ์— ๋”ฐ๋ผ ์ƒ์„ฑํ•œ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ Mapper ํด๋ž˜์Šค

@Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Seoul")
private final ToastRepository toastRepository;

private final PlatformTransactionManager transactionManager;
private final SqsProducer sqsProducer;

@PersistenceContext
private EntityManager em;


private final int PUSH_MESSAGE_NUMBER = 5;
@Scheduled(cron = "1 * * * * *", zone = "Asia/Seoul")
public String pushTodayTimer() {

log.info("๋ฆฌ๋งˆ์ธ๋“œ ์•Œ๋žŒ");

// ์˜ค๋Š˜ ์š”์ผ
int today = LocalDateTime.now().getDayOfWeek().getValue();
TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);

try {

// ์˜ค๋Š˜ ์š”์ผ
int today = LocalDateTime.now().getDayOfWeek().getValue();


timerRepository.findAll().stream().filter(reminder -> reminder.getRemindDates().contains(today)).forEach(timer -> {

LocalTime now = LocalTime.now();

//ํ•œ๊ตญ ์‹œ๊ฐ„๋Œ€๋กœ ๋ณ€ํ™˜
ZoneId koreaTimeZone = ZoneId.of("Asia/Seoul");
ZonedDateTime koreaTime = now.atDate(ZonedDateTime.now().toLocalDate()).atZone(koreaTimeZone);

//ZonedDateTime์—์„œ LocalTime ์ถ”์ถœ
LocalTime koreaLocalTime = koreaTime.toLocalTime();

// ํ˜„์žฌ ์•Œ๋žŒ์ด ์ปค์ ธ์žˆ๊ณ  ์„ค์ •๊ฐ’์ด ๋™์ผํ•˜๋ฉด ์•Œ๋žŒ ์ „์†ก
if(timer.getIsAlarm() && timer.getUser().getFcmIsAllowed() && timer.getRemindTime().getHour()== koreaLocalTime.getHour()
&&timer.getRemindTime().getMinute()== koreaLocalTime.getMinute()) {
System.out.println("=========================================");
System.out.println("timer.getRemindTime().equals(koreaTime)");
System.out.println("================= ์ „์†ก์‹œ๊ฐ„ =================");
//sqs ํ‘ธ์‹œ
FCMPushRequestDto request = getPushMessage(timer,toastRepository.getUnReadToastNumber(timer.getUser().getUserId()) );

timerRepository.findAll().stream().filter(reminder -> reminder.getRemindDates().contains(today)).forEach(timer -> {
System.out.println("timerId : " + timer.getId());
String cronExpression = String.format("0 %s %s * * ?", timer.getRemindTime().getMinute(),timer.getRemindTime().getHour());
sqsProducer.sendMessage(request, timer.getId().toString());

fcmService.schedulePushAlarm(cronExpression, timer.getId());
System.out.println("========="+request.getTitle() + request.getBody()+"=========");

});
}

});


} catch (PessimisticLockingFailureException | PessimisticLockException e) {
transactionManager.rollback(transactionStatus);
} finally {
em.close();
}

return "์˜ค๋Š˜์˜ ํ† ์Šคํ„ฐ๋ฅผ ๊ตฌ์›Œ ์ „๋‹ฌํ–ˆ์–ด์š”!!!";
}


private FCMPushRequestDto getPushMessage(Reminder reminder, int unReadToastNumber){
Random random = new Random();
int randomNumber = random.nextInt(PUSH_MESSAGE_NUMBER);
String categoryTitle = "์ „์ฒด";

Category category = timerRepository.findCategoryByReminderId(reminder.getId());
if(category != null){
categoryTitle = category.getTitle();
}


String title="";
String body="";

switch (randomNumber) {
case 0 -> {
title = reminder.getUser().getNickname()+PushMessage.ALARM_MESSAGE_0.getTitle();
body = categoryTitle+PushMessage.ALARM_MESSAGE_0.getBody();
}
case 1 -> {
title = "๋ต๋™! " + categoryTitle+PushMessage.ALARM_MESSAGE_1.getTitle();
body = PushMessage.ALARM_MESSAGE_1.getBody();
}
case 2 -> {
title = reminder.getUser().getNickname()+"๋‹˜, "+categoryTitle+PushMessage.ALARM_MESSAGE_2.getTitle();
body = PushMessage.ALARM_MESSAGE_2.getBody();
}
case 3 -> {
LocalDateTime now = LocalDateTime.now();

title = now.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.KOREA)+"์š”์ผ "+now.getHour()+"์‹œ์—๋Š” "
+categoryTitle+PushMessage.ALARM_MESSAGE_3.getTitle();
body = PushMessage.ALARM_MESSAGE_3.getBody();
}
case 4 -> {
title = reminder.getUser().getNickname()+"๋‹˜, " +categoryTitle+PushMessage.ALARM_MESSAGE_4.getTitle();
body = PushMessage.ALARM_MESSAGE_4.getBody()+unReadToastNumber+"๊ฐœ ๋‚จ์•„์žˆ์–ด์š”";
}
};

return FCMPushRequestDto.builder()
.targetToken(reminder.getUser().getFcmToken())
.title(title)
.body(body)
.build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -154,10 +157,18 @@ public void schedulePushAlarm(String cronExpression,Long timerId) {
()-> new NotFoundException(Error.NOT_FOUND_TIMER, Error.NOT_FOUND_TIMER.getMessage())
);

String cron = String.format("0 %s %s * * ?", timer.getRemindTime().getMinute(),timer.getRemindTime().getHour());
LocalTime now = LocalTime.now();

// ํ•œ๊ตญ ์‹œ๊ฐ„๋Œ€๋กœ ๋ณ€ํ™˜
//ZoneId koreaTimeZone = ZoneId.of("Asia/Seoul");
//ZonedDateTime koreaTime = localTime.atDate(ZonedDateTime.now().toLocalDate()).atZone(koreaTimeZone);

// ZonedDateTime์—์„œ LocalTime ์ถ”์ถœ
//LocalTime koreaLocalTime = koreaTime.toLocalTime();


// ํ˜„์žฌ ์•Œ๋žŒ์ด ์ปค์ ธ์žˆ๊ณ  ์„ค์ •๊ฐ’์ด ๋™์ผํ•˜๋ฉด ์•Œ๋žŒ ์ „์†ก
if(timer.getIsAlarm() && timer.getUser().getFcmIsAllowed() && cronExpression.equals(cron)) {
if(timer.getIsAlarm() && timer.getUser().getFcmIsAllowed() && timer.getRemindTime().equals(now)) {
System.out.println("================= ์ „์†ก์‹œ๊ฐ„ =================");
//sqs ํ‘ธ์‹œ
FCMPushRequestDto request = getPushMessage(timer,toastRepository.getUnReadToastNumber(timer.getUser().getUserId()) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public void createTimer(Long userId, CreateTimerRequestDto createTimerRequestDto
timerRepository.save(reminder);

// ๋ฐ”๋€ ํƒ€์ด๋จธ๊ฐ€ ์˜ค๋Š˜ ์ดํ›„ ์„ค์ •๋˜์–ด์žˆ์œผ๋ฉด ์ƒˆ๋กœ์šด schedule ์ถ”๊ฐ€
if (createTimerRequestDto.remindDates().contains(LocalDateTime.now().getDayOfWeek().getValue()))
if(LocalTime.parse(createTimerRequestDto.remindTime()).isAfter(LocalTime.now())){
String cronExpression = String.format("0 %s %s * * ?", LocalTime.parse(createTimerRequestDto.remindTime()).getMinute(),LocalTime.parse(createTimerRequestDto.remindTime()).getHour());

fcmService.schedulePushAlarm(cronExpression, reminder.getId());
}
// if (createTimerRequestDto.remindDates().contains(LocalDateTime.now().getDayOfWeek().getValue()))
// if(LocalTime.parse(createTimerRequestDto.remindTime()).isAfter(LocalTime.now())){
// String cronExpression = String.format("0 %s %s * * ?", LocalTime.parse(createTimerRequestDto.remindTime()).getMinute(),LocalTime.parse(createTimerRequestDto.remindTime()).getHour());
//
// fcmService.schedulePushAlarm(cronExpression, reminder.getId());
// }
}
@Transactional(readOnly = true)
public GetTimerResponseDto getTimer(Long userId, Long timerId){
Expand Down Expand Up @@ -118,12 +118,12 @@ public void updateTimerDatetime(Long userId, Long timerId, UpdateTimerDateTimeDt
LocalDateTime now = LocalDateTime.now();

// ๋ฐ”๋€ ํƒ€์ด๋จธ๊ฐ€ ์˜ค๋Š˜ ์ดํ›„ ์„ค์ •๋˜์–ด์žˆ์œผ๋ฉด ์ƒˆ๋กœ์šด schedule ์ถ”๊ฐ€
if (updateTimerDateTimeDto.remindDates().contains(now.getDayOfWeek().getValue()))
if(LocalTime.parse(updateTimerDateTimeDto.remindTime()).isAfter(LocalTime.now())){
String cronExpression = String.format("0 %s %s * * ?", LocalTime.parse(updateTimerDateTimeDto.remindTime()).getMinute(),LocalTime.parse(updateTimerDateTimeDto.remindTime()).getHour());

fcmService.schedulePushAlarm(cronExpression, reminder.getId());
}
// if (updateTimerDateTimeDto.remindDates().contains(now.getDayOfWeek().getValue()))
// if(LocalTime.parse(updateTimerDateTimeDto.remindTime()).isAfter(LocalTime.now())){
// String cronExpression = String.format("0 %s %s * * ?", LocalTime.parse(updateTimerDateTimeDto.remindTime()).getMinute(),LocalTime.parse(updateTimerDateTimeDto.remindTime()).getHour());
//
// fcmService.schedulePushAlarm(cronExpression, reminder.getId());
// }


}
Expand Down

0 comments on commit 20b9362

Please sign in to comment.