fix(worker): refactor and fix issues of worker

This commit is contained in:
hexxa 2021-09-12 12:45:03 +08:00 committed by Hexxa
parent 8518072c7e
commit 4a5c68df17
13 changed files with 194 additions and 86 deletions

View file

@ -2,6 +2,7 @@ package fileshdr
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
@ -20,7 +21,6 @@ import (
"github.com/ihexxa/quickshare/src/depidx"
q "github.com/ihexxa/quickshare/src/handlers"
"github.com/ihexxa/quickshare/src/userstore"
"github.com/ihexxa/quickshare/src/worker"
"github.com/ihexxa/quickshare/src/worker/localworker"
)
@ -41,16 +41,17 @@ type FileHandlers struct {
cfg gocfg.ICfg
deps *depidx.Deps
uploadMgr *UploadMgr
workers worker.IWorkerPool
}
func NewFileHandlers(cfg gocfg.ICfg, deps *depidx.Deps, workers worker.IWorkerPool) (*FileHandlers, error) {
return &FileHandlers{
func NewFileHandlers(cfg gocfg.ICfg, deps *depidx.Deps) (*FileHandlers, error) {
handlers := &FileHandlers{
cfg: cfg,
deps: deps,
uploadMgr: NewUploadMgr(deps.KV()),
workers: workers,
}, nil
}
deps.Workers().AddHandler(MsgTypeSha1, handlers.genSha1)
return handlers, nil
}
type AutoLocker struct {
@ -232,7 +233,7 @@ type MetadataResp struct {
Size int64 `json:"size"`
ModTime time.Time `json:"modTime"`
IsDir bool `json:"isDir"`
Sha1 string `json"sha1"`
Sha1 string `json:"sha1"`
}
func (h *FileHandlers) Metadata(c *gin.Context) {
@ -416,11 +417,19 @@ func (h *FileHandlers) UploadChunk(c *gin.Context) {
return
}
err = h.workers.TryPut(
msg, err := json.Marshal(Sha1Params{
FilePath: fsFilePath,
})
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.Workers().TryPut(
localworker.NewMsg(
h.deps.ID().Gen(),
map[string]string{localworker.MsgTypeKey: "sha1"},
fsFilePath,
map[string]string{localworker.MsgTypeKey: MsgTypeSha1},
string(msg),
),
)
if err != nil {
@ -877,11 +886,19 @@ func (h *FileHandlers) GenerateHash(c *gin.Context) {
return
}
err := h.workers.TryPut(
msg, err := json.Marshal(Sha1Params{
FilePath: req.FilePath,
})
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
err = h.deps.Workers().TryPut(
localworker.NewMsg(
h.deps.ID().Gen(),
map[string]string{localworker.MsgTypeKey: "sha1"},
req.FilePath,
map[string]string{localworker.MsgTypeKey: MsgTypeSha1},
string(msg),
),
)
if err != nil {