Add cleanup jobs task

This commit is contained in:
Eike Kettner
2020-03-09 20:24:00 +01:00
parent 854a596da3
commit 718e44a21c
8 changed files with 126 additions and 40 deletions

View File

@ -1,12 +1,15 @@
package docspell.store.records
import cats.effect.Sync
import cats.implicits._
import fs2.Stream
import doobie._
import doobie.implicits._
import io.circe.Encoder
import docspell.common._
import docspell.store.impl.Column
import docspell.store.impl.Implicits._
import io.circe.Encoder
case class RJob(
id: Ident,
@ -227,7 +230,8 @@ object RJob {
}
def selectGroupInState(states: Seq[JobState]): ConnectionIO[Vector[Ident]] = {
val sql = selectDistinct(List(group), table, state.isOneOf(states)) ++ orderBy(group.f)
val sql =
selectDistinct(List(group), table, state.isOneOf(states)) ++ orderBy(group.f)
sql.query[Ident].to[Vector]
}
@ -236,4 +240,19 @@ object RJob {
n0 <- RJobLog.deleteAll(jobId)
n1 <- deleteFrom(table, id.is(jobId)).update.run
} yield n0 + n1
def findIdsDoneAndOlderThan(ts: Timestamp): Stream[ConnectionIO, Ident] =
selectSimple(
Seq(id),
table,
and(state.isOneOf(JobState.done.toSeq), or(finished.isNull, finished.isLt(ts)))
).query[Ident].stream
def deleteDoneAndOlderThan(ts: Timestamp, batch: Int): ConnectionIO[Int] =
findIdsDoneAndOlderThan(ts)
.take(batch.toLong)
.evalMap(delete)
.map(_ => 1)
.compile
.foldMonoid
}