Refactored project structure
This commit is contained in:
46
backend/handlers/daily_cleanup.go
Normal file
46
backend/handlers/daily_cleanup.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func StartDailyCleanup(app *App) {
|
||||
loc, _ := time.LoadLocation("Europe/Budapest")
|
||||
|
||||
go func() {
|
||||
for {
|
||||
// read finalize_time from DB (default "10:30")
|
||||
t := app.getSetting("finalize_time", "10:30")
|
||||
|
||||
// parse "HH:MM"
|
||||
parts := strings.Split(t, ":")
|
||||
hour, _ := strconv.Atoi(parts[0])
|
||||
minute, _ := strconv.Atoi(parts[1])
|
||||
|
||||
now := time.Now().In(loc)
|
||||
next := time.Date(now.Year(), now.Month(), now.Day(), hour, minute, 0, 0, loc)
|
||||
if !next.After(now) {
|
||||
next = next.Add(24 * time.Hour)
|
||||
}
|
||||
|
||||
slog.Info("Next finalize scheduled", "time", next)
|
||||
|
||||
timer := time.NewTimer(time.Until(next))
|
||||
|
||||
select {
|
||||
case <-timer.C:
|
||||
if err := finalizeOrders(app); err != nil {
|
||||
slog.Error("Daily finalize failed", "error", err)
|
||||
} else {
|
||||
slog.Info("Orders finalized", "time", time.Now().In(loc))
|
||||
}
|
||||
case <-app.FinalizeUpdate:
|
||||
slog.Info("Finalize time updated, recalculating...")
|
||||
timer.Stop()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user