diff --git a/src/main/java/com/ssafy/ssafsound/batch/config/PostBatchConfig.java b/src/main/java/com/ssafy/ssafsound/batch/config/PostBatchConfig.java deleted file mode 100644 index 9c65eb14c..000000000 --- a/src/main/java/com/ssafy/ssafsound/batch/config/PostBatchConfig.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.ssafy.ssafsound.batch.config; - -import com.ssafy.ssafsound.batch.tasklet.PostTasklet; -import com.ssafy.ssafsound.domain.post.service.PostConstantProvider; -import com.ssafy.ssafsound.domain.post.service.PostService; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.batch.core.Job; -import org.springframework.batch.core.Step; -import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; -import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; -import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; -import org.springframework.batch.core.step.tasklet.Tasklet; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Slf4j -@Configuration -@EnableBatchProcessing -@RequiredArgsConstructor -public class PostBatchConfig { - private final JobBuilderFactory jobBuilderFactory; - private final StepBuilderFactory stepBuilderFactory; - private final PostService postService; - private final PostConstantProvider postConstantProvider; - - - @Bean - public Job deleteHotPostJob() { - return jobBuilderFactory - .get("deleteHotPostJob") - .start(deleteHotPostStep()) - .build(); - } - - @Bean - public Step deleteHotPostStep() { - return stepBuilderFactory.get("deleteHotPostJob") - .tasklet(deleteHotPostTasklet()) - .build(); - } - - @Bean - public Tasklet deleteHotPostTasklet() { - return new PostTasklet(postService, postConstantProvider); - } -} diff --git a/src/main/java/com/ssafy/ssafsound/batch/scheduler/PostScheduler.java b/src/main/java/com/ssafy/ssafsound/batch/scheduler/PostScheduler.java deleted file mode 100644 index 42b798a3e..000000000 --- a/src/main/java/com/ssafy/ssafsound/batch/scheduler/PostScheduler.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.ssafy.ssafsound.batch.scheduler; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.batch.core.*; -import org.springframework.batch.core.launch.JobLauncher; -import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; -import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; -import org.springframework.batch.core.repository.JobRestartException; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -@Slf4j -@EnableScheduling -@RequiredArgsConstructor -@Component -public class PostScheduler { - private final Job deleteHotPostJob; - private final JobLauncher jobLauncher; - - - /** - * 매일 오전 4시에 실행 - */ - @Scheduled(cron = "0 0 4 * * *") - public void deleteHotPostScheduler() { - try { - Map jobParametersMap = new HashMap<>(); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - jobParametersMap.put("date", new JobParameter(dateFormat.format(new Date()))); - JobParameters parameters = new JobParameters(jobParametersMap); - - jobLauncher.run(deleteHotPostJob, parameters); - - } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | - JobParametersInvalidException e) { - e.printStackTrace(); - } - } - -} diff --git a/src/main/java/com/ssafy/ssafsound/batch/tasklet/PostTasklet.java b/src/main/java/com/ssafy/ssafsound/batch/tasklet/PostTasklet.java deleted file mode 100644 index 77d2bf103..000000000 --- a/src/main/java/com/ssafy/ssafsound/batch/tasklet/PostTasklet.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.ssafy.ssafsound.batch.tasklet; - -import com.ssafy.ssafsound.domain.post.service.PostConstantProvider; -import com.ssafy.ssafsound.domain.post.service.PostService; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.batch.core.ExitStatus; -import org.springframework.batch.core.StepContribution; -import org.springframework.batch.core.StepExecution; -import org.springframework.batch.core.StepExecutionListener; -import org.springframework.batch.core.scope.context.ChunkContext; -import org.springframework.batch.core.step.tasklet.Tasklet; -import org.springframework.batch.repeat.RepeatStatus; - -@Slf4j -@RequiredArgsConstructor -public class PostTasklet implements Tasklet, StepExecutionListener { - private final PostService postService; - private final PostConstantProvider postConstantProvider; - - @Override - public void beforeStep(StepExecution stepExecution) { - } - - @Override - public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) { - postService.deleteHotPostsUnderThreshold(postConstantProvider.getHOT_POST_LIKES_THRESHOLD()); - return RepeatStatus.FINISHED; - } - - @Override - public ExitStatus afterStep(StepExecution stepExecution) { - return ExitStatus.COMPLETED; - } - -} diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostCustomRepository.java b/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostCustomRepository.java index e70336c03..f54b6aa8f 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostCustomRepository.java +++ b/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostCustomRepository.java @@ -8,4 +8,8 @@ public interface HotPostCustomRepository { List findHotPosts(Long cursor, int size); List findHotPostsByKeyword(String keyword, Long cursor, int size); + + void deleteHotPostsUnderThreshold(Long threshold); + + } diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostCustomRepositoryImpl.java b/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostCustomRepositoryImpl.java index c75025811..64df6c446 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostCustomRepositoryImpl.java +++ b/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostCustomRepositoryImpl.java @@ -12,10 +12,11 @@ import java.util.List; import java.util.stream.Collectors; -import static com.ssafy.ssafsound.domain.post.domain.QHotPost.hotPost; -import static com.ssafy.ssafsound.domain.post.domain.QPost.post; import static com.ssafy.ssafsound.domain.board.domain.QBoard.board; import static com.ssafy.ssafsound.domain.member.domain.QMember.member; +import static com.ssafy.ssafsound.domain.post.domain.QHotPost.hotPost; +import static com.ssafy.ssafsound.domain.post.domain.QPost.post; +import static com.ssafy.ssafsound.domain.post.domain.QPostLike.postLike; @Repository @@ -58,6 +59,21 @@ public List findHotPostsByKeyword(String keyword, Long cursor, int size .collect(Collectors.toList()); } + @Override + public void deleteHotPostsUnderThreshold(Long threshold) { + jpaQueryFactory.delete(hotPost) + .where(hotPost.in( + jpaQueryFactory.select(hotPost) + .from(hotPost) + .leftJoin(postLike) + .on(hotPost.post.eq(postLike.post)) + .groupBy(hotPost.id) + .having(hotPost.id.count().lt(threshold)) + .fetch()) + ) + .execute(); + } + private BooleanExpression postIdLtCursor(Long cursor) { return cursor != -1 ? post.id.lt(cursor) : null; } diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostRepository.java b/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostRepository.java index c9794de51..f1719090d 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostRepository.java +++ b/src/main/java/com/ssafy/ssafsound/domain/post/repository/HotPostRepository.java @@ -15,16 +15,6 @@ @Repository public interface HotPostRepository extends JpaRepository, HotPostCustomRepository { - @Modifying - @Query("DELETE FROM hot_post h " + - "WHERE h.id IN ( " + - " SELECT h.id " + - " FROM hot_post h LEFT JOIN post_like p " + - " ON h.post.id = p.post.id " + - " GROUP BY h.id " + - " HAVING COUNT(p.id) < :threshold " + - ")") - void deleteHotPostsUnderThreshold(@Param("threshold") Long threshold); Optional findByPostId(Long postId); diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/scheduler/PostScheduler.java b/src/main/java/com/ssafy/ssafsound/domain/post/scheduler/PostScheduler.java new file mode 100644 index 000000000..713de80a9 --- /dev/null +++ b/src/main/java/com/ssafy/ssafsound/domain/post/scheduler/PostScheduler.java @@ -0,0 +1,21 @@ +package com.ssafy.ssafsound.domain.post.scheduler; + +import com.ssafy.ssafsound.domain.post.service.PostConstantProvider; +import com.ssafy.ssafsound.domain.post.service.PostService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@RequiredArgsConstructor +@Slf4j +@Component +public class PostScheduler { + private final PostService postService; + private final PostConstantProvider postConstantProvider; + + @Scheduled(cron = "0 0 4 * * *") + private void deleteHotPostsUnderThreshold() { + postService.deleteHotPostsUnderThreshold(postConstantProvider.getHOT_POST_LIKES_THRESHOLD()); + } +} diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/service/PostService.java b/src/main/java/com/ssafy/ssafsound/domain/post/service/PostService.java index 58ebedc51..bdc49dca2 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/post/service/PostService.java +++ b/src/main/java/com/ssafy/ssafsound/domain/post/service/PostService.java @@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;