-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#223 알림 내역 삭제 스케줄러 & 회원탈퇴 시 알림 삭제 처리 #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
61cca0e
:sparkles: feat: 회원 탈퇴 시 관련된 알림 객체 자동 삭제를 위한 OnDelete 어노테이션 추가
ojy0903 316cc37
:sparkles: feat: 오래된 알림 삭제 스케줄러를 위한 Repository 메서드 추가
ojy0903 8c20199
:sparkles: feat: 오래된 알림 삭제 배치 Executor 추가
ojy0903 212c7fa
:sparkles: feat: 오래된 알림 삭제 스케줄러 추가
ojy0903 dba8e2b
Merge remote-tracking branch 'origin/develop' into feat/#223
ojy0903 47af00c
:art: style: 오래된 알림 내역 삭제 스케줄러 주석 추가
ojy0903 5478569
:recycle: refactor: 오래된 알림 내역 삭제 스케줄러 내부 삭제 날짜 기준 조정
ojy0903 e865a77
:recycle: refactor: 오래된 알림 내역 삭제 스케줄러 밤 12시 실행으로 수정 + ZoneId 명시
ojy0903 9b6206d
:recycle: refactor: Notification 엔티티에 인덱스 적용 & Repository 조회 정렬 수정
ojy0903 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...WhereYouAd/domains/notification/domain/service/scheduler/NotificationCleanupExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.whereyouad.WhereYouAd.domains.notification.domain.service.scheduler; | ||
|
|
||
| import com.whereyouad.WhereYouAd.domains.notification.persistence.repository.NotificationDeliveryRepository; | ||
| import com.whereyouad.WhereYouAd.domains.notification.persistence.repository.NotificationRepository; | ||
| import com.whereyouad.WhereYouAd.domains.notification.persistence.repository.UserNotificationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class NotificationCleanupExecutor { | ||
|
|
||
| private final NotificationRepository notificationRepository; | ||
| private final UserNotificationRepository userNotificationRepository; | ||
| private final NotificationDeliveryRepository notificationDeliveryRepository; | ||
|
|
||
| @Transactional | ||
| public void deleteNotificationChunk(List<Long> notificationIds) { | ||
| int userNotificationCount = userNotificationRepository.deleteByNotificationIdIn(notificationIds); | ||
| int deliveryCount = notificationDeliveryRepository.deleteByNotificationIdIn(notificationIds); | ||
| notificationRepository.deleteAllByIdInBatch(notificationIds); | ||
|
|
||
| log.debug("만료 알림 청크 삭제 - 알림(Notification): {}건, 회원 알림 내역(UserNotification): {}건, 발송 이력(NotificationDelivery): {}건", | ||
| notificationIds.size(), userNotificationCount, deliveryCount); | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
...hereYouAd/domains/notification/domain/service/scheduler/NotificationCleanupScheduler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.whereyouad.WhereYouAd.domains.notification.domain.service.scheduler; | ||
|
|
||
| import com.whereyouad.WhereYouAd.domains.notification.persistence.repository.NotificationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.ZoneId; | ||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class NotificationCleanupScheduler { | ||
|
|
||
| private static final int RETENTION_DAYS = 30; | ||
| private static final int CHUNK_SIZE = 1000; | ||
|
|
||
| private final NotificationRepository notificationRepository; | ||
| private final NotificationCleanupExecutor notificationCleanupExecutor; | ||
|
|
||
| @Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul") | ||
| public void deleteOldNotifications() { | ||
| // 스케줄러 실행 시점으로 부터 만료 기준일 (30일 과거) 설정 | ||
| LocalDateTime threshold = LocalDate.now(ZoneId.of("Asia/Seoul")).minusDays(RETENTION_DAYS).atStartOfDay(); | ||
| log.info("오래된 알림 내역 삭제 스케줄러 실행 - threshold: {}", threshold); | ||
|
|
||
| int totalDeleted = 0; | ||
| while (true) { | ||
| // 삭제 대상인 알림 내역 (30일 이상 지난 내역) Id 를 List 조회 | ||
| List<Long> notificationIds = | ||
| notificationRepository.findIdsByCreatedAtBefore(threshold, PageRequest.of(0, CHUNK_SIZE)); | ||
|
|
||
| // 삭제 대상인 알림 내역이 없으면 종료 | ||
| if (notificationIds.isEmpty()) { | ||
| break; | ||
| } | ||
|
|
||
| // 청크 단위 알림 내역 삭제 | ||
| notificationCleanupExecutor.deleteNotificationChunk(notificationIds); | ||
| totalDeleted += notificationIds.size(); | ||
| } | ||
|
|
||
| log.info("오래된 알림 내역 삭제 완료 - 삭제 알림 갯수: {}", totalDeleted); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...eyouad/WhereYouAd/domains/notification/persistence/repository/NotificationRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| package com.whereyouad.WhereYouAd.domains.notification.persistence.repository; | ||
|
|
||
| import com.whereyouad.WhereYouAd.domains.notification.persistence.entity.Notification; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| public interface NotificationRepository extends JpaRepository<Notification, Long> { | ||
|
|
||
| // 보관 기간이 지난 알림 id 조회 (청크 단위로 끊어서 조회) | ||
| @Query("SELECT n.id FROM Notification n WHERE n.createdAt < :threshold ORDER BY n.createdAt, n.id") | ||
| List<Long> findIdsByCreatedAtBefore(@Param("threshold") LocalDateTime threshold, Pageable pageable); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.