sdk-php icon indicating copy to clipboard operation
sdk-php copied to clipboard

[Question] Asynchronous child workflow execution problem

Open Muz4k opened this issue 1 year ago • 2 comments

Hello!

(Sorry, I posted on the forum, but I really need to get a response as soon as possible) I’ve got a problem with child workflow working.

The child workflow is initialized, but the execution does not start

I assume that the problem is due to the fact that it does not have time to start execution - the parent workflow finishes faster.

I found a bestway for java - Best way to create an async child workflow that looks like the one I need

Point 4 says “Wait for the Promise returned by getWorkflowExecution to complete. ”

Can you please tell me how this can be implemented using php-sdk? My code now looks like this (called inside the parent workflow)


Workflow::asyncDetached(
            task: static function (): void {
                $options = ChildWorkflowOptions::new()
                    ->withTaskQueue('task_name')
                    ->withParentClosePolicy(ParentClosePolicy::Abandon);

                $workflow = Workflow::newChildWorkflowStub(
                    class: MyChildWorkflowInterface::class,
                    options: $options,
                );

                $workflow->execute(); // workflow logic
            },
);

Muz4k avatar Dec 03 '24 17:12 Muz4k

Hi. Sorry for the delay. Have you tried using yield to wait promises?

// Here:
yield Workflow::asyncDetached(
    task: static function () {
        $options = ChildWorkflowOptions::new()
            ->withTaskQueue('task_name')
            ->withParentClosePolicy(ParentClosePolicy::Abandon);

        $workflow = Workflow::newChildWorkflowStub(
            class: MyChildWorkflowInterface::class,
            options: $options,
        );

        // And here:
        yield $workflow->execute(); // workflow logic
    },
);

roxblnfk avatar Dec 09 '24 19:12 roxblnfk

I may have misunderstood your question.

If you want to simply run a Child Workflow in a Detached context (for example, after receiving a Cancel) and just complete the parent, UntypedChildWorkflowStub can help you, as it has a start() method.

You can obtain UntypedChildWorkflowStub via Workflow::newUntypedChildWorkflowStub(), and you can wait for the Child Workflow to start like this: yield $untypedStub->start();

roxblnfk avatar Dec 09 '24 19:12 roxblnfk