feat: replace http polling with webscoket notify in sd image page

This commit is contained in:
RockYang
2024-02-26 15:45:54 +08:00
parent 668d4c9c64
commit b95dff0751
8 changed files with 189 additions and 124 deletions

View File

@@ -52,6 +52,26 @@ func (p *ServicePool) PushTask(task types.SdTask) {
p.taskQueue.RPush(task)
}
func (p *ServicePool) CheckTaskNotify() {
go func() {
for {
var userId uint
err := p.notifyQueue.LPop(&userId)
if err != nil {
continue
}
client := p.Clients.Get(userId)
if client == nil {
continue
}
err = client.Send([]byte("Task Updated"))
if err != nil {
continue
}
}
}()
}
// HasAvailableService check if it has available mj service in pool
func (p *ServicePool) HasAvailableService() bool {
return len(p.services) > 0

View File

@@ -310,4 +310,7 @@ func (s *Service) callback(data CBReq) {
// restore img_calls
s.db.Model(&model.User{}).Where("id = ? AND img_calls > 0", data.UserId).UpdateColumn("img_calls", gorm.Expr("img_calls + ?", 1))
}
// 发送更新状态信号
s.notifyQueue.RPush(data.UserId)
}