fusenapi/server/websocket/internal/logic/ws_render_image_logic.go
laodaming a106082364 fix
2023-08-07 10:48:14 +08:00

70 lines
2.2 KiB
Go

package logic
import (
"encoding/json"
"fusenapi/constants"
"fusenapi/server/websocket/internal/types"
"fusenapi/utils/hash"
"github.com/zeromicro/go-zero/core/logx"
)
// 云渲染属性
type renderProperty struct {
renderImageTask map[string]string //需要渲染的图片任务 key是taskId val 是renderId
renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道
}
// 渲染任务新增移除的控制通道的数据
type renderImageControlChanItem struct {
Option int // 0删除 1添加
TaskId string //map的key
RenderId string // map的val
}
// 渲染请求数据处理发送云渲染服务处理
func (w *wsConnectItem) SendToCloudRender(data []byte) {
var renderImageData types.RenderImageReqMsg
if err := json.Unmarshal(data, &renderImageData); err != nil {
w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:"+string(data))
logx.Error("invalid format of websocket render image message", err)
return
}
logx.Info("收到请求云渲染图片数据:", renderImageData)
//把需要渲染的图片任务加进去
select {
case <-w.closeChan: //连接关闭了
return
default:
//加入渲染任务
taskId := hash.JsonHashKey(renderImageData.RenderData)
w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
Option: 1, //0删除 1添加
TaskId: taskId,
RenderId: renderImageData.RenderId,
}
//发送给对应的流水线组装数据
if err := w.rabbitMq.SendMsg(constants.RABBIT_MQ_ASSEMBLE_RENDER_DATA, data); err != nil {
logx.Error("发送渲染任务数据到MQ失败:", string(data), "err:", err)
return
}
logx.Info("发送渲染数据到rabbitmq成功:", string(data))
}
}
// 操作连接中渲染任务的增加/删除
func (w *wsConnectItem) operationRenderTask() {
for {
select {
case <-w.closeChan:
return
case data := <-w.renderProperty.renderImageTaskCtlChan:
switch data.Option {
case 0: //删除任务
delete(w.renderProperty.renderImageTask, data.TaskId)
case 1: //新增任务
w.renderProperty.renderImageTask[data.TaskId] = data.RenderId
}
}
}
}