47 lines
1021 B
Go
47 lines
1021 B
Go
package cleanupsessions
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/logger"
|
|
)
|
|
|
|
// TODO set with config
|
|
const cronInterval = time.Minute * 10
|
|
|
|
type ExpiredSessionsRemover interface {
|
|
RemoveExpiredSessions(ctx context.Context) error
|
|
}
|
|
|
|
type CleanupSessionCron struct {
|
|
db ExpiredSessionsRemover
|
|
}
|
|
|
|
func New(db ExpiredSessionsRemover) *CleanupSessionCron {
|
|
return &CleanupSessionCron{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (c *CleanupSessionCron) Run(ctx context.Context) {
|
|
logger.Info(ctx, "[CleanupSessionCron] running cron")
|
|
go func() {
|
|
t := time.NewTicker(cronInterval)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
logger.Warn(ctx, "[CleanupSessionCron] context cancelled")
|
|
return
|
|
case <-t.C:
|
|
logger.Notice(ctx, "[CleanupSessionCron] cleanup started")
|
|
t.Reset(cronInterval)
|
|
if err := c.db.RemoveExpiredSessions(ctx); err != nil {
|
|
logger.Error(ctx, "[CleanupSessionCron] failed to remove expired sessions", logger.Err(err))
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
}
|