feat: persistant Task (#6925 close #5313)

This commit is contained in:
itsHenry
2024-08-07 12:16:21 +08:00
committed by GitHub
parent f2727095d9
commit 74f8295960
15 changed files with 201 additions and 56 deletions

View File

@ -76,6 +76,7 @@ func AddURL(ctx context.Context, args *AddURLArgs) (tache.TaskWithInfo, error) {
DstDirPath: args.DstDirPath,
TempDir: tempDir,
DeletePolicy: deletePolicy,
Toolname: args.Tool,
tool: tool,
}
DownloadTaskManager.Add(t)

View File

@ -14,19 +14,26 @@ import (
type DownloadTask struct {
tache.Base
Url string `json:"url"`
DstDirPath string `json:"dst_dir_path"`
TempDir string `json:"temp_dir"`
DeletePolicy DeletePolicy `json:"delete_policy"`
Status string `json:"status"`
Signal chan int `json:"-"`
GID string `json:"-"`
Url string `json:"url"`
DstDirPath string `json:"dst_dir_path"`
TempDir string `json:"temp_dir"`
DeletePolicy DeletePolicy `json:"delete_policy"`
Toolname string `json:"toolname"`
Status string `json:"-"`
Signal chan int `json:"-"`
GID string `json:"-"`
tool Tool
callStatusRetried int
}
func (t *DownloadTask) Run() error {
if t.tool == nil {
tool, err := Tools.Get(t.Toolname)
if err != nil {
return errors.WithMessage(err, "failed get tool")
}
t.tool = tool
}
if err := t.tool.Run(t); !errs.IsNotSupportError(err) {
if err == nil {
return t.Complete()
@ -142,9 +149,10 @@ func (t *DownloadTask) Complete() error {
file := files[i]
TransferTaskManager.Add(&TransferTask{
file: file,
dstDirPath: t.DstDirPath,
tempDir: t.TempDir,
deletePolicy: t.DeletePolicy,
DstDirPath: t.DstDirPath,
TempDir: t.TempDir,
DeletePolicy: t.DeletePolicy,
FileDir: file.Path,
})
}
return nil

View File

@ -2,6 +2,9 @@ package tool
import (
"fmt"
"os"
"path/filepath"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/stream"
@ -9,21 +12,27 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/xhofe/tache"
"os"
"path/filepath"
)
type TransferTask struct {
tache.Base
FileDir string `json:"file_dir"`
DstDirPath string `json:"dst_dir_path"`
TempDir string `json:"temp_dir"`
DeletePolicy DeletePolicy `json:"delete_policy"`
file File
dstDirPath string
tempDir string
deletePolicy DeletePolicy
}
func (t *TransferTask) Run() error {
// check dstDir again
storage, dstDirActualPath, err := op.GetStorageAndActualPath(t.dstDirPath)
var err error
if (t.file == File{}) {
t.file, err = GetFile(t.FileDir)
if err != nil {
return errors.Wrapf(err, "failed to get file %s", t.FileDir)
}
}
storage, dstDirActualPath, err := op.GetStorageAndActualPath(t.DstDirPath)
if err != nil {
return errors.WithMessage(err, "failed get storage")
}
@ -44,7 +53,7 @@ func (t *TransferTask) Run() error {
Mimetype: mimetype,
Closers: utils.NewClosers(rc),
}
relDir, err := filepath.Rel(t.tempDir, filepath.Dir(t.file.Path))
relDir, err := filepath.Rel(t.TempDir, filepath.Dir(t.file.Path))
if err != nil {
log.Errorf("find relation directory error: %v", err)
}
@ -53,7 +62,7 @@ func (t *TransferTask) Run() error {
}
func (t *TransferTask) GetName() string {
return fmt.Sprintf("transfer %s to [%s]", t.file.Path, t.dstDirPath)
return fmt.Sprintf("transfer %s to [%s]", t.file.Path, t.DstDirPath)
}
func (t *TransferTask) GetStatus() string {
@ -61,7 +70,7 @@ func (t *TransferTask) GetStatus() string {
}
func (t *TransferTask) OnSucceeded() {
if t.deletePolicy == DeleteOnUploadSucceed || t.deletePolicy == DeleteAlways {
if t.DeletePolicy == DeleteOnUploadSucceed || t.DeletePolicy == DeleteAlways {
err := os.Remove(t.file.Path)
if err != nil {
log.Errorf("failed to delete file %s, error: %s", t.file.Path, err.Error())
@ -70,7 +79,7 @@ func (t *TransferTask) OnSucceeded() {
}
func (t *TransferTask) OnFailed() {
if t.deletePolicy == DeleteOnUploadFailed || t.deletePolicy == DeleteAlways {
if t.DeletePolicy == DeleteOnUploadFailed || t.DeletePolicy == DeleteAlways {
err := os.Remove(t.file.Path)
if err != nil {
log.Errorf("failed to delete file %s, error: %s", t.file.Path, err.Error())

View File

@ -26,3 +26,16 @@ func GetFiles(dir string) ([]File, error) {
}
return files, nil
}
func GetFile(path string) (File, error) {
info, err := os.Stat(path)
if err != nil {
return File{}, err
}
return File{
Name: info.Name(),
Size: info.Size(),
Path: path,
Modified: info.ModTime(),
}, nil
}