Skip to content

Commit 3252215

Browse files
committed
add tenant configuration
1 parent 5a0ff86 commit 3252215

5 files changed

Lines changed: 88 additions & 20 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ annotation class ProcessEngineWorker(
1212
/**
1313
* Topic name to subscribe this worker for.
1414
*/
15-
@get: AliasFor(attribute = "value")
15+
@get:AliasFor(attribute = "value")
1616
val topic: String = DEFAULT_UNSET_TOPIC,
1717
/**
1818
* Topic name to subscribe this worker for.
1919
*/
20-
@get: AliasFor(attribute = "topic")
20+
@get:AliasFor(attribute = "topic")
2121
val value: String = DEFAULT_UNSET_TOPIC,
2222
/**
2323
* Flag, indicating if the task should be automatically completed after the execution of the worker.
@@ -38,13 +38,23 @@ annotation class ProcessEngineWorker(
3838
* If not specified (default: -1), the adapter's global configuration will be used.
3939
* @since 0.8.0
4040
*/
41-
val lockDuration: Long = DEFAULT_UNSET_LOCK_DURATION
41+
val lockDuration: Long = DEFAULT_UNSET_LOCK_DURATION,
42+
/**
43+
* Tenant ID to subscribe this worker for. Defaults to "__unset" representing no tenant.
44+
*/
45+
val tenantId: String = DEFAULT_UNSET_TENANT_ID,
4246
) {
4347
companion object {
4448
/**
4549
* Null value for the topic.
4650
*/
4751
const val DEFAULT_UNSET_TOPIC = "__unset"
52+
53+
/**
54+
* Null value for the tenant.
55+
*/
56+
const val DEFAULT_UNSET_TENANT_ID = "__unset"
57+
4858
/**
4959
* Sentinel value indicating lock duration is not specified.
5060
*/
@@ -59,12 +69,14 @@ annotation class ProcessEngineWorker(
5969
* Use default configured via property.
6070
*/
6171
DEFAULT,
72+
6273
/**
6374
* Execute external task completion before the transaction is committed.
6475
*/
6576
BEFORE_COMMIT,
77+
6678
/**
67-
* Execute external task completion after transaction is committed.
79+
* Execute external task completion after the transaction is committed.
6880
*/
6981
AFTER_COMMIT,
7082
}

spring-boot-starter/src/main/kotlin/dev/bpmcrafters/processengine/worker/configuration/ProcessEngineWorkerProperties.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ data class ProcessEngineWorkerProperties(
2626
* if the completion of a task was successful but the removal of a task result was not.
2727
*/
2828
var removeTaskResultOnCompletion: Boolean = true,
29+
/**
30+
* Default tenant id to use for all workers.
31+
*/
32+
var tenantId: String? = null,
2933
) {
3034
companion object {
3135
const val DEFAULT_PREFIX = "dev.bpm-crafters.process-api.worker"

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@ import dev.bpmcrafters.processengine.worker.ProcessEngineWorker.Completion.DEFAU
88
import dev.bpmcrafters.processengine.worker.configuration.ProcessEngineWorkerAutoConfiguration
99
import dev.bpmcrafters.processengine.worker.configuration.ProcessEngineWorkerProperties
1010
import dev.bpmcrafters.processengine.worker.configuration.ProcessEngineWorkerProperties.Companion.DEFAULT_PREFIX
11-
import dev.bpmcrafters.processengineapi.task.CompleteTaskByErrorCmd
12-
import dev.bpmcrafters.processengineapi.task.CompleteTaskCmd
13-
import dev.bpmcrafters.processengineapi.task.FailTaskCmd
14-
import dev.bpmcrafters.processengineapi.task.ServiceTaskCompletionApi
15-
import dev.bpmcrafters.processengineapi.task.SubscribeForTaskCmd
16-
import dev.bpmcrafters.processengineapi.task.TaskInformation
17-
import dev.bpmcrafters.processengineapi.task.TaskSubscriptionApi
18-
import dev.bpmcrafters.processengineapi.task.TaskType
1911
import dev.bpmcrafters.processengine.worker.idempotency.IdempotencyRegistry
12+
import dev.bpmcrafters.processengineapi.CommonRestrictions
13+
import dev.bpmcrafters.processengineapi.task.*
2014
import io.github.oshai.kotlinlogging.KotlinLogging
2115
import org.springframework.beans.factory.config.BeanPostProcessor
2216
import org.springframework.boot.autoconfigure.AutoConfiguration
@@ -88,11 +82,16 @@ class ProcessEngineStarterRegistrar(
8882

8983
val completion = method.getCompletion()
9084
val customLockDuration = method.getLockDuration()
91-
val restrictions = if (customLockDuration == null) {
92-
mapOf()
93-
} else {
94-
mapOf("workerLockDurationInMilliseconds" to customLockDuration.toString()) // FIXME replace with constant introduced in Process Engine API 1.6
95-
}
85+
val tenantId = method.getTenantId() ?: processEngineWorkerProperties.tenantId
86+
87+
val restrictions: Map<String, String> = mutableMapOf<String, String>().apply {
88+
if (customLockDuration != null) {
89+
this[CommonRestrictions.WORKER_LOCK_DURATION_IN_MILLISECONDS] = customLockDuration.toString()
90+
}
91+
if (tenantId != null) {
92+
this[CommonRestrictions.TENANT_ID] = tenantId
93+
}
94+
}.toMap()
9695

9796
// check if the method or class is marked to run in transaction
9897
val isTransactional = method.isTransactional()

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ fun Method.getLockDuration(): Long? {
176176
}
177177
}
178178

179+
/**
180+
* Returns the tenant id, configured on the annotation or null if it is not set.
181+
* @return tenant id, or null if the default should be used.
182+
* @since 0.8.4
183+
*/
184+
fun Method.getTenantId(): String? {
185+
val tenantId = this.getAnnotation(ProcessEngineWorker::class.java).tenantId
186+
return if (tenantId == ProcessEngineWorker.DEFAULT_UNSET_TENANT_ID || tenantId.isBlank()) {
187+
null
188+
} else {
189+
tenantId
190+
}
191+
}
192+
179193
/**
180194
* Checks if the method of the worker is transactional.
181195
* @return true, if the method should be executed transactional and be atomic with completion of the worker.

spring-boot-starter/src/test/kotlin/dev/bpmcrafters/processengine/worker/registrar/ReflectionUtilsTest.kt

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,52 @@ class ReflectionUtilsTest {
6363

6464
inner class LockDurationTestWorker {
6565
@ProcessEngineWorker(topic = "withLock", lockDuration = 30)
66-
fun workerWithLockDuration() {}
66+
fun workerWithLockDuration() {
67+
}
6768

6869
@ProcessEngineWorker(topic = "withoutLock")
69-
fun workerWithoutLockDuration() {}
70+
fun workerWithoutLockDuration() {
71+
}
7072

7173
@ProcessEngineWorker(topic = "explicitDefault", lockDuration = -1)
72-
fun workerWithExplicitDefault() {}
74+
fun workerWithExplicitDefault() {
75+
}
76+
}
77+
}
78+
79+
@Nested
80+
inner class TenantIdTest {
81+
82+
@Test
83+
fun `getTenantId should return value when specified`() {
84+
val method = TenantWorker::class.java.getDeclaredMethod("workerWithTenantId")
85+
assertThat(method.getTenantId()).isEqualTo("tenant")
86+
}
87+
88+
@Test
89+
fun `getTenantId should return null when not specified`() {
90+
val method = TenantWorker::class.java.getDeclaredMethod("worker")
91+
assertThat(method.getTenantId()).isNull()
92+
}
93+
94+
@Test
95+
fun `getTenantId should return null when specified empty string`() {
96+
val method = TenantWorker::class.java.getDeclaredMethod("withEmptyTenantId")
97+
assertThat(method.getTenantId()).isNull()
98+
}
99+
100+
inner class TenantWorker {
101+
@ProcessEngineWorker(topic = "withTenant", tenantId = "tenant")
102+
fun workerWithTenantId() {
103+
}
104+
105+
@ProcessEngineWorker(topic = "withoutTenant")
106+
fun worker() {
107+
}
108+
109+
@ProcessEngineWorker(topic = "withEmptyTenantId", tenantId = "")
110+
fun withEmptyTenantId() {
111+
}
73112
}
74113
}
75114
}

0 commit comments

Comments
 (0)