feat: add task work limit
This commit is contained in:
parent
dd46e99e66
commit
d665cce739
@ -32,7 +32,7 @@ func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) (
|
|||||||
return false, operations.Copy(ctx, account, srcActualPath, dstActualPath)
|
return false, operations.Copy(ctx, account, srcActualPath, dstActualPath)
|
||||||
}
|
}
|
||||||
// not in an account
|
// not in an account
|
||||||
CopyTaskManager.Add(
|
CopyTaskManager.Submit(
|
||||||
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcActualPath, dstAccount.GetAccount().VirtualPath, dstActualPath),
|
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcActualPath, dstAccount.GetAccount().VirtualPath, dstActualPath),
|
||||||
func(task *task.Task) error {
|
func(task *task.Task) error {
|
||||||
return CopyBetween2Accounts(task, srcAccount, dstAccount, srcActualPath, dstActualPath)
|
return CopyBetween2Accounts(task, srcAccount, dstAccount, srcActualPath, dstActualPath)
|
||||||
@ -58,14 +58,14 @@ func CopyBetween2Accounts(t *task.Task, srcAccount, dstAccount driver.Driver, sr
|
|||||||
}
|
}
|
||||||
srcObjPath := stdpath.Join(srcPath, obj.GetName())
|
srcObjPath := stdpath.Join(srcPath, obj.GetName())
|
||||||
dstObjPath := stdpath.Join(dstPath, obj.GetName())
|
dstObjPath := stdpath.Join(dstPath, obj.GetName())
|
||||||
CopyTaskManager.Add(
|
CopyTaskManager.Submit(
|
||||||
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjPath, dstAccount.GetAccount().VirtualPath, dstObjPath),
|
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjPath, dstAccount.GetAccount().VirtualPath, dstObjPath),
|
||||||
func(t *task.Task) error {
|
func(t *task.Task) error {
|
||||||
return CopyBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstObjPath)
|
return CopyBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstObjPath)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CopyTaskManager.Add(
|
CopyTaskManager.Submit(
|
||||||
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcPath, dstAccount.GetAccount().VirtualPath, dstPath),
|
fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcPath, dstAccount.GetAccount().VirtualPath, dstPath),
|
||||||
func(t *task.Task) error {
|
func(t *task.Task) error {
|
||||||
return CopyFileBetween2Accounts(t, srcAccount, dstAccount, srcPath, dstPath)
|
return CopyFileBetween2Accounts(t, srcAccount, dstAccount, srcPath, dstPath)
|
||||||
|
@ -18,7 +18,7 @@ func Put(ctx context.Context, account driver.Driver, parentPath string, file mod
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "failed get account")
|
return errors.WithMessage(err, "failed get account")
|
||||||
}
|
}
|
||||||
UploadTaskManager.Add(fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), account.GetAccount().VirtualPath, actualParentPath), func(task *task.Task) error {
|
UploadTaskManager.Submit(fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), account.GetAccount().VirtualPath, actualParentPath), func(task *task.Task) error {
|
||||||
return operations.Put(task.Ctx, account, actualParentPath, file, nil)
|
return operations.Put(task.Ctx, account, actualParentPath, file, nil)
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
|
7
pkg/task/errors.go
Normal file
7
pkg/task/errors.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package task
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrTaskNotFound = errors.New("task not found")
|
||||||
|
)
|
@ -1,25 +1,39 @@
|
|||||||
package task
|
package task
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pkg/errors"
|
log "github.com/sirupsen/logrus"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/pkg/generic_sync"
|
"github.com/alist-org/alist/v3/pkg/generic_sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Manager struct {
|
type Manager struct {
|
||||||
works uint
|
workerC chan struct{}
|
||||||
curID uint64
|
curID uint64
|
||||||
tasks generic_sync.MapOf[uint64, *Task]
|
tasks generic_sync.MapOf[uint64, *Task]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *Manager) Add(name string, f Func) uint64 {
|
func (tm *Manager) Submit(name string, f Func) uint64 {
|
||||||
task := newTask(name, f)
|
task := newTask(name, f)
|
||||||
tm.addTask(task)
|
tm.addTask(task)
|
||||||
go task.Run()
|
tm.do(task.ID)
|
||||||
return task.ID
|
return task.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tm *Manager) do(tid uint64) {
|
||||||
|
task := tm.MustGet(tid)
|
||||||
|
go func() {
|
||||||
|
log.Debugf("task [%s] waiting for worker", task.Name)
|
||||||
|
select {
|
||||||
|
case <-tm.workerC:
|
||||||
|
log.Debugf("task [%s] starting", task.Name)
|
||||||
|
task.run()
|
||||||
|
log.Debugf("task [%s] ended", task.Name)
|
||||||
|
}
|
||||||
|
tm.workerC <- struct{}{}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func (tm *Manager) addTask(task *Task) {
|
func (tm *Manager) addTask(task *Task) {
|
||||||
task.ID = tm.curID
|
task.ID = tm.curID
|
||||||
atomic.AddUint64(&tm.curID, 1)
|
atomic.AddUint64(&tm.curID, 1)
|
||||||
@ -30,30 +44,35 @@ func (tm *Manager) GetAll() []*Task {
|
|||||||
return tm.tasks.Values()
|
return tm.tasks.Values()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *Manager) Get(id uint64) (*Task, bool) {
|
func (tm *Manager) Get(tid uint64) (*Task, bool) {
|
||||||
return tm.tasks.Load(id)
|
return tm.tasks.Load(tid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *Manager) Retry(id uint64) error {
|
func (tm *Manager) MustGet(tid uint64) *Task {
|
||||||
t, ok := tm.Get(id)
|
task, _ := tm.Get(tid)
|
||||||
if !ok {
|
return task
|
||||||
return errors.New("task not found")
|
|
||||||
}
|
}
|
||||||
t.Retry()
|
|
||||||
|
func (tm *Manager) Retry(tid uint64) error {
|
||||||
|
t, ok := tm.Get(tid)
|
||||||
|
if !ok {
|
||||||
|
return ErrTaskNotFound
|
||||||
|
}
|
||||||
|
tm.do(t.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *Manager) Cancel(id uint64) error {
|
func (tm *Manager) Cancel(tid uint64) error {
|
||||||
t, ok := tm.Get(id)
|
t, ok := tm.Get(tid)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("task not found")
|
return ErrTaskNotFound
|
||||||
}
|
}
|
||||||
t.Cancel()
|
t.Cancel()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *Manager) Remove(id uint64) {
|
func (tm *Manager) Remove(tid uint64) {
|
||||||
tm.tasks.Delete(id)
|
tm.tasks.Delete(tid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *Manager) RemoveFinished() {
|
func (tm *Manager) RemoveFinished() {
|
||||||
|
@ -4,6 +4,7 @@ package task
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -12,6 +13,7 @@ var (
|
|||||||
FINISHED = "finished"
|
FINISHED = "finished"
|
||||||
CANCELING = "canceling"
|
CANCELING = "canceling"
|
||||||
CANCELED = "canceled"
|
CANCELED = "canceled"
|
||||||
|
ERRORED = "errored"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Func func(task *Task) error
|
type Func func(task *Task) error
|
||||||
@ -46,18 +48,27 @@ func (t *Task) SetProgress(percentage int) {
|
|||||||
t.Progress = percentage
|
t.Progress = percentage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) Run() {
|
func (t *Task) run() {
|
||||||
t.Status = RUNNING
|
t.Status = RUNNING
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
log.Errorf("error [%+v] while run task [%s]", err, t.Name)
|
||||||
|
t.Error = errors.Errorf("panic: %+v", err)
|
||||||
|
t.Status = ERRORED
|
||||||
|
}
|
||||||
|
}()
|
||||||
t.Error = t.Func(t)
|
t.Error = t.Func(t)
|
||||||
if errors.Is(t.Ctx.Err(), context.Canceled) {
|
if errors.Is(t.Ctx.Err(), context.Canceled) {
|
||||||
t.Status = CANCELED
|
t.Status = CANCELED
|
||||||
|
} else if t.Error != nil {
|
||||||
|
t.Status = ERRORED
|
||||||
} else {
|
} else {
|
||||||
t.Status = FINISHED
|
t.Status = FINISHED
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) Retry() {
|
func (t *Task) retry() {
|
||||||
t.Run()
|
t.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) Cancel() {
|
func (t *Task) Cancel() {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
func TestTask_Manager(t *testing.T) {
|
func TestTask_Manager(t *testing.T) {
|
||||||
tm := NewTaskManager()
|
tm := NewTaskManager()
|
||||||
id := tm.Add("test", func(task *Task) error {
|
id := tm.Submit("test", func(task *Task) error {
|
||||||
time.Sleep(time.Millisecond * 500)
|
time.Sleep(time.Millisecond * 500)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -29,7 +29,7 @@ func TestTask_Manager(t *testing.T) {
|
|||||||
|
|
||||||
func TestTask_Cancel(t *testing.T) {
|
func TestTask_Cancel(t *testing.T) {
|
||||||
tm := NewTaskManager()
|
tm := NewTaskManager()
|
||||||
id := tm.Add("test", func(task *Task) error {
|
id := tm.Submit("test", func(task *Task) error {
|
||||||
for {
|
for {
|
||||||
if utils.IsCanceled(task.Ctx) {
|
if utils.IsCanceled(task.Ctx) {
|
||||||
return nil
|
return nil
|
||||||
@ -53,7 +53,7 @@ func TestTask_Cancel(t *testing.T) {
|
|||||||
func TestTask_Retry(t *testing.T) {
|
func TestTask_Retry(t *testing.T) {
|
||||||
tm := NewTaskManager()
|
tm := NewTaskManager()
|
||||||
num := 0
|
num := 0
|
||||||
id := tm.Add("test", func(task *Task) error {
|
id := tm.Submit("test", func(task *Task) error {
|
||||||
num++
|
num++
|
||||||
if num&1 == 1 {
|
if num&1 == 1 {
|
||||||
return errors.New("test error")
|
return errors.New("test error")
|
||||||
@ -71,7 +71,7 @@ func TestTask_Retry(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
t.Logf("task error: %s", task.Error)
|
t.Logf("task error: %s", task.Error)
|
||||||
}
|
}
|
||||||
task.Retry()
|
task.retry()
|
||||||
time.Sleep(time.Millisecond)
|
time.Sleep(time.Millisecond)
|
||||||
if task.Error != nil {
|
if task.Error != nil {
|
||||||
t.Errorf("task error: %+v, but expected nil", task.Error)
|
t.Errorf("task error: %+v, but expected nil", task.Error)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user