Skip to content

Commit

Permalink
Merge pull request #4 from VEuPathDB/attempt-count
Browse files Browse the repository at this point in the history
Add attempt count to queue model
  • Loading branch information
dmgaldi authored Nov 10, 2023
2 parents 893cbd6 + 7d6a3dd commit 6067c25
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ import org.veupathdb.lib.rabbit.jobs.serialization.JsonSerializable
data class ErrorNotification @JvmOverloads constructor(
val jobID: HashID,
val code: Int,
val attemptCount: Int = 0,
val message: String? = null,
val body: JsonNode? = null,
) : JsonSerializable {
override fun toJson(): JsonNode =
Json.new<ObjectNode> {
put(JsonKey.JobID, jobID.string)
put(JsonKey.Code, code)
put(JsonKey.Message, message)
put(JsonKey.AttemptCount, attemptCount)
putPOJO(JsonKey.Body, body)
}

override fun toString() =
Expand All @@ -46,21 +50,33 @@ data class ErrorNotification @JvmOverloads constructor(
throw IllegalStateException("Error notification has no ${JsonKey.JobID} field!")
if (!json.has(JsonKey.Code))
throw IllegalStateException("Error notification has no ${JsonKey.Code} field!")
if (!json.has(JsonKey.Body))
throw IllegalStateException("Error notification has no ${JsonKey.Body} field!")
if (!json.has(JsonKey.AttemptCount))
throw IllegalStateException("Error notification has no ${JsonKey.AttemptCount} field!")

if (json.get(JsonKey.JobID).isNull)
throw IllegalStateException("Error notification has a null ${JsonKey.JobID} field!")
if (json.get(JsonKey.Code).isNull)
throw IllegalStateException("Error notification has a null ${JsonKey.Code} field!")
if (json.get(JsonKey.Body).isNull)
throw IllegalStateException("Error notification has a null ${JsonKey.Body} field!")
if (json.get(JsonKey.AttemptCount).isNull)
throw IllegalStateException("Error notification has a null ${JsonKey.AttemptCount} field!")

if (!json.get(JsonKey.JobID).isTextual)
throw IllegalStateException("Error notification has a non-textual ${JsonKey.JobID} field!")
if (!json.get(JsonKey.Code).isIntegralNumber)
throw IllegalStateException("Error notification has a non-integral ${JsonKey.Code} field!")
if (!json.get(JsonKey.AttemptCount).isIntegralNumber)
throw IllegalStateException("Error notification has a non-integral ${JsonKey.AttemptCount} field!")

return ErrorNotification(
HashID(json.get(JsonKey.JobID).textValue()),
json.get(JsonKey.Code).intValue(),
json.get(JsonKey.Message).textValue()
jobID = HashID(json.get(JsonKey.JobID).textValue()),
code = json.get(JsonKey.Code).intValue(),
message = json.get(JsonKey.Message).textValue(),
attemptCount = json.get(JsonKey.AttemptCount).intValue(),
body = json.get(JsonKey.Body),
)
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/kotlin/org/veupathdb/lib/rabbit/jobs/model/JobDispatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.veupathdb.lib.rabbit.jobs.serialization.JsonDeserializable
import org.veupathdb.lib.rabbit.jobs.serialization.JsonKey
import org.veupathdb.lib.rabbit.jobs.serialization.JsonSerializable
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter

/**
* Job Request/Dispatch
Expand Down Expand Up @@ -38,13 +37,15 @@ data class JobDispatch(
val jobID: HashID,
val body: JsonNode? = null,
val type: String = "",
val attemptCount: Int = 0,
val dispatched: OffsetDateTime = OffsetDateTime.now()
) : JsonSerializable {
override fun toJson() =
Json.new<ObjectNode> {
put(JsonKey.JobID, jobID.string)
put(JsonKey.Type, type)
put(JsonKey.Dispatched, dispatched.toString())
put(JsonKey.AttemptCount, attemptCount)
putPOJO(JsonKey.Body, body)
}

Expand All @@ -64,6 +65,8 @@ data class JobDispatch(
throw IllegalStateException("Job dispatch has no ${JsonKey.Body} field!")
if (!json.has(JsonKey.Dispatched))
throw IllegalStateException("Job dispatch has no ${JsonKey.Dispatched} field!")
if (!json.has(JsonKey.AttemptCount))
throw IllegalStateException("Job dispatch has no ${JsonKey.AttemptCount} field!")

// Only body field may be null
if (json.get(JsonKey.JobID).isNull)
Expand All @@ -72,6 +75,8 @@ data class JobDispatch(
throw IllegalStateException("Job dispatch has a null ${JsonKey.Type} field!")
if (json.get(JsonKey.Dispatched).isNull)
throw IllegalStateException("Job dispatch has a null ${JsonKey.Dispatched} field!")
if (json.get(JsonKey.AttemptCount).isNull)
throw IllegalStateException("Job dispatch has a null ${JsonKey.AttemptCount} field!")

// Fields must be the correct type
if (!json.get(JsonKey.JobID).isTextual)
Expand All @@ -80,12 +85,15 @@ data class JobDispatch(
throw IllegalStateException("Job dispatch has a non-textual ${JsonKey.Type} field!")
if (!json.get(JsonKey.Dispatched).isTextual)
throw IllegalStateException("Job dispatch has a non-textual ${JsonKey.Dispatched} field!")
if (!json.get(JsonKey.AttemptCount).isInt)
throw IllegalStateException("Job dispatch has a non-int ${JsonKey.AttemptCount} field!")

return JobDispatch(
HashID(json.get(JsonKey.JobID).textValue()),
json.get(JsonKey.Body),
json.get(JsonKey.Type).textValue(),
OffsetDateTime.parse(json.get(JsonKey.Dispatched).textValue())
jobID = HashID(json.get(JsonKey.JobID).textValue()),
body = json.get(JsonKey.Body),
type = json.get(JsonKey.Type).textValue(),
dispatched = OffsetDateTime.parse(json.get(JsonKey.Dispatched).textValue()),
attemptCount = json.get(JsonKey.AttemptCount).intValue()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ internal object JsonKey {
const val Message = "message"
const val Type = "type"
const val Dispatched = "dispatched"
const val AttemptCount = "attemptCount"
}
3 changes: 2 additions & 1 deletion test/client/src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@file:JvmName("Main")

import com.fasterxml.jackson.databind.node.TextNode
import org.veupathdb.lib.hash_id.HashID
import org.veupathdb.lib.rabbit.jobs.QueueWorker
import org.veupathdb.lib.rabbit.jobs.model.ErrorNotification
Expand All @@ -17,5 +18,5 @@ fun main() {
}

conFac.sendSuccess(SuccessNotification(HashID("0102030405060708090A0B0C0D0E0F10")))
conFac.sendError(ErrorNotification(HashID("0102030405060708090A0B0C0D0E0F10"), 123, "butts"))
conFac.sendError(ErrorNotification(HashID("0102030405060708090A0B0C0D0E0F10"), 123, 0,"butts", TextNode("body")))
}
2 changes: 1 addition & 1 deletion test/server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "org.veupathdb.lib.test"
version = "1.0.0"
version = "1.1.0"

repositories {
mavenCentral()
Expand Down

0 comments on commit 6067c25

Please sign in to comment.