|
@@ -6,6 +6,7 @@ import (
|
|
|
"eta/eta_api/models"
|
|
|
"eta/eta_api/models/data_manage/edb_inspection"
|
|
|
"eta/eta_api/services/data"
|
|
|
+ edb_monitor "eta/eta_api/services/edb_monitor"
|
|
|
"eta/eta_api/utils"
|
|
|
"fmt"
|
|
|
"runtime"
|
|
@@ -221,44 +222,123 @@ func DealEdbInspectionMessageTest(adminId int) {
|
|
|
}()
|
|
|
}
|
|
|
|
|
|
-func AutoCheckInspectionMessageList() (err error) {
|
|
|
- defer func() {
|
|
|
- if err != nil {
|
|
|
- utils.FileLog.Error("巡检信息发送失败,err:%s", err.Error())
|
|
|
- }
|
|
|
- }()
|
|
|
+func AutoCheckWebsocketMessageList() {
|
|
|
ticker := time.NewTicker(time.Second * 10)
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
for {
|
|
|
select {
|
|
|
case <-ticker.C:
|
|
|
- err = autoCheckInspectionMessageList()
|
|
|
+ // 获取活跃连接
|
|
|
+ admins := make([]int, 0)
|
|
|
+ for adminId, conn := range global.AdminWebSocketConnMap {
|
|
|
+ if conn == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ admins = append(admins, adminId)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有活跃连接,继续等待下一个tick
|
|
|
+ if len(admins) == 0 {
|
|
|
+ //utils.FileLog.Info("当前没有活跃的WebSocket连接")
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 并发处理不同类型的消息
|
|
|
+ var wg sync.WaitGroup
|
|
|
+ wg.Add(2)
|
|
|
+
|
|
|
+ // 处理巡检消息
|
|
|
+ go func() {
|
|
|
+ defer wg.Done()
|
|
|
+ if err := AutoCheckInspectionMessageList(admins); err != nil {
|
|
|
+ utils.FileLog.Error("处理巡检消息失败: %v", err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 处理监控消息
|
|
|
+ go func() {
|
|
|
+ defer wg.Done()
|
|
|
+ if err := edb_monitor.AutoCheckMonitorMessageList(admins); err != nil {
|
|
|
+ utils.FileLog.Error("处理监控消息失败: %v", err)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 等待所有消息处理完成
|
|
|
+ wg.Wait()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-func autoCheckInspectionMessageList() (err error) {
|
|
|
+
|
|
|
+func AutoCheckInspectionMessageList(admins []int) (err error) {
|
|
|
+ //utils.FileLog.Info("检查是否有巡检信息")
|
|
|
+ // 设置redis缓存,防止消息重复处理
|
|
|
+ cacheKey := fmt.Sprintf("%s", utils.CACHE_EDB_INSPECTION_MESSAGE)
|
|
|
+ if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
|
|
|
+ err = fmt.Errorf("系统处理中,请稍后重试!")
|
|
|
+ utils.FileLog.Error("巡检信息检查失败,err:%s", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
defer func() {
|
|
|
- if err != nil {
|
|
|
- utils.FileLog.Error("巡检信息发送失败,err:%s", err.Error())
|
|
|
- }
|
|
|
+ _ = utils.Rc.Delete(cacheKey)
|
|
|
}()
|
|
|
- utils.FileLog.Info("检查是否有巡检信息")
|
|
|
- admins := make([]int, 0)
|
|
|
- for adminId, conn := range global.AdminWebSocketConnMap {
|
|
|
- if conn == nil {
|
|
|
- continue
|
|
|
+ messageList, er := edb_inspection.GetUnreadInspectionMessageList(admins)
|
|
|
+ if er != nil {
|
|
|
+ err = fmt.Errorf("获取巡检信息历史失败,err:%s", er.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ readList := make([]int64, 0)
|
|
|
+ for _, msg := range messageList {
|
|
|
+ adminId := int(msg.AdminId)
|
|
|
+ respData, er := data.SendInspectionMessages(adminId, msg)
|
|
|
+ if er != nil {
|
|
|
+ utils.FileLog.Error("巡检信息发送失败,err:%s, adminId:%d", er.Error(), adminId)
|
|
|
+ } else {
|
|
|
+ resp := models.WebsocketMessageResponse{
|
|
|
+ MessageType: 1,
|
|
|
+ Data: respData,
|
|
|
+ }
|
|
|
+ conn := global.AdminWebSocketConnMap[int(msg.AdminId)]
|
|
|
+ if conn == nil {
|
|
|
+ utils.FileLog.Error("巡检信息发送失败,连接已断开, adminId:%d", adminId)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ message, er := json.Marshal(resp)
|
|
|
+ if er != nil {
|
|
|
+ utils.FileLog.Error("巡检信息发送失败,err:%s, adminId:%d", er.Error(), adminId)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ok := conn.Send(message)
|
|
|
+ if !ok {
|
|
|
+ utils.FileLog.Error("巡检信息发送失败,err:%s, adminId:%d", adminId)
|
|
|
+ }
|
|
|
}
|
|
|
- admins = append(admins, adminId)
|
|
|
+ readList = append(readList, msg.MessageId)
|
|
|
}
|
|
|
- if len(admins) == 0 {
|
|
|
+
|
|
|
+ err = edb_inspection.SetEdbInspectionMessageRead(readList)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("巡检信息已读失败,err:%s", err.Error())
|
|
|
return
|
|
|
}
|
|
|
- messageList, er := edb_inspection.GetUnreadInspectionMessageList(admins)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// 弃用
|
|
|
+func AutoCheckInspectionMessageListByAdminId(adminId int) (err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error("巡检信息发送失败,err:%s", err.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ messageList, er := edb_inspection.GetUnreadInspectionMessageListByAdminId(adminId)
|
|
|
if er != nil {
|
|
|
err = fmt.Errorf("获取巡检信息历史失败,err:%s", er.Error())
|
|
|
return
|
|
|
}
|
|
|
+ if len(messageList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
readList := make([]int64, 0)
|
|
|
for _, msg := range messageList {
|
|
|
adminId := int(msg.AdminId)
|
|
@@ -270,7 +350,7 @@ func autoCheckInspectionMessageList() (err error) {
|
|
|
MessageType: 1,
|
|
|
Data: respData,
|
|
|
}
|
|
|
- conn := global.AdminWebSocketConnMap[int(msg.AdminId)]
|
|
|
+ conn := global.AdminWebSocketConnMap[adminId]
|
|
|
if conn == nil {
|
|
|
utils.FileLog.Error("巡检信息发送失败,连接已断开, adminId:%d", adminId)
|
|
|
return
|
|
@@ -287,7 +367,6 @@ func autoCheckInspectionMessageList() (err error) {
|
|
|
}
|
|
|
readList = append(readList, msg.MessageId)
|
|
|
}
|
|
|
-
|
|
|
err = edb_inspection.SetEdbInspectionMessageRead(readList)
|
|
|
if err != nil {
|
|
|
err = fmt.Errorf("巡检信息已读失败,err:%s", err.Error())
|