-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Spring Batch 기반 연관 엔티티 삭제 및 하드 딜리트 기능 추가(#64) #69
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
Merged
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions
21
src/main/java/com/ject/studytrip/cleanup/application/executor/HardDeleteExecutor.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,21 @@ | ||
| package com.ject.studytrip.cleanup.application.executor; | ||
|
|
||
| import java.util.function.LongSupplier; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class HardDeleteExecutor { | ||
|
|
||
| public long run(String phase, LongSupplier supplier) { | ||
| try { | ||
| long deleted = supplier.getAsLong(); | ||
| log.info("[HardDelete] phase={}, deleted={}", phase, deleted); | ||
| return deleted; | ||
| } catch (Exception e) { | ||
| log.error("[HardDelete] phase={} failed: {}", phase, e.getMessage(), e); | ||
| return -1L; // 실패 표식 | ||
| } | ||
| } | ||
| } | ||
174 changes: 174 additions & 0 deletions
174
src/main/java/com/ject/studytrip/cleanup/application/facade/HardDeleteFacade.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,174 @@ | ||
| package com.ject.studytrip.cleanup.application.facade; | ||
|
|
||
| import com.ject.studytrip.cleanup.application.executor.HardDeleteExecutor; | ||
| import com.ject.studytrip.member.application.service.MemberService; | ||
| import com.ject.studytrip.mission.application.service.DailyMissionService; | ||
| import com.ject.studytrip.mission.application.service.MissionService; | ||
| import com.ject.studytrip.pomodoro.application.service.PomodoroService; | ||
| import com.ject.studytrip.stamp.application.service.StampService; | ||
| import com.ject.studytrip.studylog.application.service.StudyLogDailyMissionService; | ||
| import com.ject.studytrip.studylog.application.service.StudyLogService; | ||
| import com.ject.studytrip.trip.application.service.DailyGoalService; | ||
| import com.ject.studytrip.trip.application.service.TripService; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class HardDeleteFacade { | ||
| private final PomodoroService pomodoroService; | ||
| private final StudyLogDailyMissionService studyLogDailyMissionService; | ||
| private final StudyLogService studyLogService; | ||
| private final DailyMissionService dailyMissionService; | ||
| private final MissionService missionService; | ||
| private final StampService stampService; | ||
| private final TripService tripService; | ||
| private final DailyGoalService dailyGoalService; | ||
| private final MemberService memberService; | ||
|
|
||
| private final HardDeleteExecutor executor; | ||
|
|
||
| private static final String POMODOROS_OWNED_BY_DELETED_DAILY_GOAL = | ||
| "pomodorosOwnedByDeletedDailyGoal"; | ||
| private static final String STUDY_LOG_DAILY_MISSIONS_OWNED_BY_DELETED_DAILY_MISSION = | ||
| "studyLogDailyMissionsOwnedByDeletedDailyMission"; | ||
| private static final String STUDY_LOG_DAILY_MISSIONS_OWNED_BY_DELETED_STUDY_LOG = | ||
| "studyLogDailyMissionsOwnedByDeletedStudyLog"; | ||
| private static final String STUDY_LOGS_OWNED_BY_DELETED_MEMBER = | ||
| "studyLogsOwnedByDeletedMember"; | ||
| private static final String STUDY_LOGS_OWNED_BY_DELETED_DAILY_GOAL = | ||
| "studyLogsOwnedByDeletedDailyGoal"; | ||
| private static final String DAILY_MISSIONS_OWNED_BY_DELETED_MISSION = | ||
| "dailyMissionsOwnedByDeletedMission"; | ||
| private static final String DAILY_MISSIONS_OWNED_BY_DELETED_DAILY_GOAL = | ||
| "dailyMissionsOwnedByDeletedDailyGoal"; | ||
|
|
||
| private static final String POMODOROS = "pomodoros"; | ||
| private static final String STUDY_LOG_DAILY_MISSIONS = "studyLogDailyMissions"; | ||
| private static final String STUDY_LOGS = "studyLogs"; | ||
| private static final String DAILY_MISSIONS = "dailyMissions"; | ||
| private static final String MISSIONS_OWNED_BY_DELETED_STAMP = "missionsOwnedByDeletedStamp"; | ||
| private static final String MISSIONS = "missions"; | ||
| private static final String STAMPS_OWNED_BY_DELETED_TRIP = "stampsOwnedByDeletedTrip"; | ||
| private static final String STAMPS = "stamps"; | ||
| private static final String TRIPS_OWNED_BY_DELETED_MEMBER = "tripsOwnedByDeletedMember"; | ||
| private static final String TRIPS = "trips"; | ||
| private static final String DAILY_GOALS_OWNED_BY_DELETED_TRIP = "dailyGoalsOwnedByDeletedTrip"; | ||
| private static final String DAILY_GOALS = "dailyGoals"; | ||
| private static final String MEMBERS = "members"; | ||
|
|
||
| public void hardDeleteAll() { | ||
chaiminwoo0223 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Map<String, Long> phases = new LinkedHashMap<>(); | ||
|
|
||
| deletePomodoros(phases); // 뽀모도로 삭제 | ||
| deleteStudyLogDailyMissions(phases); // StudyLogDailyMission 삭제 | ||
| deleteDailyMissions(phases); // 데일리 미션 삭제 | ||
| deleteStudyLogs(phases); // 학습 로그 삭제 | ||
| deleteDailyGoals(phases); // 데일리 목표 삭제 | ||
| deleteMissions(phases); // 미션 삭제 | ||
| deleteStamps(phases); // 스탬프 삭제 | ||
| deleteTrips(phases); // 여행 삭제 | ||
| deleteMembers(phases); // 멤버 삭제 | ||
| } | ||
|
|
||
| private void deletePomodoros(Map<String, Long> phases) { | ||
| phases.put( | ||
| POMODOROS_OWNED_BY_DELETED_DAILY_GOAL, | ||
| executor.run( | ||
| POMODOROS_OWNED_BY_DELETED_DAILY_GOAL, | ||
| pomodoroService::hardDeletePomodorosOwnedByDeletedDailyGoal)); | ||
| phases.put(POMODOROS, executor.run(POMODOROS, pomodoroService::hardDeletePomodoros)); | ||
| } | ||
|
|
||
| private void deleteStudyLogDailyMissions(Map<String, Long> phases) { | ||
| phases.put( | ||
| STUDY_LOG_DAILY_MISSIONS_OWNED_BY_DELETED_DAILY_MISSION, | ||
| executor.run( | ||
| STUDY_LOG_DAILY_MISSIONS_OWNED_BY_DELETED_DAILY_MISSION, | ||
| studyLogDailyMissionService | ||
| ::hardDeleteStudyLogDailyMissionsOwnedByDeletedDailyMission)); | ||
| phases.put( | ||
| STUDY_LOG_DAILY_MISSIONS_OWNED_BY_DELETED_STUDY_LOG, | ||
| executor.run( | ||
| STUDY_LOG_DAILY_MISSIONS_OWNED_BY_DELETED_STUDY_LOG, | ||
| studyLogDailyMissionService | ||
| ::hardDeleteStudyLogDailyMissionsOwnedByDeletedStudyLog)); | ||
| phases.put( | ||
| STUDY_LOG_DAILY_MISSIONS, | ||
| executor.run( | ||
| STUDY_LOG_DAILY_MISSIONS, | ||
| studyLogDailyMissionService::hardDeleteStudyLogDailyMissions)); | ||
| } | ||
|
|
||
| private void deleteDailyMissions(Map<String, Long> phases) { | ||
| phases.put( | ||
| DAILY_MISSIONS_OWNED_BY_DELETED_MISSION, | ||
| executor.run( | ||
| DAILY_MISSIONS_OWNED_BY_DELETED_MISSION, | ||
| dailyMissionService::hardDeleteDailyMissionsOwnedByDeletedMission)); | ||
| phases.put( | ||
| DAILY_MISSIONS_OWNED_BY_DELETED_DAILY_GOAL, | ||
| executor.run( | ||
| DAILY_MISSIONS_OWNED_BY_DELETED_DAILY_GOAL, | ||
| dailyMissionService::hardDeleteDailyMissionsOwnedByDeletedDailyGoal)); | ||
| phases.put( | ||
| DAILY_MISSIONS, | ||
| executor.run(DAILY_MISSIONS, dailyMissionService::hardDeleteDailyMissions)); | ||
| } | ||
|
|
||
| private void deleteStudyLogs(Map<String, Long> phases) { | ||
| phases.put( | ||
| STUDY_LOGS_OWNED_BY_DELETED_MEMBER, | ||
| executor.run( | ||
| STUDY_LOGS_OWNED_BY_DELETED_MEMBER, | ||
| studyLogService::hardDeleteStudyLogsOwnedByDeletedMember)); | ||
| phases.put( | ||
| STUDY_LOGS_OWNED_BY_DELETED_DAILY_GOAL, | ||
| executor.run( | ||
| STUDY_LOGS_OWNED_BY_DELETED_DAILY_GOAL, | ||
| studyLogService::hardDeleteStudyLogsOwnedByDeletedDailyGoal)); | ||
| phases.put(STUDY_LOGS, executor.run(STUDY_LOGS, studyLogService::hardDeleteStudyLogs)); | ||
| } | ||
|
|
||
| private void deleteDailyGoals(Map<String, Long> phases) { | ||
| phases.put( | ||
| DAILY_GOALS_OWNED_BY_DELETED_TRIP, | ||
| executor.run( | ||
| DAILY_GOALS_OWNED_BY_DELETED_TRIP, | ||
| dailyGoalService::hardDeleteDailyGoalsOwnedByDeletedTrip)); | ||
| phases.put(DAILY_GOALS, executor.run(DAILY_GOALS, dailyGoalService::hardDeleteDailyGoals)); | ||
| } | ||
|
|
||
| private void deleteMissions(Map<String, Long> phases) { | ||
| phases.put( | ||
| MISSIONS_OWNED_BY_DELETED_STAMP, | ||
| executor.run( | ||
| MISSIONS_OWNED_BY_DELETED_STAMP, | ||
| missionService::hardDeleteMissionsOwnedByDeletedStamp)); | ||
| phases.put(MISSIONS, executor.run(MISSIONS, missionService::hardDeleteMissions)); | ||
| } | ||
|
|
||
| private void deleteStamps(Map<String, Long> phases) { | ||
| phases.put( | ||
| STAMPS_OWNED_BY_DELETED_TRIP, | ||
| executor.run( | ||
| STAMPS_OWNED_BY_DELETED_TRIP, | ||
| stampService::hardDeleteStampsOwnedByDeletedTrip)); | ||
| phases.put(STAMPS, executor.run(STAMPS, stampService::hardDeleteStamps)); | ||
| } | ||
|
|
||
| private void deleteTrips(Map<String, Long> phases) { | ||
| phases.put( | ||
| TRIPS_OWNED_BY_DELETED_MEMBER, | ||
| executor.run( | ||
| TRIPS_OWNED_BY_DELETED_MEMBER, | ||
| tripService::hardDeleteTripsOwnedByDeletedMember)); | ||
| phases.put(TRIPS, executor.run(TRIPS, tripService::hardDeleteTrips)); | ||
| } | ||
|
|
||
| private void deleteMembers(Map<String, Long> phases) { | ||
| phases.put(MEMBERS, executor.run(MEMBERS, memberService::hardDeleteMembers)); | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
src/main/java/com/ject/studytrip/global/config/BatchJobConfig.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,49 @@ | ||
| package com.ject.studytrip.global.config; | ||
|
|
||
| import com.ject.studytrip.cleanup.application.facade.HardDeleteFacade; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.batch.core.Job; | ||
| import org.springframework.batch.core.Step; | ||
| import org.springframework.batch.core.job.builder.JobBuilder; | ||
| import org.springframework.batch.core.launch.support.RunIdIncrementer; | ||
| import org.springframework.batch.core.repository.JobRepository; | ||
| import org.springframework.batch.core.step.builder.StepBuilder; | ||
| import org.springframework.batch.core.step.tasklet.Tasklet; | ||
| import org.springframework.batch.repeat.RepeatStatus; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.transaction.PlatformTransactionManager; | ||
|
|
||
| @Configuration | ||
| @RequiredArgsConstructor | ||
| public class BatchJobConfig { | ||
| private final JobRepository jobRepository; | ||
| private final HardDeleteFacade hardDeleteFacade; | ||
| private final PlatformTransactionManager transactionManager; | ||
|
|
||
| private static final String HARD_DELETE_JOB_NAME = "hardDeleteJob"; | ||
| private static final String HARD_DELETE_STEP_NAME = "hardDeleteStep"; | ||
|
|
||
| @Bean | ||
| public Job hardDeleteJob(Step hardDeleteStep) { | ||
| return new JobBuilder(HARD_DELETE_JOB_NAME, jobRepository) | ||
| .incrementer(new RunIdIncrementer()) | ||
| .start(hardDeleteStep) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public Step hardDeleteStep(Tasklet hardDeleteTasklet) { | ||
| return new StepBuilder(HARD_DELETE_STEP_NAME, jobRepository) | ||
| .tasklet(hardDeleteTasklet, transactionManager) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public Tasklet hardDeleteTasklet() { | ||
| return (contribution, chunkContext) -> { | ||
| hardDeleteFacade.hardDeleteAll(); | ||
| return RepeatStatus.FINISHED; | ||
| }; | ||
| } | ||
| } |
27 changes: 26 additions & 1 deletion
27
src/main/java/com/ject/studytrip/global/config/SchedulerConfig.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,8 +1,33 @@ | ||
| package com.ject.studytrip.global.config; | ||
|
|
||
| import org.springframework.batch.core.Job; | ||
| import org.springframework.batch.core.JobParametersBuilder; | ||
| import org.springframework.batch.core.launch.JobLauncher; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.scheduling.annotation.EnableScheduling; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
|
|
||
| @Profile("!test") | ||
chaiminwoo0223 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @EnableScheduling | ||
| @Configuration | ||
| public class SchedulerConfig {} | ||
| public class SchedulerConfig { | ||
| private final JobLauncher jobLauncher; | ||
| private final Job hardDeleteJob; | ||
|
|
||
| public SchedulerConfig(JobLauncher jobLauncher, @Qualifier("hardDeleteJob") Job hardDeleteJob) { | ||
| this.jobLauncher = jobLauncher; | ||
| this.hardDeleteJob = hardDeleteJob; | ||
| } | ||
|
|
||
| // 매일 04:00 (Asia/Seoul) | ||
| @Scheduled(cron = "0 0 4 * * *", zone = "Asia/Seoul") | ||
| public void runDaily() throws Exception { | ||
| jobLauncher.run( | ||
| hardDeleteJob, | ||
| new JobParametersBuilder() | ||
| .addLong("ts", System.currentTimeMillis()) // 매 실행마다 유니크 파라미터 | ||
| .toJobParameters()); | ||
| } | ||
| } | ||
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
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
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.