temporalio / sdk-php

Temporal PHP SDK
https://php.preview.thundergun.io
MIT License
266 stars 45 forks source link

Workflow-init support #480

Open drewhoskins-temporal opened 2 months ago

drewhoskins-temporal commented 2 months ago

We should have a method that's guaranteed to initialize before signals and updates come in so handlers can use initialized values.

josh-berry commented 1 month ago

see the parent feature issue for how other languages have handled it: https://github.com/temporalio/features/issues/400

roxblnfk commented 3 weeks ago

I'm developing the concept of Workflow Init in the constructor: the user can call the Workflow::getInput() method to obtain input arguments.

    public function __construct() {
        $input = Workflow::getInput()->getValues();
        // ... init code
    }

However, getInput() returns a ValuesInterface, which does not contain type information.

Manually specifying the type for required arguments like Workflow::getInput()->getValue(0, DateTimeInterface::class); is inconvenient. Additionally, new objects will be created instead of reusing existing ones.

My proposal is to add a new method Workflow::getTypedInput(), which will return the same arguments that will be sent to the Workflow Handler after initialization. This way, we can provide a convenient Workflow Init using language mechanisms. At the same time, we still have the flexibility to add a separate InitHandler if needed.

Sushisource commented 1 week ago

@roxblnfk That works for me. If it's possible to allow them to specify the args in their constructor, and pass them in there I would say let's do that (possibly in addition to getTypedInput), possibly by using an annotation on the constructor like we've done for Python. If that's not feasible I think this approach will work just fine too.

drewhoskins-temporal commented 2 days ago

+1 to @Sushisource's comment, if it's doable. It seems somewhat more discoverable and idiomatic.