Purge old expired sessions
This commit is contained in:
parent
93b464b0ff
commit
c577facc24
3 changed files with 75 additions and 0 deletions
30
helpers/jobs.go
Normal file
30
helpers/jobs.go
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
package helpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterJob(interval time.Duration, name string, job func(string) error) {
|
||||||
|
log := GetLogger()
|
||||||
|
log.Info(
|
||||||
|
"registering job",
|
||||||
|
"name", name,
|
||||||
|
"interval", interval,
|
||||||
|
)
|
||||||
|
|
||||||
|
for {
|
||||||
|
go func(job func(string) error, name string) {
|
||||||
|
log := GetLogger()
|
||||||
|
log.Info("starting job", "name", name)
|
||||||
|
|
||||||
|
err := job(name)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err, "job", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("job finished", "name", name)
|
||||||
|
}(job, name)
|
||||||
|
|
||||||
|
time.Sleep(interval)
|
||||||
|
}
|
||||||
|
}
|
||||||
40
jobs/saved_session.go
Normal file
40
jobs/saved_session.go
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
package jobs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.readonly.ch/bouzoure/pop-camarades/helpers"
|
||||||
|
"git.readonly.ch/bouzoure/pop-camarades/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CleanSavedSessions(jobName string) error {
|
||||||
|
log := helpers.GetLogger()
|
||||||
|
|
||||||
|
db, err := helpers.GetDatabase()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("fetching expired sessions", "job", jobName)
|
||||||
|
|
||||||
|
result := db.Unscoped().Delete(
|
||||||
|
&models.UserSavedSession{},
|
||||||
|
"expiration <= ?", time.Now(),
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.RowsAffected < 1 {
|
||||||
|
log.Info("found no expired sessions", "job", jobName)
|
||||||
|
} else {
|
||||||
|
log.Info(
|
||||||
|
"removed expired sessions",
|
||||||
|
"sessions", result.RowsAffected,
|
||||||
|
"job", jobName,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
5
main.go
5
main.go
|
|
@ -5,9 +5,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.readonly.ch/bouzoure/pop-camarades/controllers"
|
"git.readonly.ch/bouzoure/pop-camarades/controllers"
|
||||||
"git.readonly.ch/bouzoure/pop-camarades/helpers"
|
"git.readonly.ch/bouzoure/pop-camarades/helpers"
|
||||||
|
"git.readonly.ch/bouzoure/pop-camarades/jobs"
|
||||||
"git.readonly.ch/bouzoure/pop-camarades/middlewares"
|
"git.readonly.ch/bouzoure/pop-camarades/middlewares"
|
||||||
"github.com/flosch/pongo2/v6"
|
"github.com/flosch/pongo2/v6"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
|
@ -43,6 +45,9 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterJobs
|
||||||
|
go helpers.RegisterJob(60*time.Minute, "clean saved sessions", jobs.CleanSavedSessions)
|
||||||
|
|
||||||
// Create a new engine
|
// Create a new engine
|
||||||
var engine *django.Engine
|
var engine *django.Engine
|
||||||
if config.Debug {
|
if config.Debug {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue