视频转码接入队列(asynq)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -23,7 +22,6 @@ func (server *Server) homeView(w http.ResponseWriter, r *http.Request) {
|
||||
if len(item.Description) > 65 {
|
||||
temp := strings.TrimSpace(item.Description[0:65]) + "..."
|
||||
item.Description = temp
|
||||
log.Println(item.Description)
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/zhang2092/mediahls/internal/pkg/config"
|
||||
"github.com/zhang2092/mediahls/internal/pkg/logger"
|
||||
"github.com/zhang2092/mediahls/internal/pkg/token"
|
||||
"github.com/zhang2092/mediahls/internal/worker"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -31,11 +32,12 @@ type Server struct {
|
||||
router *mux.Router
|
||||
secureCookie *securecookie.SecureCookie
|
||||
|
||||
store db.Store
|
||||
tokenMaker token.Maker
|
||||
store db.Store
|
||||
tokenMaker token.Maker
|
||||
taskDistributor worker.TaskDistributor
|
||||
}
|
||||
|
||||
func NewServer(templateFS fs.FS, staticFS fs.FS, conf *config.Config, store db.Store) (*Server, error) {
|
||||
func NewServer(templateFS fs.FS, staticFS fs.FS, conf *config.Config, store db.Store, taskDistributor worker.TaskDistributor) (*Server, error) {
|
||||
tokenMaker, err := token.NewPasetoMaker(conf.TokenSymmetricKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot create token maker: %w", err)
|
||||
@@ -47,12 +49,13 @@ func NewServer(templateFS fs.FS, staticFS fs.FS, conf *config.Config, store db.S
|
||||
// secureCookie.MaxAge(7200)
|
||||
|
||||
server := &Server{
|
||||
templateFS: templateFS,
|
||||
staticFS: staticFS,
|
||||
conf: conf,
|
||||
secureCookie: secureCookie,
|
||||
store: store,
|
||||
tokenMaker: tokenMaker,
|
||||
templateFS: templateFS,
|
||||
staticFS: staticFS,
|
||||
conf: conf,
|
||||
secureCookie: secureCookie,
|
||||
store: store,
|
||||
tokenMaker: tokenMaker,
|
||||
taskDistributor: taskDistributor,
|
||||
}
|
||||
|
||||
server.setupRouter()
|
||||
|
||||
@@ -10,10 +10,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/zhang2092/mediahls/internal/db"
|
||||
"github.com/zhang2092/mediahls/internal/pkg/convert"
|
||||
"github.com/zhang2092/mediahls/internal/pkg/fileutil"
|
||||
"github.com/zhang2092/mediahls/internal/pkg/logger"
|
||||
"github.com/zhang2092/mediahls/internal/worker"
|
||||
)
|
||||
|
||||
// obj
|
||||
@@ -137,16 +139,30 @@ func (server *Server) editVideo(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
u := withUser(ctx)
|
||||
if len(vm.ID) == 0 {
|
||||
_, err := server.store.CreateVideo(ctx, db.CreateVideoParams{
|
||||
ID: genId(),
|
||||
Title: vm.Title,
|
||||
Description: vm.Description,
|
||||
Images: vm.Images,
|
||||
OriginLink: vm.OriginLink,
|
||||
PlayLink: "",
|
||||
UserID: u.ID,
|
||||
CreateBy: u.Name,
|
||||
})
|
||||
arg := db.CreateVideoTxParam{
|
||||
CreateVideoParams: db.CreateVideoParams{
|
||||
ID: genId(),
|
||||
Title: vm.Title,
|
||||
Description: vm.Description,
|
||||
Images: vm.Images,
|
||||
OriginLink: vm.OriginLink,
|
||||
PlayLink: "",
|
||||
UserID: u.ID,
|
||||
CreateBy: u.Name,
|
||||
},
|
||||
AfterCreate: func(video db.Video) error {
|
||||
taskPayload := worker.PayloadConvertHLS{
|
||||
Id: video.ID,
|
||||
}
|
||||
opts := []asynq.Option{
|
||||
asynq.MaxRetry(3),
|
||||
asynq.ProcessIn(10 * time.Second),
|
||||
asynq.Queue(worker.QueueCritical),
|
||||
}
|
||||
return server.taskDistributor.DistributeConvertHLS(ctx, &taskPayload, opts...)
|
||||
},
|
||||
}
|
||||
_, err := server.store.CreateVideoTx(ctx, arg)
|
||||
if err != nil {
|
||||
vm.Summary = "添加视频失败"
|
||||
server.renderEditVideo(w, r, vm)
|
||||
@@ -204,6 +220,8 @@ func (server *Server) deleteVideo(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// transfer 视频转码
|
||||
// 已弃用
|
||||
// 在视频添加的时候 同时添加到队列 通过队列去视频转码
|
||||
func (server *Server) transfer(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
xid := vars["xid"]
|
||||
|
||||
Reference in New Issue
Block a user