Skip to content

Commit 30bffc4

Browse files
authored
Merge pull request #195 from bpm-crafters/bugfix/194__restore_retry_decrement
fix: correct retry counter decrementation
2 parents 39f5950 + 0b21fe7 commit 30bffc4

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

spring-boot-starter/src/main/kotlin/dev/bpmcrafters/processengine/worker/registrar/ProcessEngineStarterRegistrar.kt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
1616
import org.springframework.context.annotation.Lazy
1717
import org.springframework.transaction.support.TransactionTemplate
1818
import java.lang.reflect.Method
19+
import java.time.Duration
1920
import java.util.concurrent.ExecutionException
2021

2122
private val logger = KotlinLogging.logger {}
@@ -218,17 +219,14 @@ class ProcessEngineStarterRegistrar(
218219
}
219220
} else {
220221
try {
221-
val retryCount = if (cause is FailJobException) cause.retryCount
222-
else taskInformation.getMetaValueAsInt(TaskInformation.RETRIES)?.apply { this - 1 }
223-
val retryBackoff = if (cause is FailJobException) cause.retryBackoff
224-
else null
222+
var retry = calculateRetry(taskInformation = taskInformation, cause = cause)
225223
taskCompletionApi.failTask(
226224
FailTaskCmd(
227225
taskId = taskInformation.taskId,
228226
reason = cause.message ?: "Exception during execution of external task worker",
229227
errorDetails = cause.stackTraceToString(),
230-
retryCount = retryCount,
231-
retryBackoff = retryBackoff
228+
retryCount = retry.retryCount,
229+
retryBackoff = retry.retryBackoff
232230
)
233231
).get()
234232
processEngineWorkerMetrics.taskFailed(topic)
@@ -240,7 +238,24 @@ class ProcessEngineStarterRegistrar(
240238
}
241239
}
242240

243-
private fun completeBeforeCommit(complete: Completion): Boolean =
241+
internal fun calculateRetry(taskInformation: TaskInformation, cause: Throwable): FailureRetry {
242+
val retryCount = if (cause is FailJobException) {
243+
cause.retryCount
244+
} else {
245+
taskInformation.getMetaValueAsInt(TaskInformation.RETRIES)?.let { it - 1 }
246+
}
247+
val retryBackoff = if (cause is FailJobException) {
248+
cause.retryBackoff
249+
} else {
250+
null
251+
}
252+
return FailureRetry(
253+
retryCount = retryCount,
254+
retryBackoff = retryBackoff
255+
)
256+
}
257+
258+
internal fun completeBeforeCommit(complete: Completion): Boolean =
244259
if (complete == DEFAULT) {
245260
processEngineWorkerProperties.completeTasksBeforeCommit
246261
} else {
@@ -251,4 +266,13 @@ class ProcessEngineStarterRegistrar(
251266
* Task handler as a function.
252267
*/
253268
fun interface TaskHandlerWithResult : (TaskInformation, Map<String, Any>) -> Any?
269+
270+
/**
271+
* Failure retry information.
272+
*/
273+
data class FailureRetry(
274+
val retryCount: Int?,
275+
val retryBackoff: Duration?
276+
)
277+
254278
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dev.bpmcrafters.processengine.worker.registrar
2+
3+
import dev.bpmcrafters.processengine.worker.FailJobException
4+
import dev.bpmcrafters.processengineapi.task.TaskInformation
5+
import org.assertj.core.api.Assertions.assertThat
6+
import org.junit.jupiter.api.Test
7+
import org.mockito.Mockito.mock
8+
import java.time.Duration
9+
import java.util.UUID
10+
11+
internal class ProcessEngineRegistrarRetryTest {
12+
13+
val testSubject = ProcessEngineStarterRegistrar(
14+
mock(),
15+
mock(),
16+
mock(),
17+
mock(),
18+
mock(),
19+
mock(),
20+
mock(),
21+
mock()
22+
)
23+
24+
@Test
25+
fun `use retry values from exception`() {
26+
val task = TaskInformation(UUID.randomUUID().toString(), mapOf())
27+
val retry = testSubject.calculateRetry(task, FailJobException(message = "Reason", cause = null, retryCount = 17, retryBackoff = Duration.ofMillis(100)))
28+
assertThat(retry.retryCount).isEqualTo(17)
29+
assertThat(retry.retryBackoff).isEqualTo(Duration.ofMillis(100))
30+
}
31+
32+
@Test
33+
fun `use decremented retry value from task`() {
34+
val task = TaskInformation(UUID.randomUUID().toString(), mapOf(
35+
TaskInformation.RETRIES to "19"
36+
))
37+
val retry = testSubject.calculateRetry(task, IllegalArgumentException("Wrong"))
38+
assertThat(retry.retryCount).isEqualTo(18)
39+
assertThat(retry.retryBackoff).isNull()
40+
}
41+
42+
@Test
43+
fun `no value if nothing is provided`() {
44+
val task = TaskInformation(UUID.randomUUID().toString(), mapOf())
45+
val retry = testSubject.calculateRetry(task, IllegalArgumentException("Wrong"))
46+
assertThat(retry.retryCount).isNull()
47+
assertThat(retry.retryBackoff).isNull()
48+
}
49+
}

0 commit comments

Comments
 (0)