refactor: refactor stable diffusion service, add service pool support

This commit is contained in:
RockYang
2023-12-14 16:48:54 +08:00
parent 10ba1430f9
commit d2991e60b6
14 changed files with 266 additions and 253 deletions

View File

@@ -60,7 +60,7 @@ func (p *ServicePool) PushTask(task types.MjTask) {
p.taskQueue.RPush(task)
}
// HasAvailableService check if has available mj service in pool
// HasAvailableService check if it has available mj service in pool
func (p *ServicePool) HasAvailableService() bool {
return len(p.services) > 0
}

View File

@@ -2,7 +2,6 @@ package mj
import (
"chatplus/core/types"
"chatplus/service"
"chatplus/service/oss"
"chatplus/store"
"chatplus/store/model"
@@ -24,7 +23,6 @@ type Service struct {
handledTaskNum int32 // already handled task number
taskStartTimes map[int]time.Time // task start time, to check if the task is timeout
taskTimeout int64
snowflake *service.Snowflake
}
func NewService(name string, queue *store.RedisQueue, maxTaskNum int32, timeout int64, db *gorm.DB, client *Client, manager *oss.UploaderManager, config *types.AppConfig) *Service {
@@ -127,6 +125,12 @@ func (s *Service) Notify(data CBReq) {
job.Hash = data.Image.Hash
job.OrgURL = data.Image.URL
res = s.db.Updates(&job)
if res.Error != nil {
logger.Error("error with update job: ", res.Error)
return
}
// upload image
if data.Status == Finished {
imgURL, err := s.uploadManager.GetUploadHandler().PutImg(data.Image.URL, true)
@@ -135,12 +139,7 @@ func (s *Service) Notify(data CBReq) {
return
}
job.ImgURL = imgURL
}
res = s.db.Updates(&job)
if res.Error != nil {
logger.Error("error with update job: ", res.Error)
return
s.db.Updates(&job)
}
if data.Status == Finished {