qmuntal / stateless

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

External state storage breaking change #20

Closed suciuvlad closed 3 years ago

suciuvlad commented 3 years ago

I've been using version 1.1.6 of stateless and i noticed that my tests fail after upgrading to 1.1.7. It seems that the callback for writing the state value is no longer being executed after OnEntry.

machine := stateless.NewStateMachineWithExternalStorage(func(_ context.Context) (stateless.State, error) {
  return myState.Value, nil
}, func(_ context.Context, state stateless.State) error {
  // this callback is not being executed anymore after onEntry so that i'm able to persist the state to db
  db.Save(myState)
  return nil
}, stateless.FiringQueued)

.Configure(entity.StateCompleted).OnEntry(func(c context.Context, args ...interface{}) error {
  myState.Value = "newValue"
})

Sequence of events with 1.1.6:

Sequence of events with 1.1.7:

I was wondering if this is an expected breaking change.

qmuntal commented 3 years ago

This breakage was not intended. I did a modification to only call the set state callback if the OnEntry callback changed the state, which could have introduced a bug in your workflow.

I'll try to fix it soon.

qmuntal commented 3 years ago

@suciuvlad I'll amend my last comment. Directly changing the state inside any callback other than the stateMutator function is not supported, and in general is not a good practice. The state machine should always change state by calling Fire() with an specific trigger.