Drop startup credit-reset catch-up (bulk per-user loop; lazy reset covers it)

This commit is contained in:
Anthony Stirling
2026-06-11 21:21:28 +01:00
parent 3a4b340313
commit 47e5977a31
@@ -2,12 +2,8 @@ package stirling.software.saas.service;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@@ -47,47 +43,11 @@ public class CreditResetScheduler {
}
}
/** Check for missed resets on application startup */
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
try {
ZoneId configuredZone = ZoneId.of(creditsProperties.getReset().getZone());
LocalDateTime now = LocalDateTime.now(configuredZone);
LocalDateTime lastScheduledReset = getMostRecentScheduledReset(now, configuredZone);
log.info(
"Checking for missed cycle credit resets. Last scheduled: {}, Current: {}",
lastScheduledReset,
now);
creditService.resetCycleCreditsForAllUsers(lastScheduledReset);
creditService.resetCycleCreditsForAllTeams(lastScheduledReset);
log.info("Catch-up cycle credit reset completed");
} catch (Exception e) {
log.error("Error during catch-up credit reset", e);
}
}
/** Get the most recent scheduled reset time based on configured schedule and zone */
private LocalDateTime getMostRecentScheduledReset(LocalDateTime now, ZoneId configuredZone) {
ZonedDateTime zonedNow = now.atZone(configuredZone);
// Find the 1st of the current month at the configured time (default 02:00)
ZonedDateTime firstOfMonth =
zonedNow.with(TemporalAdjusters.firstDayOfMonth())
.withHour(2)
.withMinute(0)
.withSecond(0)
.withNano(0);
// If it's the 1st and before the reset hour, or if current time is before the 1st at 2 AM,
// go to previous month's 1st
if (zonedNow.isBefore(firstOfMonth)) {
firstOfMonth = firstOfMonth.minusMonths(1);
}
return firstOfMonth.toLocalDateTime();
}
// NOTE: The startup catch-up reset (formerly @EventListener(ApplicationReadyEvent)) was
// removed. It bulk-looped every user on each boot (per-row save), hammering the DB and
// stalling boot on large user tables. Per-user cycle resets already happen lazily in
// CreditService.getOrCreateUserCredits (isCycleResetDue), and the monthly cron above still
// performs the scheduled reset.
/**
* Cleanup and maintenance task; runs daily at 3 AM UTC. Performs maintenance tasks like