holunda-io / camunda-bpm-taskpool

Library for pooling user tasks and process related business objects.
https://www.holunda.io/camunda-bpm-taskpool/
Apache License 2.0
68 stars 26 forks source link

Process Variables set by a TaskListener on task creation are not present in the command #792

Closed lbilger closed 1 year ago

lbilger commented 1 year ago

Steps to reproduce

  /**
   * The process is started and waits in a user task. The user task has a task listener that changes some local process variables on create.
   * The create command should contain the local variables.
   */
  @Test
  fun `updates variables with create listener`() {

    // deploy
    driver.deployProcess(
      createUserTaskProcess(
        taskListeners = listOf(
          "create" to "#{setTaskLocalVariables}"
        )
      )
    )

    // start
    val instance = driver.startProcessInstance(variables = Variables.createVariables().putValue("overriddenVariable", "global-value"))
    driver.assertProcessInstanceWaitsInUserTask(instance)
    verify(commandListGateway).sendToGateway(
      listOf(
        createTaskCommand(
          variables = Variables.createVariables()
            .putValue("taskLocalOnlyVariable", "only-value")
            .putValue("overriddenVariable", "local-value")
        )
      )
    )

    verifyNoMoreInteractions(commandListGateway)
  }

with the following task listener implementation:


    /**
     * A task listener that sets some local variables.
     */
    @Bean
    fun setTaskLocalVariables() = TaskListener { delegateTask ->
      delegateTask.setVariableLocal("taskLocalOnlyVariable", "only-value")
      delegateTask.setVariableLocal("overriddenVariable", "local-value")
    }

Expected behaviour

The local variables set by the task listener should be visible in the command.

Actual behaviour

They are not.

This is due to the workaround for historic commands in TaskVariableLoader. The variables are loaded in a new context, which does not see the changed variables.

We use this pattern to override process variables for a specific task when we want the payload of one task to be different from another task's payload.

zambrovski commented 1 year ago

Could you please check what kind of sender you are using? tx or tx-job?

lbilger commented 1 year ago

We are using the tx sender.

zambrovski commented 1 year ago

I could reproduce the problem and found a work-around to your problem. For now, I see no way to fix this bug (no matter what sender you are using), but you can use a different listener to modify your variables.

I added tests to the collector test suite demonstrating it and updated the docs. Please check my changes introduced in e2a8d7d and also check the warning at the end of the Enrichment Chapter

zambrovski commented 1 year ago

I'll mark this issue with WONT since a workaround is present and I have no smart idea how to implement it in a better way. If you have an idea - please don't hesitate to provide me a hint.