feat: add copy to task manager

This commit is contained in:
Noah Hsu
2022-06-17 21:23:44 +08:00
parent 53e969e894
commit fa6e918fc7
10 changed files with 143 additions and 70 deletions

61
pkg/task/manager.go Normal file
View File

@ -0,0 +1,61 @@
package task
import (
"sync/atomic"
"github.com/alist-org/alist/v3/pkg/generic_sync"
)
func NewTaskManager() *Manager {
return &Manager{
tasks: generic_sync.MapOf[int64, *Task]{},
curID: 0,
}
}
type Manager struct {
curID int64
tasks generic_sync.MapOf[int64, *Task]
}
func (tm *Manager) AddTask(task *Task) {
task.ID = tm.curID
atomic.AddInt64(&tm.curID, 1)
tm.tasks.Store(task.ID, task)
}
func (tm *Manager) GetAll() []*Task {
return tm.tasks.Values()
}
func (tm *Manager) Get(id int64) (*Task, bool) {
return tm.tasks.Load(id)
}
func (tm *Manager) Remove(id int64) {
tm.tasks.Delete(id)
}
func (tm *Manager) RemoveFinished() {
tasks := tm.GetAll()
for _, task := range tasks {
if task.Status == FINISHED {
tm.Remove(task.ID)
}
}
}
func (tm *Manager) RemoveError() {
tasks := tm.GetAll()
for _, task := range tasks {
if task.Error != nil {
tm.Remove(task.ID)
}
}
}
func (tm *Manager) Add(name string, f Func) {
task := newTask(name, f)
tm.AddTask(task)
go task.Run()
}

64
pkg/task/task.go Normal file
View File

@ -0,0 +1,64 @@
// Package task manage task, such as file upload, file copy between accounts, offline download, etc.
package task
import (
"context"
"github.com/pkg/errors"
)
var (
PENDING = "pending"
RUNNING = "running"
FINISHED = "finished"
CANCELING = "canceling"
CANCELED = "canceled"
)
type Func func(task *Task) error
type Task struct {
ID int64
Name string
Status string
Error error
Func Func
Ctx context.Context
cancel context.CancelFunc
}
func newTask(name string, func_ Func) *Task {
ctx, cancel := context.WithCancel(context.Background())
return &Task{
Name: name,
Status: PENDING,
Func: func_,
Ctx: ctx,
cancel: cancel,
}
}
func (t *Task) SetStatus(status string) {
t.Status = status
}
func (t *Task) Run() {
t.Status = RUNNING
t.Error = t.Func(t)
if errors.Is(t.Ctx.Err(), context.Canceled) {
t.Status = CANCELED
} else {
t.Status = FINISHED
}
}
func (t *Task) Retry() {
t.Run()
}
func (t *Task) Cancel() {
if t.cancel != nil {
t.cancel()
}
// maybe can't cancel
t.Status = CANCELING
}

12
pkg/utils/ctx.go Normal file
View File

@ -0,0 +1,12 @@
package utils
import "context"
func IsCanceled(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}