qmuntal / stateless

Go library for creating finite state machines
BSD 2-Clause "Simplified" License
898 stars 47 forks source link

How to stop FSM execution #77

Open paveq opened 2 months ago

paveq commented 2 months ago

I'm creating workflow execution using the stateless FSM behind the scene. In the execution I need to keep track of number of transitions, and halt the execution if that count is ever exceeded, in order to prevent runaway situations.

I have a struct to keep track of number of transitions happened in the FSM:

type ResetOperation struct {
    fsm                     *stateless.StateMachine
    transitionCount int32
}

To increment transitionCount I'm using OnTransitioning() handler. However, there seems to be no way to return error from the handler function, and calling fsm.Deactive() will not stop the execution.

I could probably fire trigger "maxTransitionsReached" and have an error state in the FSM, but if I understand correctly, firing the trigger will cause it to be put on queue fireModeQueued, and queue might not be empty.

What is proper way to handle this scenario?