Updated scheduling package for more flexibility

This commit is contained in:
🐙PiperYxzzy
2022-05-29 20:10:03 +02:00
parent 10a28f4e89
commit 2d65f565ec
2 changed files with 12 additions and 6 deletions

View File

@@ -33,7 +33,7 @@ func main() {
Migrate(db)
// Scheduled tasks
scheduled.Schedule(func() (string, time.Duration) {
go scheduled.ExecuteImmediatelyAndSchedule(func() (string, time.Duration) {
err := database.Db.Where("used < ?", time.Now().Add(-24*time.Hour)).Delete(&models.TotpUsage{}).Error
if err != nil {
return "purge failed, trying again in one hour: " + err.Error(), time.Hour

View File

@@ -7,12 +7,18 @@ import (
type Scheduled func() (string, time.Duration)
func Schedule(f Scheduled) {
func ExecuteImmediatelyAndSchedule(f Scheduled) {
print, wait := f()
fmt.Println(print)
go func(w time.Duration) {
time.Sleep(w)
Schedule(f)
}(wait)
go ExecuteWithDelayAndSchedule(f, wait)
}
func ExecuteWithDelayAndSchedule(f Scheduled, wait time.Duration) {
time.Sleep(wait)
print, nextWait := f()
fmt.Println(print)
go ExecuteWithDelayAndSchedule(f, nextWait)
}