Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; // 실패 표식
}
}
}
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() {
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 src/main/java/com/ject/studytrip/global/config/BatchJobConfig.java
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;
};
}
}
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")
@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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.ject.studytrip.member.presentation.dto.request.UpdateMemberRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
Expand Down Expand Up @@ -90,6 +91,11 @@ public String getRoleByMemberId(String memberId) {
return memberRole.name();
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public long hardDeleteMembers() {
return memberQueryRepository.deleteAllByDeletedAtIsNotNull();
}

private void validateMemberIsUnique(SocialProvider socialProvider, String socialId) {
boolean isMemberDuplicated =
memberRepository.existsBySocialProviderAndSocialId(socialProvider, socialId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public interface MemberQueryRepository {
MemberRole findMemberRoleById(Long memberId);

long deleteAllByDeletedAtIsNotNull();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public MemberRole findMemberRoleById(Long memberId) {
.where(member.id.eq(memberId))
.fetchOne();
}

@Override
public long deleteAllByDeletedAtIsNotNull() {
return queryFactory.delete(member).where(member.deletedAt.isNotNull()).execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -47,4 +49,19 @@ public List<DailyMission> getValidDailyMissionsByIds(
public List<DailyMission> getDailyMissionsByDailyGoal(Long dailyGoalId) {
return dailyMissionQueryRepository.findAllByDailyGoalIdFetchJoinMission(dailyGoalId);
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public long hardDeleteDailyMissions() {
return dailyMissionQueryRepository.deleteAllByDeletedAtIsNotNull();
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public long hardDeleteDailyMissionsOwnedByDeletedMission() {
return dailyMissionQueryRepository.deleteAllByDeletedMissionOwner();
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public long hardDeleteDailyMissionsOwnedByDeletedDailyGoal() {
return dailyMissionQueryRepository.deleteAllByDeletedDailyGoalOwner();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
Expand Down Expand Up @@ -95,6 +96,16 @@ public void validateAllMissionsCompletedByStampId(Long stampId) {
MissionPolicy.validateAllCompleted(exists);
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public long hardDeleteMissions() {
return missionQueryRepository.deleteAllByDeletedAtIsNotNull();
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public long hardDeleteMissionsOwnedByDeletedStamp() {
return missionQueryRepository.deleteAllByDeletedStampOwner();
}

private void validateMissionIsActiveAndBelongsToStamp(Long stampId, Mission mission) {
MissionPolicy.validateMissionBelongsToStamp(stampId, mission);
MissionPolicy.validateNotDeleted(mission);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@

public interface DailyMissionQueryRepository {
List<DailyMission> findAllByDailyGoalIdFetchJoinMission(Long dailyGoalId);

long deleteAllByDeletedAtIsNotNull();

long deleteAllByDeletedMissionOwner();

long deleteAllByDeletedDailyGoalOwner();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public interface MissionQueryRepository {
List<Mission> findAllByIdsInFetchJoinStamp(List<Long> ids);

boolean existsByStampIdAndCompletedIsFalseAndDeletedAtIsNull(Long stampId);

long deleteAllByDeletedAtIsNotNull();

long deleteAllByDeletedStampOwner();
}
Loading