|
| 1 | +package team.wego.wegobackend.group.v2.application.service; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.context.ApplicationEventPublisher; |
| 7 | +import org.springframework.stereotype.Service; |
| 8 | +import org.springframework.transaction.annotation.Propagation; |
| 9 | +import org.springframework.transaction.annotation.Transactional; |
| 10 | +import org.springframework.transaction.support.TransactionSynchronization; |
| 11 | +import org.springframework.transaction.support.TransactionSynchronizationManager; |
| 12 | +import team.wego.wegobackend.group.v2.application.event.GroupDeletedEvent; |
| 13 | +import team.wego.wegobackend.group.v2.domain.entity.GroupUserV2Status; |
| 14 | +import team.wego.wegobackend.group.v2.domain.entity.GroupV2; |
| 15 | +import team.wego.wegobackend.group.v2.domain.entity.GroupV2Status; |
| 16 | +import team.wego.wegobackend.group.v2.domain.repository.GroupImageV2Repository; |
| 17 | +import team.wego.wegobackend.group.v2.domain.repository.GroupTagV2Repository; |
| 18 | +import team.wego.wegobackend.group.v2.domain.repository.GroupUserV2Repository; |
| 19 | +import team.wego.wegobackend.group.v2.domain.repository.GroupV2Repository; |
| 20 | +import team.wego.wegobackend.image.application.service.ImageUploadService; |
| 21 | + |
| 22 | +@Slf4j |
| 23 | +@RequiredArgsConstructor |
| 24 | +@Service |
| 25 | +public class GroupV2AutoDeleteWorker { |
| 26 | + |
| 27 | + private final GroupV2Repository groupV2Repository; |
| 28 | + private final GroupUserV2Repository groupUserV2Repository; |
| 29 | + private final GroupTagV2Repository groupTagV2Repository; |
| 30 | + private final GroupImageV2Repository groupImageV2Repository; |
| 31 | + |
| 32 | + private final ImageUploadService imageUploadService; |
| 33 | + private final ApplicationEventPublisher eventPublisher; |
| 34 | + |
| 35 | + // REQUIRES_NEW를 제대로 적용하려면 다른 빈(프록시)에서 호출 |
| 36 | + @Transactional(propagation = Propagation.REQUIRES_NEW) |
| 37 | + public void deleteOneGroupAfter24h(Long groupId) { |
| 38 | + GroupV2 group = groupV2Repository.findById(groupId).orElse(null); |
| 39 | + if (group == null) { |
| 40 | + log.warn("[모임 자동삭제] 대상 모임을 찾을 수 없어 건너뜁니다. groupId={}", groupId); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (group.getDeletedAt() != null || group.getStatus() != GroupV2Status.FINISHED) { |
| 45 | + log.info("[모임 자동삭제] 현재 삭제 조건이 아니어서 건너뜁니다. groupId={} status={}", |
| 46 | + groupId, group.getStatus()); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // 삭제 전 알림을 위한 정보를 캡처 |
| 51 | + final Long hostId = group.getHost().getId(); |
| 52 | + final String hostNickName = group.getHost().getNickName(); |
| 53 | + final String groupTitle = group.getTitle(); |
| 54 | + |
| 55 | + List<Long> attendeeIds = groupUserV2Repository.findUserIdsByGroupIdAndStatus( |
| 56 | + groupId, GroupUserV2Status.ATTEND |
| 57 | + ).stream().filter(id -> !id.equals(hostId)).toList(); |
| 58 | + |
| 59 | + // 삭제 전에 S3 삭제 대상을 포착 |
| 60 | + List<String> variantUrls = groupImageV2Repository.findAllVariantUrlsByGroupId(groupId); |
| 61 | + |
| 62 | + log.info("[모임 자동삭제] 삭제 시작. groupId={} hostId={} 제목='{}' 참여자수={} S3파일수={}", |
| 63 | + groupId, hostId, groupTitle, attendeeIds.size(), |
| 64 | + (variantUrls == null ? 0 : variantUrls.size())); |
| 65 | + |
| 66 | + // DB delete (same order as deleteHard) |
| 67 | + groupUserV2Repository.deleteByGroupId(groupId); |
| 68 | + groupTagV2Repository.deleteByGroupId(groupId); |
| 69 | + groupImageV2Repository.deleteVariantsByGroupId(groupId); |
| 70 | + groupImageV2Repository.deleteImagesByGroupId(groupId); |
| 71 | + groupV2Repository.delete(group); |
| 72 | + |
| 73 | + registerAfterCommitS3Deletion(groupId, variantUrls); |
| 74 | + registerAfterCommitGroupDeletedEvent(groupId, hostId, hostNickName, groupTitle, |
| 75 | + attendeeIds); |
| 76 | + |
| 77 | + log.info("[모임 자동삭제] DB 삭제 완료 및 커밋 후 작업 등록 완료. groupId={}", groupId); |
| 78 | + } |
| 79 | + |
| 80 | + private void registerAfterCommitS3Deletion(Long groupId, List<String> variantUrls) { |
| 81 | + if (variantUrls == null || variantUrls.isEmpty()) { |
| 82 | + log.info("[모임 자동삭제][S3] 삭제할 이미지가 없습니다. groupId={}", groupId); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (!TransactionSynchronizationManager.isSynchronizationActive()) { |
| 87 | + log.warn("[모임 자동삭제][S3] 트랜잭션 동기화가 없어 즉시 삭제합니다. groupId={} url개수={}", |
| 88 | + groupId, variantUrls.size()); |
| 89 | + imageUploadService.deleteAllByUrls(variantUrls); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { |
| 94 | + @Override |
| 95 | + public void afterCommit() { |
| 96 | + try { |
| 97 | + log.info("[모임 자동삭제][S3] 커밋 완료. S3 이미지 삭제 시작. groupId={} url개수={}", |
| 98 | + groupId, variantUrls.size()); |
| 99 | + imageUploadService.deleteAllByUrls(variantUrls); |
| 100 | + log.info("[모임 자동삭제][S3] S3 이미지 삭제 완료. groupId={}", groupId); |
| 101 | + } catch (Exception e) { |
| 102 | + log.error("[모임 자동삭제][S3] S3 삭제 실패. groupId={} 원인={}", |
| 103 | + groupId, e.toString(), e); |
| 104 | + } |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + private void registerAfterCommitGroupDeletedEvent( |
| 110 | + Long groupId, |
| 111 | + Long hostId, |
| 112 | + String hostNickName, |
| 113 | + String groupTitle, |
| 114 | + List<Long> attendeeIds |
| 115 | + ) { |
| 116 | + if (attendeeIds == null || attendeeIds.isEmpty()) { |
| 117 | + log.info("[모임 자동삭제][알림] 알림 대상자가 없어 발행을 생략합니다. groupId={}", groupId); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + GroupDeletedEvent event = new GroupDeletedEvent( |
| 122 | + groupId, hostId, hostNickName, groupTitle, attendeeIds |
| 123 | + ); |
| 124 | + |
| 125 | + if (!TransactionSynchronizationManager.isSynchronizationActive()) { |
| 126 | + log.warn("[모임 자동삭제][알림] 트랜잭션 동기화가 없어 즉시 발행합니다. groupId={} 대상자수={}", |
| 127 | + groupId, attendeeIds.size()); |
| 128 | + eventPublisher.publishEvent(event); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { |
| 133 | + @Override |
| 134 | + public void afterCommit() { |
| 135 | + log.info("[모임 자동삭제][알림] 커밋 완료. 삭제 알림 이벤트 발행. groupId={} hostId={} 대상자수={}", |
| 136 | + groupId, hostId, attendeeIds.size()); |
| 137 | + eventPublisher.publishEvent(event); |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
| 141 | +} |
0 commit comments