-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from linkarchive/feat/link-delete
스케쥴러 : 삭제된 링크 일괄 삭제 기능
- Loading branch information
Showing
5 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
20 changes: 20 additions & 0 deletions
20
src/main/java/project/linkarchive/backend/aspect/SchedulerExceptionAspect.java
This file contains 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,20 @@ | ||
package project.linkarchive.backend.aspect; | ||
|
||
import org.aspectj.lang.annotation.AfterThrowing; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Aspect | ||
@Component | ||
public class SchedulerExceptionAspect { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(SchedulerExceptionAspect.class); | ||
|
||
@AfterThrowing(pointcut = "execution(* project.linkarchive.backend..*.*(..)) && @annotation(org.springframework.scheduling.annotation.Scheduled)", throwing = "ex") | ||
public void handleSchedulerException(RuntimeException ex) { | ||
logger.error("스케쥴링된 작업 실행 중 오류 발생: {}", ex.getMessage(), ex); | ||
} | ||
|
||
} |
This file contains 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
52 changes: 52 additions & 0 deletions
52
src/main/java/project/linkarchive/backend/link/scheduler/LinkScheduler.java
This file contains 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,52 @@ | ||
package project.linkarchive.backend.link.scheduler; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import project.linkarchive.backend.link.domain.Link; | ||
import project.linkarchive.backend.link.repository.LinkRepository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import static project.linkarchive.backend.advice.data.DataConstants.DEFAULT_COUNT; | ||
import static project.linkarchive.backend.advice.data.DataConstants.TWO_WEEK; | ||
|
||
@Component | ||
@Transactional | ||
public class LinkScheduler { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(LinkScheduler.class); | ||
|
||
private final LinkRepository linkRepository; | ||
|
||
public LinkScheduler(LinkRepository linkRepository) { | ||
this.linkRepository = linkRepository; | ||
} | ||
|
||
@Scheduled(cron = "0 0 0 * * ?") | ||
public void deleteOldLinksFromTrash() { | ||
logger.info("Schedule: 매 자정마다 일주일 지난 삭제된 링크 일괄 삭제"); | ||
|
||
LocalDateTime twoWeekAgo = LocalDateTime.now().minusWeeks(TWO_WEEK); | ||
List<Link> trashLinks = linkRepository.findOldLinksInTrashStatus(twoWeekAgo); | ||
|
||
int deletedCount = trashLinks.size(); | ||
|
||
if (deletedCount == DEFAULT_COUNT) { | ||
logger.info("삭제될 링크가 존재하지 않습니다."); | ||
return; | ||
} | ||
|
||
trashLinks.forEach( | ||
link -> { | ||
linkRepository.deleteById(link.getId()); | ||
} | ||
); | ||
|
||
logger.info("Delete: {} 개의 삭제보관함의 링크 삭제 완료.", deletedCount); | ||
} | ||
|
||
} |