From a774c9cc97541513078519ec009f2bb0d76095d0 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Mon, 11 Aug 2025 10:05:37 +0800 Subject: [PATCH] Cancel lottery schedule on post deletion --- .../java/com/openisle/service/PostService.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/com/openisle/service/PostService.java b/backend/src/main/java/com/openisle/service/PostService.java index a694e06c4..b5aa65a80 100644 --- a/backend/src/main/java/com/openisle/service/PostService.java +++ b/backend/src/main/java/com/openisle/service/PostService.java @@ -38,6 +38,9 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ScheduledFuture; @Service public class PostService { @@ -58,6 +61,7 @@ public class PostService { private final ImageUploader imageUploader; private final TaskScheduler taskScheduler; private final EmailSender emailSender; + private final ConcurrentMap> scheduledFinalizations = new ConcurrentHashMap<>(); @org.springframework.beans.factory.annotation.Autowired public PostService(PostRepository postRepository, @@ -186,8 +190,10 @@ public class PostService { notificationService.notifyMentions(content, author, post, null); if (post instanceof LotteryPost lp && lp.getEndTime() != null) { - taskScheduler.schedule(() -> finalizeLottery(lp.getId()), + ScheduledFuture future = taskScheduler.schedule( + () -> finalizeLottery(lp.getId()), java.util.Date.from(lp.getEndTime().atZone(ZoneId.systemDefault()).toInstant())); + scheduledFinalizations.put(lp.getId(), future); } return post; } @@ -202,6 +208,7 @@ public class PostService { } private void finalizeLottery(Long postId) { + scheduledFinalizations.remove(postId); lotteryPostRepository.findById(postId).ifPresent(lp -> { List participants = new ArrayList<>(lp.getParticipants()); if (participants.isEmpty()) { @@ -506,6 +513,12 @@ public class PostService { notificationRepository.deleteAll(notificationRepository.findByPost(post)); postReadService.deleteByPost(post); imageUploader.removeReferences(imageUploader.extractUrls(post.getContent())); + if (post instanceof LotteryPost lp) { + ScheduledFuture future = scheduledFinalizations.remove(lp.getId()); + if (future != null) { + future.cancel(false); + } + } postRepository.delete(post); }