kalix-jvm-sdk icon indicating copy to clipboard operation
kalix-jvm-sdk copied to clipboard

Accessing currentState from inside Workflow.definition

Open octonato opened this issue 2 years ago • 4 comments

@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

octonato avatar Jul 18 '23 10:07 octonato

@max8github can you describe your use case? I would like to understand the full context.

aludwiko avatar Jul 18 '23 10:07 aludwiko

@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.

max8github avatar Jul 19 '23 04:07 max8github

Ok, I get it, yeah both options with pause and with timeout could be interesting.

aludwiko avatar Jul 19 '23 08:07 aludwiko

Related to https://github.com/lightbend/kalix-jvm-sdk/issues/1733

The following 'flow' is possible:

  • book step is executed, transition to something like set timer step
  • 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.

octonato avatar Jul 19 '23 09:07 octonato