Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct state machine in a particular state

I am using Spring Statemachine to provide a user's workflow. I need to persist the state changes so that user's state is not lost across restarts. Now, I can do this based on the examples provided, however one thing missing is how to recreate the state if a crash does occur.

Basically, I want to create the state machine and tell it set itself to the last state it had before the crash and copy any extended state variables from the database. Is there a way to do this?

like image 879
teknokrat Avatar asked Dec 09 '22 01:12

teknokrat


2 Answers

Maybe this can help you:

stateMachine
                .getStateMachineAccessor()
                .doWithAllRegions(access -> {
                    access.resetStateMachine(new DefaultStateMachineContext<>({ResetState}, null, null, null, null));
                });
stateMachine.start();
stateMachine.sendEvent({NewEventFromResetState});
like image 131
Tkk Avatar answered Dec 11 '22 08:12

Tkk


Persist sample is using PersistStateMachineHandler recipe to update stuff in a db when state machine is transitioning between states. One important thing to remember in this recipe is that it is using interceptor instead of listener to hook into state changes. If db update is done within an interceptor callback, in case of error/exception, transition in a state machine is denied, while if one would use listener error would cause state machine and db become inconsistent between each others.

One other thing is that this recipe allows to reset machine state into a particular state and then continue from there.

It really doesn't matter if new machine is created for every update if user doesn't care about speed and garbage. State machine instantiation is relatively expensive so simply using one instance and then reseting its state is relatively light operation.

How you interact with db from state machine hooks is very low level at this moment because you need to do everything manually in terms of how to interact with db. There's no automatic tweaks currently because we simply don't know what's in db and how rows would be updated.

like image 37
Janne Valkealahti Avatar answered Dec 11 '22 09:12

Janne Valkealahti