Cleanup remember-me tokens periodically

This commit is contained in:
Eike Kettner 2020-12-04 00:59:08 +01:00
parent a0642905db
commit f5ae389eea
4 changed files with 41 additions and 1 deletions

View File

@ -142,6 +142,17 @@ docspell.joex {
older-than = "30 days" older-than = "30 days"
} }
# This task removes expired remember-me tokens. The timespan
# should be greater than the `valid` time in the restserver
# config.
cleanup-remember-me = {
# Whether the job is enabled.
enabled = true
# The minimum age of tokens to be deleted.
older-than = "30 days"
}
# Jobs store their log output in the database. Normally this data # Jobs store their log output in the database. Normally this data
# is only interesting for some period of time. The processing logs # is only interesting for some period of time. The processing logs
# of old files can be removed eventually. # of old files can be removed eventually.

View File

@ -0,0 +1,25 @@
package docspell.joex.hk
import cats.effect._
import cats.implicits._
import docspell.common._
import docspell.joex.scheduler.Task
import docspell.store.records._
object CleanupRememberMeTask {
def apply[F[_]: Sync](cfg: HouseKeepingConfig.CleanupRememberMe): Task[F, Unit, Unit] =
Task { ctx =>
if (cfg.enabled)
for {
now <- Timestamp.current[F]
ts = now - cfg.olderThan
_ <- ctx.logger.info(s"Cleanup remember-me tokens older than $ts")
n <- ctx.store.transact(RRememberMe.deleteOlderThan(ts))
_ <- ctx.logger.info(s"Removed $n tokens")
} yield ()
else
ctx.logger.info("CleanupRememberMe task is disabled in the configuration")
}
}

View File

@ -8,7 +8,8 @@ import com.github.eikek.calev.CalEvent
case class HouseKeepingConfig( case class HouseKeepingConfig(
schedule: CalEvent, schedule: CalEvent,
cleanupInvites: CleanupInvites, cleanupInvites: CleanupInvites,
cleanupJobs: CleanupJobs cleanupJobs: CleanupJobs,
cleanupRememberMe: CleanupRememberMe
) )
object HouseKeepingConfig { object HouseKeepingConfig {
@ -17,4 +18,6 @@ object HouseKeepingConfig {
case class CleanupJobs(enabled: Boolean, olderThan: Duration, deleteBatch: Int) case class CleanupJobs(enabled: Boolean, olderThan: Duration, deleteBatch: Int)
case class CleanupRememberMe(enabled: Boolean, olderThan: Duration)
} }

View File

@ -19,6 +19,7 @@ object HouseKeepingTask {
Task Task
.log[F, Unit](_.info(s"Running house-keeping task now")) .log[F, Unit](_.info(s"Running house-keeping task now"))
.flatMap(_ => CleanupInvitesTask(cfg.houseKeeping.cleanupInvites)) .flatMap(_ => CleanupInvitesTask(cfg.houseKeeping.cleanupInvites))
.flatMap(_ => CleanupRememberMeTask(cfg.houseKeeping.cleanupRememberMe))
.flatMap(_ => CleanupJobsTask(cfg.houseKeeping.cleanupJobs)) .flatMap(_ => CleanupJobsTask(cfg.houseKeeping.cleanupJobs))
def onCancel[F[_]: Sync]: Task[F, Unit, Unit] = def onCancel[F[_]: Sync]: Task[F, Unit, Unit] =