Use a minimum age of items to remove

In order to keep deleted items for a while, the periodic task can now
use a duration to only remove items with a certain age. This can be
used to ensure that a deleted item stays at least X days before it
will be removed from the database.

Refs: #347
This commit is contained in:
eikek
2021-08-15 12:28:42 +02:00
parent d136bb8166
commit f4a2b86ea8
27 changed files with 303 additions and 124 deletions

View File

@ -87,9 +87,11 @@ final class JoexAppImpl[F[_]: Async](
private def scheduleEmptyTrashTasks: F[Unit] =
store
.transact(
REmptyTrashSetting.findForAllCollectives(EmptyTrashArgs.defaultSchedule, 50)
REmptyTrashSetting.findForAllCollectives(OCollective.EmptyTrash.default, 50)
)
.evalMap(es =>
EmptyTrashTask.periodicTask(EmptyTrashArgs(es.cid, es.minAge), es.schedule)
)
.evalMap(es => EmptyTrashTask.periodicTask(es.cid, es.schedule))
.evalMap(pstore.insert)
.compile
.drain

View File

@ -26,7 +26,7 @@ object EmptyTrashTask {
private val pageSize = 20
def periodicTask[F[_]: Sync](collective: Ident, ce: CalEvent): F[RPeriodicTask] =
def periodicTask[F[_]: Sync](args: EmptyTrashArgs, ce: CalEvent): F[RPeriodicTask] =
Ident
.randomId[F]
.flatMap(id =>
@ -36,8 +36,8 @@ object EmptyTrashTask {
true,
ce,
None,
EmptyTrashArgs(collective)
).encode.toPeriodicTask(UserTaskScope(collective))
args
).encode.toPeriodicTask(UserTaskScope(args.collective), args.makeSubject.some)
)
def apply[F[_]: Async](
@ -45,23 +45,27 @@ object EmptyTrashTask {
itemSearchOps: OItemSearch[F]
): Task[F, Args, Unit] =
Task { ctx =>
val collId = ctx.args.collective
for {
_ <- ctx.logger.info(s"Starting removing all soft-deleted items")
nDeleted <- deleteAll(collId, itemOps, itemSearchOps, ctx)
now <- Timestamp.current[F]
maxDate = now.minus(ctx.args.minAge)
_ <- ctx.logger.info(
s"Starting removing all soft-deleted items older than ${maxDate.asString}"
)
nDeleted <- deleteAll(ctx.args, maxDate, itemOps, itemSearchOps, ctx)
_ <- ctx.logger.info(s"Finished deleting ${nDeleted} items")
} yield ()
}
private def deleteAll[F[_]: Async](
collective: Ident,
args: Args,
maxUpdate: Timestamp,
itemOps: OItem[F],
itemSearchOps: OItemSearch[F],
ctx: Context[F, _]
): F[Int] =
Stream
.eval(itemSearchOps.findDeleted(collective, pageSize))
.evalMap(deleteChunk(collective, itemOps, ctx))
.eval(itemSearchOps.findDeleted(args.collective, maxUpdate, pageSize))
.evalMap(deleteChunk(args.collective, itemOps, ctx))
.repeat
.takeWhile(_ > 0)
.compile