Skip to content

Commit

Permalink
Add lock around task processing
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 11, 2024
1 parent 4f98eba commit 8d9f7ae
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions api/app/util/LockUtil.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package util

import anorm._
import cats.data.ValidatedNec
import cats.implicits._
import io.flow.postgresql.Query
import org.apache.commons.codec.digest.DigestUtils
import play.api.db.Database
Expand All @@ -16,15 +14,15 @@ class LockUtil @Inject() (
db: Database
) {

def lock[T](id: String)(f: Connection => T): ValidatedNec[String, T] =
def lock[T](id: String)(f: Connection => T): Option[T] =
db.withTransaction(lock(_)(id)(f))

def lock[T](c: Connection)(id: String)(f: Connection => T): ValidatedNec[String, T] = {
def lock[T](c: Connection)(id: String)(f: Connection => T): Option[T] = {
require(!c.getAutoCommit, "Must be in a transaction")
if (acquireLock(c)(id))
f(c).validNec
Some(f(c))
else
s"Failed to acquire lock for id '$id'".invalidNec
None
}

private def acquireLock(c: Connection)(id: String): Boolean = {
Expand Down

0 comments on commit 8d9f7ae

Please sign in to comment.