Added ability to run & schedule tasks on a period
This commit is contained in:
11
main.go
11
main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/yxzzy-wtf/gin-gonic-prepack/config"
|
"github.com/yxzzy-wtf/gin-gonic-prepack/config"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
"github.com/yxzzy-wtf/gin-gonic-prepack/controllers/core"
|
"github.com/yxzzy-wtf/gin-gonic-prepack/controllers/core"
|
||||||
"github.com/yxzzy-wtf/gin-gonic-prepack/database"
|
"github.com/yxzzy-wtf/gin-gonic-prepack/database"
|
||||||
"github.com/yxzzy-wtf/gin-gonic-prepack/models"
|
"github.com/yxzzy-wtf/gin-gonic-prepack/models"
|
||||||
|
"github.com/yxzzy-wtf/gin-gonic-prepack/scheduled"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -27,6 +29,15 @@ func main() {
|
|||||||
db := database.Init()
|
db := database.Init()
|
||||||
Migrate(db)
|
Migrate(db)
|
||||||
|
|
||||||
|
// Scheduled tasks
|
||||||
|
scheduled.Schedule(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: " + err.Error(), time.Hour
|
||||||
|
}
|
||||||
|
return "purged old TOTP usages", time.Hour * 24
|
||||||
|
})
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
// Fresh admin functionality
|
// Fresh admin functionality
|
||||||
|
|||||||
18
scheduled/scheduled.go
Normal file
18
scheduled/scheduled.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package scheduled
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Scheduled func() (string, time.Duration)
|
||||||
|
|
||||||
|
func Schedule(f Scheduled) {
|
||||||
|
print, wait := f()
|
||||||
|
fmt.Println(print)
|
||||||
|
|
||||||
|
go func(w time.Duration) {
|
||||||
|
time.Sleep(w)
|
||||||
|
Schedule(f)
|
||||||
|
}(wait)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user