This commit is contained in:
laodaming 2023-08-09 16:06:40 +08:00
parent 84dc60be49
commit 64536beae6
3 changed files with 27 additions and 21 deletions

View File

@ -49,26 +49,6 @@ func (m *MqConsumerRenderAssemble) Run(ctx context.Context, data []byte) error {
return errors.New("allmodels is nil!!") return errors.New("allmodels is nil!!")
} }
rabbitmq := initalize.RabbitMqHandle{} rabbitmq := initalize.RabbitMqHandle{}
//查询有没有缓存的资源
resource, err := allmodels.FsResource.FindOneById(ctx, parseInfo.TaskId)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error("failed to find render resource:", err)
return err
}
} else {
//有数据则直接返回
cacheData := websocket_data.RenderImageNotify{
TaskId: parseInfo.TaskId,
Image: *resource.ResourceUrl,
}
d, _ := json.Marshal(cacheData)
if err = rabbitmq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d); err != nil {
logx.Error("failed to send cache render resource to queue:RABBIT_MQ_RENDER_RESULT_DATA")
return err
}
return nil
}
timeSearchBegin := time.Now().UnixMilli() timeSearchBegin := time.Now().UnixMilli()
//获取模板 //获取模板
templateInfo, err := allmodels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId)) templateInfo, err := allmodels.FsProductTemplateV2.FindOneByProductIdTagIdWithSizeTable(ctx, parseInfo.RenderData.ProductId, fmt.Sprintf("%d", parseInfo.RenderData.TemplateTagId))

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fusenapi/constants" "fusenapi/constants"
"fusenapi/initalize" "fusenapi/initalize"
"fusenapi/model/gmodel"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/id_generator" "fusenapi/utils/id_generator"
"fusenapi/utils/websocket_data" "fusenapi/utils/websocket_data"
@ -64,7 +65,9 @@ var (
// 每个连接的连接基本属性 // 每个连接的连接基本属性
type wsConnectItem struct { type wsConnectItem struct {
conn *websocket.Conn //websocket的连接 conn *websocket.Conn //websocket的连接
ctx context.Context
rabbitMq *initalize.RabbitMqHandle rabbitMq *initalize.RabbitMqHandle
allModels *gmodel.AllModelsGen
closeChan chan struct{} //ws连接关闭chan closeChan chan struct{} //ws连接关闭chan
isClose bool //是否已经关闭 isClose bool //是否已经关闭
uniqueId uint64 //ws连接唯一标识 uniqueId uint64 //ws连接唯一标识
@ -110,7 +113,9 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
uniqueId := websocketIdGenerator.Get() uniqueId := websocketIdGenerator.Get()
ws := wsConnectItem{ ws := wsConnectItem{
conn: conn, conn: conn,
ctx: l.ctx,
rabbitMq: l.svcCtx.RabbitMq, rabbitMq: l.svcCtx.RabbitMq,
allModels: l.svcCtx.AllModels,
uniqueId: uniqueId, uniqueId: uniqueId,
closeChan: make(chan struct{}, 1), closeChan: make(chan struct{}, 1),
inChan: make(chan []byte, 1000), inChan: make(chan []byte, 1000),

View File

@ -2,10 +2,12 @@ package logic
import ( import (
"encoding/json" "encoding/json"
"errors"
"fusenapi/constants" "fusenapi/constants"
"fusenapi/utils/hash" "fusenapi/utils/hash"
"fusenapi/utils/websocket_data" "fusenapi/utils/websocket_data"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
) )
// 云渲染属性 // 云渲染属性
@ -33,8 +35,27 @@ func (w *wsConnectItem) renderImage(data []byte) {
//用户id赋值 //用户id赋值
renderImageData.RenderData.UserId = w.userId renderImageData.RenderData.UserId = w.userId
renderImageData.RenderData.GuestId = w.guestId renderImageData.RenderData.GuestId = w.guestId
//把需要渲染的图片任务加进去 //生成任务id
taskId := hash.JsonHashKey(renderImageData.RenderData) taskId := hash.JsonHashKey(renderImageData.RenderData)
//查询有没有缓存的资源,有就返回######################
resource, err := w.allModels.FsResource.FindOneById(w.ctx, taskId)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error("failed to find render resource:", err)
return
}
} else {
//返回给客户端
b := w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{
RenderId: renderImageData.RenderId,
Image: *resource.ResourceUrl,
})
//发送数据到out chan
w.sendToOutChan(b)
return
}
//###########################################
//把需要渲染的图片任务加进去
w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
Option: 1, //0删除 1添加 Option: 1, //0删除 1添加
TaskId: taskId, TaskId: taskId,