fusenapi/server/websocket/internal/logic/rendernotifylogic.go

137 lines
4.7 KiB
Go
Raw Normal View History

2023-07-25 09:10:50 +00:00
package logic
2023-08-23 07:08:45 +00:00
//云渲染回调
2023-07-25 09:10:50 +00:00
import (
2023-08-21 05:19:12 +00:00
"context"
2023-09-13 05:59:01 +00:00
"encoding/json"
2023-09-13 02:42:33 +00:00
"fmt"
2023-08-21 08:19:30 +00:00
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
2023-08-07 04:31:31 +00:00
"fusenapi/utils/auth"
2023-07-25 09:10:50 +00:00
"fusenapi/utils/basic"
2023-08-09 06:49:15 +00:00
"fusenapi/utils/file"
2023-09-13 02:42:33 +00:00
"fusenapi/utils/websocket_data"
2023-08-21 08:19:30 +00:00
"time"
2023-07-25 09:10:50 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
type RenderNotifyLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RenderNotifyLogic {
return &RenderNotifyLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *RenderNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *RenderNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
2023-08-07 04:31:31 +00:00
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-08-25 03:06:32 +00:00
unityRenderEndTime := time.Now().UTC().UnixMilli()
2023-11-03 08:17:05 +00:00
logx.Info("收到unity返回的渲染消息")
2023-09-13 05:59:01 +00:00
//解析数据
var info websocket_data.ToUnityIdStruct
if err := json.Unmarshal([]byte(req.TaskId), &info); err != nil {
logx.Error("解析taskId错误")
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "failed to parse param taskId !!!!")
2023-09-04 08:54:55 +00:00
}
2023-11-02 07:07:38 +00:00
//发出去的时间必须是大于服务启动的时间才参与统计
2023-11-02 04:15:52 +00:00
if info.RenderBeginTime > serverStartTime {
2023-11-02 04:25:51 +00:00
//logx.Info("任务时间:", info.RenderBeginTime, "服务器启动时间:", serverStartTime)
2023-11-02 04:15:52 +00:00
//统计unity处理数
2023-11-02 08:00:49 +00:00
decreaseUnityRequestCount(info.UserId, info.GuestId)
2023-11-02 04:15:52 +00:00
if req.Code != 0 {
//统计unity失败处理数
2023-11-02 08:00:49 +00:00
increaseUnityErrorCount(info.UserId, info.GuestId)
2023-11-02 04:15:52 +00:00
}
}
2023-09-04 08:54:55 +00:00
//重新赋值(很重要)
2023-09-13 05:59:01 +00:00
wid := info.Wid
2023-10-11 06:19:22 +00:00
requestId := info.RequestId
2023-09-13 05:59:01 +00:00
unityRenderBeginTime := info.RenderBeginTime
2023-10-08 09:04:37 +00:00
//获取连接
value, wsConnectOk := mapConnPool.Load(wid)
2023-11-03 08:13:53 +00:00
var ws wsConnectItem
if wsConnectOk {
ws = value.(wsConnectItem)
}
2023-10-08 09:04:37 +00:00
if req.Code == 0 { //渲染成功
//上传文件
var upload = file.Upload{
Ctx: l.ctx,
MysqlConn: l.svcCtx.MysqlConn,
AwsSession: l.svcCtx.AwsSession,
}
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
Source: "unity cloud render",
FileHash: info.TaskId,
FileData: req.Image,
Metadata: "",
UploadBucket: 1,
ApiType: 2,
2023-11-02 08:00:49 +00:00
UserId: info.UserId,
GuestId: info.GuestId,
2023-10-08 09:04:37 +00:00
FileByte: nil,
})
if err != nil {
logx.Error("渲染回调上传文件失败:", err)
2023-11-03 08:13:53 +00:00
if ws.conn != nil {
2023-11-03 08:07:11 +00:00
//发送错误信息给前端
ws.renderErrResponse(requestId, info.TemplateTag, info.TaskId, "unity图片上传错误", 0, 0, 0, 0, 0, 0, 0)
}
2023-10-08 09:04:37 +00:00
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image")
}
uploadUnityRenderImageTakesTime := time.Now().UTC().UnixMilli() - unityRenderEndTime
2023-11-13 02:35:51 +00:00
//转换unity真实处理时间
duration, err := time.Parse("15:04:05.9999999", req.CostTime)
if err != nil {
2023-11-22 10:17:52 +00:00
logx.Error("解析时间错误:", err)
2023-11-13 02:35:51 +00:00
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "转换unity时间错误")
}
// 将时间对象转换为毫秒数
2023-11-22 10:17:52 +00:00
unityRealTakesTime := duration.Nanosecond()/1e6 + duration.Second()*1000 + duration.Minute()*6000 + duration.Hour()*3600000
2023-11-03 08:13:53 +00:00
if ws.conn != nil {
2023-10-08 09:04:37 +00:00
//发送到出口
ws.sendRenderResultData(websocket_data.RenderImageRspMsg{
2023-10-11 06:19:22 +00:00
RequestId: requestId,
Image: uploadRes.ResourceUrl,
2023-11-02 02:10:22 +00:00
RenderProcessTime: &websocket_data.RenderProcessTime{
2023-10-08 09:04:37 +00:00
UnityRenderTakesTime: fmt.Sprintf("%dms", unityRenderEndTime-unityRenderBeginTime),
UploadUnityRenderImageTakesTime: fmt.Sprintf("%dms", uploadUnityRenderImageTakesTime),
2023-11-22 10:17:52 +00:00
UnityRealTakesTime: fmt.Sprintf("%dms", unityRealTakesTime),
2023-10-08 09:04:37 +00:00
},
})
logx.Info("渲染回调成功,渲染结果图片为:", uploadRes.ResourceUrl)
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
2023-10-10 06:15:43 +00:00
logx.Info("渲染成功但找不到ws连接")
2023-10-08 09:04:37 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "successbut websocket connect not found")
2023-08-09 06:49:15 +00:00
}
2023-10-08 09:04:37 +00:00
//渲染失败走下面
2023-11-03 08:13:53 +00:00
if ws.conn != nil {
2023-10-08 09:04:37 +00:00
//发送错误信息给前端
2023-10-27 09:46:24 +00:00
ws.renderErrResponse(requestId, info.TemplateTag, info.TaskId, "unity云渲染错误:"+req.Msg, 0, 0, 0, 0, 0, 0, 0)
2023-11-10 02:23:26 +00:00
//发送给前端重发消息
ws.requestResendRenderResponse(websocket_data.RequestBrowserResendRenderEvent{
RequestId: info.RequestId,
Description: "unity require resend",
})
2023-11-03 08:23:57 +00:00
logx.Info("渲染失败且发送了失败信息:", req.Msg)
2023-10-10 06:15:43 +00:00
} else {
logx.Info("渲染失败且找不到ws连接")
2023-09-04 08:54:55 +00:00
}
2023-08-15 09:10:43 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success")
2023-07-25 09:10:50 +00:00
}