refactor: centralize scheduling cleanup with row-count returns

Convert all scheduling cleanup functions to return (int, error). Remove inline cleanup calls from GetAvailableHours. Add scheduled-cleanup.go for centralized job wrappers.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-07-07 00:09:51 +01:00
co-authored by Sisyphus
parent 6064a06b7d
commit a4fa75154d
5 changed files with 1248 additions and 178 deletions
+1 -52
View File
@@ -15,13 +15,7 @@ import (
"log"
)
var londonLocation = func() *time.Location {
loc, err := time.LoadLocation("Europe/London")
if err != nil {
panic("failed to load Europe/London timezone: " + err.Error())
}
return loc
}()
var londonLocation = clock.London
// --- Types ---
type DefaultHours struct {
@@ -361,51 +355,6 @@ func GetAvailableHours(w http.ResponseWriter, r *http.Request) {
start = time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, londonLocation)
end = time.Date(end.Year(), end.Month(), end.Day(), 23, 59, 59, 999999999, londonLocation)
// Clean up old reservations (older than 1 hour)
if err := CleanupOldReservations(r.Context()); err != nil {
log.Printf("Failed to cleanup old reservations: %v", err)
}
// Anonymize stale guest accounts (6+ months after last booking)
if err := AnonymizeStaleGuestAccounts(r.Context()); err != nil {
log.Printf("Failed to anonymize stale guest accounts: %v", err)
}
// Clean up expired loyalty redemptions (pending past expires_at)
if err := CleanupExpiredLoyaltyRedemptions(r.Context()); err != nil {
log.Printf("Failed to cleanup expired loyalty redemptions: %v", err)
}
// Clean up expired financial records (aggregate + delete granular data)
if err := CleanupExpiredFinancialRecords(r.Context()); err != nil {
log.Printf("Failed to cleanup expired financial records: %v", err)
}
// Clean up bookings past deposit deadline (no deposit paid)
if err := CleanupExpiredDeposits(r.Context()); err != nil {
log.Printf("Failed to cleanup expired deposits: %v", err)
}
// Clean up expired gift cards (unused for 24+ months)
if err := CleanupExpiredGiftCards(r.Context()); err != nil {
log.Printf("Failed to cleanup expired gift cards: %v", err)
}
// Clean up idle accounts (2yr no money, 5yr with money)
if err := CleanupIdleAccounts(r.Context()); err != nil {
log.Printf("Failed to cleanup idle accounts: %v", err)
}
// Clean up old idempotency keys (24h+ and non-pending)
if err := CleanupOldIdempotencyKeys(r.Context()); err != nil {
log.Printf("Failed to cleanup old idempotency keys: %v", err)
}
// Clean up old name history (6+ months)
if err := CleanupOldNameHistory(r.Context()); err != nil {
log.Printf("Failed to cleanup old name history: %v", err)
}
// Load default hours
defaultMap := map[int]DefaultHours{}
defRows, _ := db.Conn.Query(r.Context(), `SELECT weekday, start_time::text, end_time, is_open FROM working_hours`)