Reschedule lottery finalization on startup

This commit is contained in:
Tim
2025-08-11 10:20:50 +08:00
parent 29e061c885
commit ff64f13765
2 changed files with 22 additions and 0 deletions

View File

@@ -42,6 +42,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledFuture;
import jakarta.annotation.PostConstruct;
@Service
public class PostService {
private final PostRepository postRepository;
@@ -100,6 +102,20 @@ public class PostService {
this.publishMode = publishMode;
}
@PostConstruct
private void rescheduleLotteries() {
LocalDateTime now = LocalDateTime.now();
for (LotteryPost lp : lotteryPostRepository.findByEndTimeAfterAndWinnersIsEmpty(now)) {
ScheduledFuture<?> future = taskScheduler.schedule(
() -> finalizeLottery(lp.getId()),
java.util.Date.from(lp.getEndTime().atZone(ZoneId.systemDefault()).toInstant()));
scheduledFinalizations.put(lp.getId(), future);
}
for (LotteryPost lp : lotteryPostRepository.findByEndTimeBeforeAndWinnersIsEmpty(now)) {
finalizeLottery(lp.getId());
}
}
public PublishMode getPublishMode() {
return publishMode;
}