Skip to content

Commit

Permalink
fix the update/create enrichment, fix #322
Browse files Browse the repository at this point in the history
  • Loading branch information
zambrovski committed Apr 28, 2021
1 parent 3ab2b79 commit cf8b661
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.holunda.camunda.taskpool.sender.accumulator.EngineTaskCommandAccumulat
import io.holunda.camunda.taskpool.sender.accumulator.ProjectingCommandAccumulator
import io.holunda.camunda.taskpool.sender.gateway.*
import org.camunda.bpm.engine.RuntimeService
import org.camunda.bpm.engine.TaskService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
Expand Down Expand Up @@ -51,9 +52,9 @@ class TaskCollectorConfiguration(
*/
@Bean
@ConditionalOnExpression("'\${camunda.taskpool.collector.task.enricher.type}' != 'custom'")
fun processVariablesEnricher(runtimeService: RuntimeService, filter: ProcessVariablesFilter, correlator: ProcessVariablesCorrelator): VariablesEnricher =
fun processVariablesEnricher(runtimeService: RuntimeService, taskService: TaskService, filter: ProcessVariablesFilter, correlator: ProcessVariablesCorrelator): VariablesEnricher =
when (properties.task.enricher.type) {
TaskCollectorEnricherType.processVariables -> ProcessVariablesTaskCommandEnricher(runtimeService, filter, correlator)
TaskCollectorEnricherType.processVariables -> ProcessVariablesTaskCommandEnricher(runtimeService, taskService, filter, correlator)
TaskCollectorEnricherType.no -> EmptyTaskCommandEnricher()
else -> throw IllegalStateException("Could not initialize task enricher, used unknown ${properties.task.enricher.type} type.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import io.holunda.camunda.taskpool.api.task.*
import io.holunda.camunda.taskpool.callInProcessEngineContext
import mu.KLogging
import org.camunda.bpm.engine.RuntimeService
import org.camunda.bpm.engine.TaskService
import org.camunda.bpm.engine.variable.VariableMap
import org.camunda.bpm.engine.variable.Variables.createVariables

/**
Expand All @@ -13,23 +15,51 @@ import org.camunda.bpm.engine.variable.Variables.createVariables
*/
open class ProcessVariablesTaskCommandEnricher(
private val runtimeService: RuntimeService,
private val taskService: TaskService,
private val processVariablesFilter: ProcessVariablesFilter,
private val processVariablesCorrelator: ProcessVariablesCorrelator,
) : VariablesEnricher {

companion object : KLogging()

override fun <T : TaskIdentityWithPayloadAndCorrelations> enrich(command: T): T {

val variablesTyped = callInProcessEngineContext(command.isHistoric()) {
val execution = runtimeService.createExecutionQuery().executionId(command.sourceReference.executionId).singleResult()
if (execution != null) {
runtimeService.getVariablesTyped(command.sourceReference.executionId)
} else {
logger.debug { "ENRICHER-004: Could not enrich variables from running execution ${command.sourceReference.executionId}, since it doesn't exist (anymore)." }
createVariables()
/**
* Retrieves typed variables to enrich current command.
* @param command command to enrich.
* @return variable map.
*/
open fun <T : TaskIdentityWithPayloadAndCorrelations> getTypeVariables(command: T): VariableMap {
return if (command.isHistoric()) {
// Task updated
// This is a historic command which is processed from a command context listener on command context close. Accessing variables at this point will try to add
// another command context listener (some camunda feature about updating mutable variables without explicitly saving them), which in turn causes a
// ConcurrentModificationException.
// We work around this by opening a new context.
// Caution: This will only work if the task is already flushed to the database, e.g. if it was created in a previous transaction.
// It will also see only the state of the variables that is flushed to the database
callInProcessEngineContext(newContext = true) {
val task = taskService.createTaskQuery().taskId(command.id).singleResult()
if (task != null) {
taskService.getVariablesTyped(command.id)
} else {
val execution = runtimeService.createExecutionQuery().executionId(command.sourceReference.executionId).singleResult()
if (execution != null) {
runtimeService.getVariablesTyped(command.sourceReference.executionId)
} else {
logger.debug { "ENRICHER-004: Could not enrich variables from running execution ${command.sourceReference.executionId}, since it doesn't exist (anymore)." }
createVariables()
}
}
}
} else {
// Create task
taskService.getVariablesTyped(command.id)
}
}

override fun <T : TaskIdentityWithPayloadAndCorrelations> enrich(command: T): T {

// load variables typed
val variablesTyped = getTypeVariables(command)

// Payload enrichment
command.payload.putAllTyped(
Expand Down

0 comments on commit cf8b661

Please sign in to comment.