kalix-jvm-sdk
kalix-jvm-sdk copied to clipboard
Accessing currentState from inside Workflow.definition
@max8github reported that he was getting a java.lang.IllegalStateException: Current state is only available when handling a command when accessing the current state from inside the 'Worfklow.definition` method.
I initially considered it a bug because there is no reason for not having the state available at this point. However, I now realise that whenever we access the currentState from inside definiton we can only have the emptyState (currentState == emptyState) because when definition is called nothing has been ever executed.
To give more context on the use case, Max was willing to add a step timeout based on data that is available after the state is initialized.
I realise that this is not possible and we may need to consider a timeout variant that receives a lambda State => Timeout.
cc @aludwiko
@max8github can you describe your use case? I would like to understand the full context.
@aludwiko , as a simplified practical example, let's say your workflow is an airline reservation. The reservation will go in steps through certain states, one of which will be, say, Booked. The workflow at that point, cannot be considered completed, because the reservation could be potentially moved to state Cancelled. You can cancel the reservation up to a certain point in time: certainly not after the plane takes off. That point in time, however, is only known after you initialize the workflow.
You can choose to model that in different ways with a workflow.
Perhaps one is by using pause() on the book step, but you wished you had a way to timeout the pause(), of course, maybe with a Duration argument passed to pause(), say.
Another could be using timeouts, but you wished you could use the current state (containing the departure time) and the current time to set the timeout.
Ok, I get it, yeah both options with pause and with timeout could be interesting.
Related to https://github.com/lightbend/kalix-jvm-sdk/issues/1733
The following 'flow' is possible:
bookstep is executed, transition to something likeset timerstep- the timer step schedules a Kalix timer using the current state and moves to
paused - timer is triggered and calls a method that will close the booking (not possible to cancel anymore), workflow transition to finished
A user can cancel the booking during the paused period. In this case, the Timer becomes obsolete and can be discarded.
The idea with delayed transition is to make the above a built-in feature. Not saying we should do it straightaway, just that we can think about the options for now.