From 2ef07574ae15be50014c25b418864de4e8d60327 Mon Sep 17 00:00:00 2001 From: RockYang Date: Mon, 26 Feb 2024 15:45:54 +0800 Subject: [PATCH] feat: replace http polling with webscoket notify in sd image page --- api/core/app_server.go | 1 + api/handler/sd_handler.go | 11 ++ api/main.go | 1 + api/service/sd/pool.go | 20 +++ api/service/sd/service.go | 3 + web/src/assets/css/task-list.styl | 1 + web/src/views/ImageSd.vue | 272 ++++++++++++++++-------------- web/src/views/admin/ChatList.vue | 4 +- 8 files changed, 189 insertions(+), 124 deletions(-) diff --git a/api/core/app_server.go b/api/core/app_server.go index 84c38cb4..95a3b22b 100644 --- a/api/core/app_server.go +++ b/api/core/app_server.go @@ -155,6 +155,7 @@ func authorizeMiddleware(s *AppServer, client *redis.Client) gin.HandlerFunc { c.Request.URL.Path == "/api/mj/notify" || c.Request.URL.Path == "/api/invite/hits" || c.Request.URL.Path == "/api/sd/jobs" || + c.Request.URL.Path == "/api/sd/client" || strings.HasPrefix(c.Request.URL.Path, "/api/test") || strings.HasPrefix(c.Request.URL.Path, "/api/function/") || strings.HasPrefix(c.Request.URL.Path, "/api/sms/") || diff --git a/api/handler/sd_handler.go b/api/handler/sd_handler.go index 37a0e6e4..aec34b01 100644 --- a/api/handler/sd_handler.go +++ b/api/handler/sd_handler.go @@ -156,6 +156,11 @@ func (h *SdJobHandler) Image(c *gin.Context) { UserId: userId, }) + client := h.pool.Clients.Get(uint(job.UserId)) + if client != nil { + _ = client.Send([]byte("Task Updated")) + } + // update user's img calls h.db.Model(&model.User{}).Where("id = ?", job.UserId).UpdateColumn("img_calls", gorm.Expr("img_calls - ?", 1)) @@ -229,6 +234,7 @@ func (h *SdJobHandler) JobList(c *gin.Context) { func (h *SdJobHandler) Remove(c *gin.Context) { var data struct { Id uint `json:"id"` + UserId uint `json:"user_id"` ImgURL string `json:"img_url"` } if err := c.ShouldBindJSON(&data); err != nil { @@ -249,6 +255,11 @@ func (h *SdJobHandler) Remove(c *gin.Context) { logger.Error("remove image failed: ", err) } + client := h.pool.Clients.Get(data.UserId) + if client != nil { + _ = client.Send([]byte("Task Updated")) + } + resp.SUCCESS(c) } diff --git a/api/main.go b/api/main.go index 3bc16790..cd1e9bda 100644 --- a/api/main.go +++ b/api/main.go @@ -247,6 +247,7 @@ func main() { }), fx.Invoke(func(s *core.AppServer, h *handler.SdJobHandler) { group := s.Engine.Group("/api/sd") + group.Any("client", h.Client) group.POST("image", h.Image) group.GET("jobs", h.JobList) group.POST("remove", h.Remove) diff --git a/api/service/sd/pool.go b/api/service/sd/pool.go index 131b66c1..db7d09c7 100644 --- a/api/service/sd/pool.go +++ b/api/service/sd/pool.go @@ -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 diff --git a/api/service/sd/service.go b/api/service/sd/service.go index 73d3b6ea..4bb9aa71 100644 --- a/api/service/sd/service.go +++ b/api/service/sd/service.go @@ -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) } diff --git a/web/src/assets/css/task-list.styl b/web/src/assets/css/task-list.styl index 7d046fa5..38b5220f 100644 --- a/web/src/assets/css/task-list.styl +++ b/web/src/assets/css/task-list.styl @@ -216,6 +216,7 @@ } } + .remove { display none position absolute diff --git a/web/src/views/ImageSd.vue b/web/src/views/ImageSd.vue index 6f2f1b74..3dbad920 100644 --- a/web/src/views/ImageSd.vue +++ b/web/src/views/ImageSd.vue @@ -307,15 +307,57 @@
-

任务列表

-
- -