package group_ev import ( "hilo-user/_const" "hilo-user/domain" "hilo-user/domain/event" "hilo-user/myerr" "hilo-user/resource/mysql" ) var groupLeaveListen = new(event.Base) // 离房事件 type GroupLeaveEvent struct { GroupId string UserId mysql.ID ExternalId string } func AddGroupLeaveEventSync(callback func(model *domain.Model, event interface{}) error) { groupLeaveListen.SyncList = append(groupLeaveListen.SyncList, callback) } func AddGroupLeaveEventAsync(callback func(model *domain.Model, event interface{}) error) { groupLeaveListen.AsyncList = append(groupLeaveListen.AsyncList, callback) } func PublishGroupLeaveEvent(model *domain.Model, event interface{}) error { // 执行同步的领域事件 for _, callback := range groupLeaveListen.SyncList { if err := callback(model, event); err != nil { return myerr.WrapErr(err) } } // 执行异步的领域事件 if len(groupLeaveListen.AsyncList) > 0 { go func() { defer _const.CheckGoPanic() for _, callback := range groupLeaveListen.AsyncList { // 异步事件需要用新model,主要是db var newModel = domain.CreateModelContext(model.MyContext) if err := callback(newModel, event); err != nil { model.Log.Errorf("GroupInEvent aysnc fail:%v", err) } } }() } return nil }