Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,23 @@ annotation class ProcessEngineWorker(
*
* @see autoComplete
*/
val completion: Completion = Completion.DEFAULT
val completion: Completion = Completion.DEFAULT,
/**
* Optional lock duration in seconds for this worker.
* If not specified (default: -1), the adapter's global configuration will be used.
* @since 0.8.0
*/
val lockDuration: Long = DEFAULT_UNSET_LOCK_DURATION
) {
companion object {
/**
* Null value for the topic.
*/
const val DEFAULT_UNSET_TOPIC = "__unset"
/**
* Sentinel value indicating lock duration is not specified.
*/
const val DEFAULT_UNSET_LOCK_DURATION = -1L
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dev.bpmcrafters.processengine.worker.ProcessEngineWorker.Completion.DEFAU
import dev.bpmcrafters.processengine.worker.configuration.ProcessEngineWorkerAutoConfiguration
import dev.bpmcrafters.processengine.worker.configuration.ProcessEngineWorkerProperties
import dev.bpmcrafters.processengine.worker.configuration.ProcessEngineWorkerProperties.Companion.DEFAULT_PREFIX
import dev.bpmcrafters.processengineapi.CommonRestrictions
import dev.bpmcrafters.processengineapi.task.*
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.beans.factory.config.BeanPostProcessor
Expand Down Expand Up @@ -54,7 +55,7 @@ class ProcessEngineStarterRegistrar(
logger.debug { "PROCESS-ENGINE-WORKER-001: Detected ${annotatedProcessEngineWorkers.size} annotated workers on $beanName." }
logger.trace { "PROCESS-ENGINE-WORKER-001: Detected annotated workers on $beanName are: ${annotatedProcessEngineWorkers.map { it.name }}." }
}
annotatedProcessEngineWorkers.map { method ->
annotatedProcessEngineWorkers.forEach { method ->

val topic = method.getTopic()
// detects among all result resolver if the specified payload may be converted to payload return type
Expand All @@ -77,6 +78,12 @@ class ProcessEngineStarterRegistrar(
}

val completion = method.getCompletion()
val customLockDuration = method.getLockDuration()
val restrictions = if (customLockDuration == null) {
mapOf()
} else {
mapOf("workerLockDurationInMilliseconds" to customLockDuration.toString()) // FIXME replace with constant introduced in Process Engine API 1.6
}

// check if the method or class is marked to run in transaction
val isTransactional = method.isTransactional()
Expand All @@ -85,6 +92,7 @@ class ProcessEngineStarterRegistrar(
subscribe(
topic = topic,
payloadDescription = variableNames,
restrictions = restrictions,
autoCompleteTask = autoCompleteTask,
completion = completion,
isTransactional = isTransactional,
Expand All @@ -110,6 +118,7 @@ class ProcessEngineStarterRegistrar(
* Executes the subscription.
* @param topic subscription topic.
* @param payloadDescription description of the variables to be passed.
* @param lockDuration optional lock duration in seconds for this worker.
* @param autoCompleteTask flag indicating if the task should be completed after execution of the worker.
* @param isTransactional flag indicating if the task worker and task completion should run in a transaction.
* @param payloadReturnType flag indicating of the return type of the method can be converted int payload.
Expand All @@ -120,14 +129,15 @@ class ProcessEngineStarterRegistrar(
private fun subscribe(
topic: String,
payloadDescription: Set<String>? = emptySet(),
restrictions: Map<String, String> = mapOf(),
autoCompleteTask: Boolean,
completion: Completion,
isTransactional: Boolean,
payloadReturnType: Boolean,
method: Method,
actionWithResult: TaskHandlerWithResult
): SubscribeForTaskCmd = SubscribeForTaskCmd(
restrictions = mapOf(),
restrictions = restrictions,
taskType = TaskType.EXTERNAL,
taskDescriptionKey = topic,
payloadDescription = payloadDescription,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ fun Method.getCompletion(): Completion {
return this.getAnnotation(ProcessEngineWorker::class.java).completion
}

/**
* Returns the lock duration from annotation, or null if not set.
* @return lock duration in seconds, or null if the default should be used.
* @since 0.8.0
*/
fun Method.getLockDuration(): Long? {
val lockDuration = this.getAnnotation(ProcessEngineWorker::class.java).lockDuration
return if (lockDuration == ProcessEngineWorker.DEFAULT_UNSET_LOCK_DURATION) {
null
} else {
lockDuration
}
}

/**
* Checks if the method of the worker is transactional.
* @return true, if the method should be executed transactional and be atomic with completion of the worker.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.bpmcrafters.processengine.worker.registrar

import dev.bpmcrafters.processengine.worker.ProcessEngineWorker
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test

class ReflectionUtilsTest {
Expand Down Expand Up @@ -37,4 +39,37 @@ class ReflectionUtilsTest {
assertThat(method.hasPayloadReturnType()).isTrue()
}
}

@Nested
inner class LockDurationTest {

@Test
fun `getLockDuration should return value when specified`() {
val method = LockDurationTestWorker::class.java.getDeclaredMethod("workerWithLockDuration")
assertThat(method.getLockDuration()).isEqualTo(30L)
}

@Test
fun `getLockDuration should return null when not specified`() {
val method = LockDurationTestWorker::class.java.getDeclaredMethod("workerWithoutLockDuration")
assertThat(method.getLockDuration()).isNull()
}

@Test
fun `getLockDuration should return null when explicit default`() {
val method = LockDurationTestWorker::class.java.getDeclaredMethod("workerWithExplicitDefault")
assertThat(method.getLockDuration()).isNull()
}

inner class LockDurationTestWorker {
@ProcessEngineWorker(topic = "withLock", lockDuration = 30)
fun workerWithLockDuration() {}

@ProcessEngineWorker(topic = "withoutLock")
fun workerWithoutLockDuration() {}

@ProcessEngineWorker(topic = "explicitDefault", lockDuration = -1)
fun workerWithExplicitDefault() {}
}
}
}