Skip to content

Commit 2a3a6d9

Browse files
committed
refactor: optimize fsm code
1 parent 5c19ea5 commit 2a3a6d9

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

internal/nightwatch/watcher/user/event.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,8 @@ func NewUserEventAfterEvent(store store.Interface) fsm.Callback {
9595
user.Status = event.FSM.Current()
9696
if err := store.UserCenter().Users().Update(ctx, user); err != nil {
9797
log.Errorw(err, "Failed to update status into database", "event", event.Event)
98-
}
99-
100-
if user.Status == known.UserStatusDeleted {
101-
// We can add some lark card here in the future.
102-
log.Infow("Finish to handle user", "event", event.Event, "username", user.Username)
98+
event.Err = err
99+
return
103100
}
104101
}
105102
}

internal/nightwatch/watcher/user/fsm.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ func NewFSM(initial string, w *userWatcher) *fsm.FSM {
3232
// Define need events.
3333
{Name: known.UserStatusNeedActive, Src: []string{known.UserStatusNeedActive}, Dst: known.UserStatusActived},
3434
{Name: known.UserStatusNeedDisable, Src: []string{known.UserStatusNeedDisable}, Dst: known.UserStatusDisabled},
35-
// After disabling the user, they can be deleted, and the FSM will automatically transition to the next deleted state.
36-
// I have decided not to delete the user in the code, so the state transition here is commented out.
37-
// {Name: known.UserStatusDisabled, Src: []string{known.UserStatusDisabled}, Dst: known.UserStatusDeleted},
35+
{Name: known.UserStatusDisabled, Src: []string{known.UserStatusDisabled}, Dst: known.UserStatusDeleted},
3836
},
3937
fsm.Callbacks{
40-
known.UserStatusActived: NewActiveUserCallback(w.store),
38+
known.UserStatusActived: NewActiveUserCallback(w.store),
4139
known.UserStatusDisabled: NewDisableUserCallback(w.store),
4240
known.UserStatusDeleted: NewDeleteUserCallback(w.store),
4341
// log, alert, save to stoer, etc for all events.
@@ -46,3 +44,12 @@ func NewFSM(initial string, w *userWatcher) *fsm.FSM {
4644
},
4745
)
4846
}
47+
48+
func filterFSMError(err error) error {
49+
switch err.(type) {
50+
case fsm.NoTransitionError:
51+
return nil
52+
default:
53+
return err
54+
}
55+
}

internal/nightwatch/watcher/user/watcher.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ type userWatcher struct {
3030
maxWorkers int64
3131
}
3232

33-
// User is a struct that represents a user finite state machine.
34-
type User struct {
35-
*model.UserM
36-
*fsm.FSM
33+
// UserStateMachine is a struct that represents a user finite state machine.
34+
type UserStateMachine struct {
35+
UserM *model.UserM
36+
FSM *fsm.FSM
3737
}
3838

3939
// Run runs the watcher.
@@ -51,6 +51,9 @@ func (w *userWatcher) Run() {
5151
known.UserStatusBlacklisted,
5252
known.UserStatusNeedActive,
5353
known.UserStatusNeedDisable,
54+
// After disabling the user, they can be deleted, and the FSM will automatically transition to the next deleted state.
55+
// I have decided not to delete the user in the code, so the state transition here is commented out.
56+
// known.UserStatusDisabled,
5457
}
5558

5659
wp := workerpool.New(int(w.maxWorkers))
@@ -62,12 +65,18 @@ func (w *userWatcher) Run() {
6265
wp.Submit(func() {
6366
ctx := onexx.NewUserM(context.Background(), user)
6467

65-
u := &User{UserM: user, FSM: NewFSM(user.Status, w)}
66-
if err := u.Event(ctx, user.Status); err != nil {
68+
usm := &UserStateMachine{UserM: user, FSM: NewFSM(user.Status, w)}
69+
if err := usm.FSM.Event(ctx, user.Status); err != nil {
6770
log.Errorw(err, "Failed to event user", "username", user.Username, "status", user.Status)
6871
return
6972
}
7073

74+
// When the entire state machine reaches the final state, print a message and send a notification.
75+
if usm.FSM.Current() == known.UserStatusDeleted {
76+
// We can add some lark card here in the future.
77+
log.Infow("Finish to handle user", "username", usm.UserM.Username)
78+
}
79+
7180
return
7281
})
7382
}

0 commit comments

Comments
 (0)