Make sure to only remove/retry items in premature states

This commit is contained in:
Eike Kettner 2020-10-26 22:02:31 +01:00
parent dd89e05cc2
commit b59696a9d3
3 changed files with 25 additions and 12 deletions

View File

@ -121,9 +121,10 @@ object CreateItem {
private def findExisting[F[_]: Sync]: Task[F, ProcessItemArgs, Option[ItemData]] =
Task { ctx =>
val states = ItemState.invalidStates.toList.toSet
val fileMetaIds = ctx.args.files.map(_.fileMetaId).toSet
for {
cand <- ctx.store.transact(QItem.findByFileIds(fileMetaIds.toSeq))
cand <- ctx.store.transact(QItem.findByFileIds(fileMetaIds.toSeq, states))
_ <-
if (cand.nonEmpty)
ctx.logger.warn(s"Found ${cand.size} existing item with these files.")

View File

@ -103,10 +103,13 @@ object ItemHandler {
)
}
def deleteByFileIds[F[_]: Sync: ContextShift]: Task[F, Args, Unit] =
private def deleteByFileIds[F[_]: Sync: ContextShift]: Task[F, Args, Unit] =
Task { ctx =>
val states = ItemState.invalidStates.toList.toSet
for {
items <- ctx.store.transact(QItem.findByFileIds(ctx.args.files.map(_.fileMetaId)))
items <- ctx.store.transact(
QItem.findByFileIds(ctx.args.files.map(_.fileMetaId), states)
)
_ <-
if (items.nonEmpty) ctx.logger.info(s"Deleting items ${items.map(_.id.id)}")
else

View File

@ -493,13 +493,15 @@ object QItem {
private def findByFileIdsQuery(
fileMetaIds: NonEmptyList[Ident],
limit: Option[Int]
limit: Option[Int],
states: Set[ItemState]
): Fragment = {
val IC = RItem.Columns.all.map(_.prefix("i"))
val aItem = RAttachment.Columns.itemId.prefix("a")
val aId = RAttachment.Columns.id.prefix("a")
val aFileId = RAttachment.Columns.fileId.prefix("a")
val iId = RItem.Columns.id.prefix("i")
val iState = RItem.Columns.state.prefix("i")
val sId = RAttachmentSource.Columns.id.prefix("s")
val sFileId = RAttachmentSource.Columns.fileId.prefix("s")
val rId = RAttachmentArchive.Columns.id.prefix("r")
@ -516,11 +518,15 @@ object QItem {
fr"LEFT OUTER JOIN" ++ RAttachmentArchive.table ++ fr"r ON" ++ aId.is(rId) ++
fr"LEFT OUTER JOIN" ++ RFileMeta.table ++ fr"m3 ON" ++ m3Id.is(rFileId)
val q = selectSimple(
IC,
from,
and(or(m1Id.isIn(fileMetaIds), m2Id.isIn(fileMetaIds), m3Id.isIn(fileMetaIds)))
)
val fileCond =
or(m1Id.isIn(fileMetaIds), m2Id.isIn(fileMetaIds), m3Id.isIn(fileMetaIds))
val cond = NonEmptyList.fromList(states.toList) match {
case Some(nel) =>
and(fileCond, iState.isIn(nel))
case None =>
fileCond
}
val q = selectSimple(IC, from, cond)
limit match {
case Some(n) => q ++ fr"LIMIT $n"
@ -531,15 +537,18 @@ object QItem {
def findOneByFileIds(fileMetaIds: Seq[Ident]): ConnectionIO[Option[RItem]] =
NonEmptyList.fromList(fileMetaIds.toList) match {
case Some(nel) =>
findByFileIdsQuery(nel, Some(1)).query[RItem].option
findByFileIdsQuery(nel, Some(1), Set.empty).query[RItem].option
case None =>
(None: Option[RItem]).pure[ConnectionIO]
}
def findByFileIds(fileMetaIds: Seq[Ident]): ConnectionIO[Vector[RItem]] =
def findByFileIds(
fileMetaIds: Seq[Ident],
states: Set[ItemState]
): ConnectionIO[Vector[RItem]] =
NonEmptyList.fromList(fileMetaIds.toList) match {
case Some(nel) =>
findByFileIdsQuery(nel, None).query[RItem].to[Vector]
findByFileIdsQuery(nel, None, states).query[RItem].to[Vector]
case None =>
Vector.empty[RItem].pure[ConnectionIO]
}