Fix next-job query to do round-robin through job groups

This commit is contained in:
eikek
2021-06-26 23:33:25 +02:00
parent 988367a281
commit ce6f53cc29
7 changed files with 162 additions and 23 deletions

View File

@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<encoder>
<pattern>%highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
<logger name="docspell" level="warn" />
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -1,27 +1,33 @@
package docspell.store
import cats.effect._
import cats.effect.unsafe.implicits.global
import docspell.common.LenientUri
import docspell.store.impl.StoreImpl
import doobie._
import munit._
import org.h2.jdbcx.JdbcConnectionPool
trait StoreFixture {
def withStore(db: String)(code: Store[IO] => IO[Unit]): Unit = {
//StoreFixture.store(StoreFixture.memoryDB(db)).use(code).unsafeRunSync()
val jdbc = StoreFixture.memoryDB(db)
val xa = StoreFixture.globalXA(jdbc)
val store = new StoreImpl[IO](jdbc, xa)
store.migrate.unsafeRunSync()
code(store).unsafeRunSync()
trait StoreFixture extends CatsEffectFunFixtures { self: CatsEffectSuite =>
val xa = ResourceFixture {
val cfg = StoreFixture.memoryDB("test")
for {
xa <- StoreFixture.makeXA(cfg)
store = new StoreImpl[IO](cfg, xa)
_ <- Resource.eval(store.migrate)
} yield xa
}
def withXA(db: String)(code: Transactor[IO] => IO[Unit]): Unit =
StoreFixture.makeXA(StoreFixture.memoryDB(db)).use(code).unsafeRunSync()
val store = ResourceFixture {
val cfg = StoreFixture.memoryDB("test")
for {
xa <- StoreFixture.makeXA(cfg)
store = new StoreImpl[IO](cfg, xa)
_ <- Resource.eval(store.migrate)
} yield store
}
}
object StoreFixture {

View File

@ -0,0 +1,88 @@
package docspell.store.queries
import java.time.Instant
import java.util.concurrent.atomic.AtomicLong
import cats.implicits._
import docspell.common._
import docspell.store.StoreFixture
import docspell.store.records.RJob
import docspell.store.records.RJobGroupUse
import doobie.implicits._
import munit._
class QJobTest extends CatsEffectSuite with StoreFixture {
private[this] val c = new AtomicLong(0)
private val worker = Ident.unsafe("joex1")
private val initialPause = Duration.seconds(5)
private val nowTs = Timestamp(Instant.parse("2021-06-26T14:54:00Z"))
private val group1 = Ident.unsafe("group1")
private val group2 = Ident.unsafe("group2")
def createJob(group: Ident): RJob =
RJob.newJob[Unit](
Ident.unsafe(s"job-${c.incrementAndGet()}"),
Ident.unsafe("task"),
group,
(),
"some subject",
nowTs - Duration.days(3),
Ident.unsafe("user1"),
Priority.Low,
None
)
xa.test("set group must insert or update") { tx =>
val res =
for {
_ <- RJobGroupUse.setGroup(RJobGroupUse(group1, worker)).transact(tx)
res <- RJobGroupUse.findGroup(worker).transact(tx)
} yield res
res.assertEquals(Some(group1))
}
xa.test("selectNextGroup should return first group on initial state") { tx =>
val nextGroup = for {
_ <- List(group1, group2, group1, group2, group2)
.map(createJob)
.map(RJob.insert)
.traverse(_.transact(tx))
_ <- RJobGroupUse.deleteAll.transact(tx)
next <- QJob.selectNextGroup(worker, nowTs, initialPause).transact(tx)
} yield next
nextGroup.assertEquals(Some(group1))
}
xa.test("selectNextGroup should return second group on subsequent call (1)") { tx =>
val nextGroup = for {
_ <- List(group1, group2, group1, group2)
.map(createJob)
.map(RJob.insert)
.traverse(_.transact(tx))
_ <- RJobGroupUse.deleteAll.transact(tx)
_ <- RJobGroupUse.setGroup(RJobGroupUse(group1, worker)).transact(tx)
next <- QJob.selectNextGroup(worker, nowTs, initialPause).transact(tx)
} yield next
nextGroup.assertEquals(Some(group2))
}
xa.test("selectNextGroup should return second group on subsequent call (2)") { tx =>
val nextGroup = for {
_ <- List(group1, group2, group1, group2)
.map(createJob)
.map(RJob.insert)
.traverse(_.transact(tx))
_ <- RJobGroupUse.deleteAll.transact(tx)
_ <- RJobGroupUse.setGroup(RJobGroupUse(group2, worker)).transact(tx)
next <- QJob.selectNextGroup(worker, nowTs, initialPause).transact(tx)
} yield next
nextGroup.assertEquals(Some(group1))
}
}