Skip to content

Commit

Permalink
Merge pull request #251 from linkarchive/feat/link-delete
Browse files Browse the repository at this point in the history
스케쥴러 : 삭제된 링크 일괄 삭제 기능
  • Loading branch information
taegyun1995 authored Aug 18, 2023
2 parents 262c1b7 + 4caa617 commit ed80d23
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@EnableJpaAuditing
@SpringBootApplication
public class LinkarchiveApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class DataConstants {
public static final int TOKEN_DATA_INDEX = 1;
public static final int MINIMUM_TITLE_LENGTH = 1;
public static final int MINIMUM_NICKNAME_LENGTH = 2;
public static final int TWO_WEEK = 2;
public static final int TOKEN_LENGTH = 2;
public static final int MINIMUM_TAG_LENGTH = 2;
public static final int S3_KEY = 3;
Expand Down
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import project.linkarchive.backend.link.domain.Link;
import project.linkarchive.backend.user.domain.User;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand All @@ -29,4 +30,7 @@ public interface LinkRepository extends JpaRepository<Link, Long> {
@Query("SELECT l FROM Link l WHERE l.linkStatus = 'ACTIVE' AND l.user = :user")
List<Link> findByUser(@Param("user") User user);

@Query("SELECT l FROM Link l WHERE l.linkStatus = 'TRASH' AND l.updatedAt < :dateTime")
List<Link> findOldLinksInTrashStatus(@Param("dateTime") LocalDateTime dateTime);

}
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);
}

}

0 comments on commit ed80d23

Please sign in to comment.