From 8a011e5f5588a280cc5fec1227f90156d72afc07 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Mon, 21 Oct 2024 19:01:37 +0900 Subject: [PATCH] =?UTF-8?q?#76=20:=20[feat]=20:=20=ED=8A=B9=EC=A0=95=20?= =?UTF-8?q?=EC=8B=9C=EA=B8=B0=EB=A7=88=EB=8B=A4=2030=EC=9D=BC=EC=9D=B4=20?= =?UTF-8?q?=EC=A7=80=EB=82=9C=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=EB=93=A4?= =?UTF-8?q?=EC=9D=84=20=EC=A0=9C=EA=B1=B0=ED=95=A0=20=EC=88=98=20=EC=9E=88?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../onetime/repository/EventRepository.java | 3 ++ .../service/EventCleanupScheduler.java | 28 +++++++++++++++++++ src/main/resources/application.yaml | 5 +++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/main/java/side/onetime/service/EventCleanupScheduler.java diff --git a/src/main/java/side/onetime/repository/EventRepository.java b/src/main/java/side/onetime/repository/EventRepository.java index f85dafa..627c915 100644 --- a/src/main/java/side/onetime/repository/EventRepository.java +++ b/src/main/java/side/onetime/repository/EventRepository.java @@ -4,10 +4,13 @@ import side.onetime.domain.Event; import side.onetime.repository.custom.EventRepositoryCustom; +import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; import java.util.UUID; public interface EventRepository extends JpaRepository, EventRepositoryCustom { Optional findByEventId(UUID eventId); boolean existsByEventId(UUID eventId); + List findByCreatedDateBefore(LocalDateTime twoWeeksAgo); } \ No newline at end of file diff --git a/src/main/java/side/onetime/service/EventCleanupScheduler.java b/src/main/java/side/onetime/service/EventCleanupScheduler.java new file mode 100644 index 0000000..5dd8d27 --- /dev/null +++ b/src/main/java/side/onetime/service/EventCleanupScheduler.java @@ -0,0 +1,28 @@ +package side.onetime.service; + +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import side.onetime.domain.Event; +import side.onetime.repository.EventRepository; + +import java.time.LocalDateTime; +import java.util.List; + +@Component +@RequiredArgsConstructor +public class EventCleanupScheduler { + + private final EventRepository eventRepository; + + @Scheduled(cron = "${scheduling.cron}") + @Transactional + public void deleteOldEvents() { + LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30); + + // 30일 이상 지난 이벤트를 찾은 후 삭제 + List oldEvents = eventRepository.findByCreatedDateBefore(thirtyDaysAgo); + oldEvents.forEach(eventRepository::deleteEvent); + } +} \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 11c2ce1..676a962 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -86,4 +86,7 @@ jwt: refresh-token: expiration-time: ${REFRESH_TOKEN_EXPIRATION_TIME} register-token: - expiration-time: ${REGISTER_TOKEN_EXPIRATION_TIME} \ No newline at end of file + expiration-time: ${REGISTER_TOKEN_EXPIRATION_TIME} + +scheduling: + cron: ${CRON} \ No newline at end of file