Files
upstream-traggo/auth/cleanup.go
upstream-mirror cda7c1a474
Some checks failed
build / build (push) Failing after 3s
snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 05:24:45 +05:00

30 lines
784 B
Go

package auth
import (
"time"
"github.com/jinzhu/gorm"
"github.com/jmattheis/go-timemath"
"github.com/rs/zerolog/log"
"github.com/traggo/server/model"
)
var timeNow = time.Now
// CleanUp clean up expired devices
func CleanUp(db *gorm.DB, interval time.Duration, close chan bool) {
for {
select {
case <-time.After(interval):
affected := db.Where("type = ? AND active_at < ?", model.TypeLongExpiry, timemath.Second.Subtract(timeNow(), model.TypeLongExpiry.Seconds())).
Or("type = ? AND active_at < ?", model.TypeShortExpiry, timemath.Second.Subtract(timeNow(), model.TypeShortExpiry.Seconds())).
Delete(new(model.Device)).RowsAffected
if affected > 0 {
log.Debug().Int64("amount", affected).Msg("removed devices")
}
case <-close:
return
}
}
}