refactor(task): generic task manager
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
stdpath "path"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/alist-org/alist/v3/pkg/task"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
@ -14,7 +15,9 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var CopyTaskManager = task.NewTaskManager()
|
||||
var CopyTaskManager = task.NewTaskManager[uint64, struct{}](3, func(tid *uint64) {
|
||||
atomic.AddUint64(tid, 1)
|
||||
})
|
||||
|
||||
// Copy if in an account, call move method
|
||||
// if not, add copy task
|
||||
@ -32,15 +35,16 @@ func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) (
|
||||
return false, operations.Copy(ctx, account, srcActualPath, dstActualPath)
|
||||
}
|
||||
// not in an account
|
||||
CopyTaskManager.Submit(
|
||||
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcActualPath, dstAccount.GetAccount().VirtualPath, dstActualPath),
|
||||
func(task *task.Task) error {
|
||||
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64, struct{}]{
|
||||
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcActualPath, dstAccount.GetAccount().VirtualPath, dstActualPath),
|
||||
Func: func(task *task.Task[uint64, struct{}]) error {
|
||||
return CopyBetween2Accounts(task, srcAccount, dstAccount, srcActualPath, dstActualPath)
|
||||
})
|
||||
},
|
||||
}))
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func CopyBetween2Accounts(t *task.Task, srcAccount, dstAccount driver.Driver, srcPath, dstPath string) error {
|
||||
func CopyBetween2Accounts(t *task.Task[uint64, struct{}], srcAccount, dstAccount driver.Driver, srcPath, dstPath string) error {
|
||||
t.SetStatus("getting src object")
|
||||
srcObj, err := operations.Get(t.Ctx, srcAccount, srcPath)
|
||||
if err != nil {
|
||||
@ -58,28 +62,30 @@ func CopyBetween2Accounts(t *task.Task, srcAccount, dstAccount driver.Driver, sr
|
||||
}
|
||||
srcObjPath := stdpath.Join(srcPath, obj.GetName())
|
||||
dstObjPath := stdpath.Join(dstPath, obj.GetName())
|
||||
CopyTaskManager.Submit(
|
||||
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjPath, dstAccount.GetAccount().VirtualPath, dstObjPath),
|
||||
func(t *task.Task) error {
|
||||
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64, struct{}]{
|
||||
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjPath, dstAccount.GetAccount().VirtualPath, dstObjPath),
|
||||
Func: func(t *task.Task[uint64, struct{}]) error {
|
||||
return CopyBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstObjPath)
|
||||
})
|
||||
},
|
||||
}))
|
||||
}
|
||||
} else {
|
||||
CopyTaskManager.Submit(
|
||||
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcPath, dstAccount.GetAccount().VirtualPath, dstPath),
|
||||
func(t *task.Task) error {
|
||||
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64, struct{}]{
|
||||
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcPath, dstAccount.GetAccount().VirtualPath, dstPath),
|
||||
Func: func(t *task.Task[uint64, struct{}]) error {
|
||||
return CopyFileBetween2Accounts(t, srcAccount, dstAccount, srcPath, dstPath)
|
||||
})
|
||||
},
|
||||
}))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CopyFileBetween2Accounts(t *task.Task, srcAccount, dstAccount driver.Driver, srcPath, dstPath string) error {
|
||||
srcFile, err := operations.Get(t.Ctx, srcAccount, srcPath)
|
||||
func CopyFileBetween2Accounts(tsk *task.Task[uint64, struct{}], srcAccount, dstAccount driver.Driver, srcPath, dstPath string) error {
|
||||
srcFile, err := operations.Get(tsk.Ctx, srcAccount, srcPath)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "failed get src [%s] file", srcPath)
|
||||
}
|
||||
link, err := operations.Link(t.Ctx, srcAccount, srcPath, model.LinkArgs{})
|
||||
link, err := operations.Link(tsk.Ctx, srcAccount, srcPath, model.LinkArgs{})
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "failed get [%s] link", srcPath)
|
||||
}
|
||||
@ -87,5 +93,5 @@ func CopyFileBetween2Accounts(t *task.Task, srcAccount, dstAccount driver.Driver
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "failed get [%s] stream", srcPath)
|
||||
}
|
||||
return operations.Put(t.Ctx, dstAccount, dstPath, stream, t.SetProgress)
|
||||
return operations.Put(tsk.Ctx, dstAccount, dstPath, stream, tsk.SetProgress)
|
||||
}
|
||||
|
Reference in New Issue
Block a user