aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuicommon/concurrent/BridgeUploadQueue.scala
blob: 82132622d3e099d54a26d40818bf26c0127a6461 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package xyz.driver.pdsuicommon.concurrent

import java.time.LocalDateTime

import xyz.driver.pdsuicommon.concurrent.BridgeUploadQueue.Item
import xyz.driver.pdsuicommon.logging._

import scala.concurrent.Future

object BridgeUploadQueue {

  /**
    * @param kind        For example documents
    * @param tag         For example, a patient's id: 1
    * @param attempts    Which attempt
    * @param created     When the task was created
    * @param nextAttempt Time of the next attempt
    */
  final case class Item(kind: String,
                        tag: String,
                        created: LocalDateTime,
                        attempts: Int,
                        nextAttempt: LocalDateTime,
                        completed: Boolean,
                        dependencyKind: Option[String],
                        dependencyTag: Option[String]) {

    def dependency: Option[Dependency] = {
      dependencyKind
        .zip(dependencyTag)
        .headOption
        .map(Function.tupled(Dependency.apply))
    }

  }

  object Item {

    implicit def toPhiString(x: Item): PhiString = {
      import x._
      phi"BridgeUploadQueue.Item(kind=${Unsafe(kind)}, tag=${Unsafe(tag)}, " +
        phi"attempts=${Unsafe(attempts)}, start=$created, nextAttempt=$nextAttempt, completed=$completed, " +
        phi"dependency=$dependency)"
    }

    def apply(kind: String, tag: String, dependency: Option[Dependency] = None): Item = {
      val now = LocalDateTime.now()

      Item(
        kind = kind,
        tag = tag,
        created = now,
        attempts = 0,
        nextAttempt = now,
        completed = false,
        dependencyKind = dependency.map(_.kind),
        dependencyTag = dependency.map(_.tag)
      )
    }

  }

  final case class Dependency(kind: String, tag: String)

  object Dependency {

    implicit def toPhiString(x: Dependency): PhiString = {
      import x._
      phi"Dependency(kind=${Unsafe(kind)}, tag=${Unsafe(tag)})"
    }
  }
}

trait BridgeUploadQueue {

  def add(item: Item): Future[Item]

  def get(kind: String): Future[Option[Item]]

  def complete(kind: String, tag: String): Future[Unit]

  def tryRetry(item: Item): Future[Option[Item]]

}