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