Duplicates in child workflows

I’d like to prevent duplicates based on a key when launching a child workflow. So if there has been a successful run I’d just like to skip starting it. I’m wondering if this is the right way to go:

try {
    val child = Workflow.newChildWorkflowStub(MyChildWorkflow::class.java, with(ChildWorkflowOptions.Builder()) {
        setWorkflowId("MyChild-{uniqueId}}")
        setWorkflowIdReusePolicy(WorkflowIdReusePolicy.AllowDuplicateFailedOnly)
        build()
    })
    child.run(args)
} catch (e: DuplicateWorkflowException) {

}

I would also assume this is free from race conditions in comparison with implementing the unique constraint with activities

Just to follow upp, this method does not seem to work.

I am getting an com.uber.cadence.workflow.StartChildWorkflowFailedException which fails the workflow.

What is the recommended approach for discarding duplicate started workflows?

I believe Cadence Java client had a bug that caused the parent to fail without hitting the catch block that surrounds the child invocation. Is it what happens to your workflow?

I’ve checked and it works fine in Temporal Java SDK. The following code snippet returns “Already running” if child with this ID already was executed:

      GreetingChild child =
          Workflow.newChildWorkflowStub(
              GreetingChild.class,
              ChildWorkflowOptions.newBuilder()
                  .setWorkflowId("MyChild")
                  .setWorkflowIdReusePolicy(
                      WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE)
                  .build());
      try {
        return child.composeGreeting("Hello", name);
      } catch (ChildWorkflowFailure e) {
        if (e.getCause() instanceof WorkflowExecutionAlreadyStarted) {
          return "Already running";
        }
        throw e;
      }
1 Like