Updated scheduling package for more flexibility
This commit is contained in:
2
main.go
2
main.go
@@ -33,7 +33,7 @@ func main() {
|
|||||||
Migrate(db)
|
Migrate(db)
|
||||||
|
|
||||||
// Scheduled tasks
|
// 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
|
err := database.Db.Where("used < ?", time.Now().Add(-24*time.Hour)).Delete(&models.TotpUsage{}).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "purge failed, trying again in one hour: " + err.Error(), time.Hour
|
return "purge failed, trying again in one hour: " + err.Error(), time.Hour
|
||||||
|
|||||||
@@ -7,12 +7,18 @@ import (
|
|||||||
|
|
||||||
type Scheduled func() (string, time.Duration)
|
type Scheduled func() (string, time.Duration)
|
||||||
|
|
||||||
func Schedule(f Scheduled) {
|
func ExecuteImmediatelyAndSchedule(f Scheduled) {
|
||||||
print, wait := f()
|
print, wait := f()
|
||||||
fmt.Println(print)
|
fmt.Println(print)
|
||||||
|
|
||||||
go func(w time.Duration) {
|
go ExecuteWithDelayAndSchedule(f, wait)
|
||||||
time.Sleep(w)
|
}
|
||||||
Schedule(f)
|
|
||||||
}(wait)
|
func ExecuteWithDelayAndSchedule(f Scheduled, wait time.Duration) {
|
||||||
|
time.Sleep(wait)
|
||||||
|
|
||||||
|
print, nextWait := f()
|
||||||
|
fmt.Println(print)
|
||||||
|
|
||||||
|
go ExecuteWithDelayAndSchedule(f, nextWait)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user