mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Add cleanup jobs task
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user