@@ -30,10 +30,10 @@ type userWatcher struct {
30
30
maxWorkers int64
31
31
}
32
32
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
37
37
}
38
38
39
39
// Run runs the watcher.
@@ -51,6 +51,9 @@ func (w *userWatcher) Run() {
51
51
known .UserStatusBlacklisted ,
52
52
known .UserStatusNeedActive ,
53
53
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,
54
57
}
55
58
56
59
wp := workerpool .New (int (w .maxWorkers ))
@@ -62,12 +65,18 @@ func (w *userWatcher) Run() {
62
65
wp .Submit (func () {
63
66
ctx := onexx .NewUserM (context .Background (), user )
64
67
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 {
67
70
log .Errorw (err , "Failed to event user" , "username" , user .Username , "status" , user .Status )
68
71
return
69
72
}
70
73
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
+
71
80
return
72
81
})
73
82
}
0 commit comments