2019-02-02 05:11:30 -06:00
|
|
|
package authtoken
|
2019-01-21 10:05:42 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-01-22 05:00:33 -06:00
|
|
|
func (srv *UserAuthTokenServiceImpl) Run(ctx context.Context) error {
|
2019-01-21 10:05:42 -06:00
|
|
|
ticker := time.NewTicker(time.Hour * 12)
|
2019-02-05 14:12:30 -06:00
|
|
|
deleteSessionAfter := time.Hour * 24 * time.Duration(srv.Cfg.ExpiredTokensCleanupIntervalDays)
|
2019-01-21 10:05:42 -06:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2019-02-05 14:12:30 -06:00
|
|
|
srv.ServerLockService.LockAndExecute(ctx, "delete expired auth tokens", time.Hour*12, func() {
|
2019-01-21 10:05:42 -06:00
|
|
|
srv.deleteOldSession(deleteSessionAfter)
|
|
|
|
})
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 05:00:33 -06:00
|
|
|
func (srv *UserAuthTokenServiceImpl) deleteOldSession(deleteSessionAfter time.Duration) (int64, error) {
|
2019-01-21 10:05:42 -06:00
|
|
|
sql := `DELETE from user_auth_token WHERE rotated_at < ?`
|
|
|
|
|
|
|
|
deleteBefore := getTime().Add(-deleteSessionAfter)
|
|
|
|
res, err := srv.SQLStore.NewSession().Exec(sql, deleteBefore.Unix())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
srv.log.Info("deleted old sessions", "count", affected)
|
|
|
|
|
|
|
|
return affected, err
|
|
|
|
}
|