2022-06-17 07:57:16 +00:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alist-org/alist/v3/pkg/generic_sync"
|
2022-06-23 13:19:01 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2022-06-23 13:09:54 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-06-21 08:14:37 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-17 07:57:16 +00:00
|
|
|
)
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
type Manager[K comparable] struct {
|
2022-06-21 08:14:37 +00:00
|
|
|
workerC chan struct{}
|
|
|
|
curID K
|
|
|
|
updateID func(*K)
|
2022-06-22 11:28:41 +00:00
|
|
|
tasks generic_sync.MapOf[K, *Task[K]]
|
2022-06-17 07:57:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
func (tm *Manager[K]) Submit(task *Task[K]) K {
|
2022-06-21 08:14:37 +00:00
|
|
|
if tm.updateID != nil {
|
|
|
|
task.ID = tm.curID
|
|
|
|
tm.updateID(&task.ID)
|
|
|
|
}
|
|
|
|
tm.tasks.Store(task.ID, task)
|
|
|
|
tm.do(task)
|
2022-06-17 13:52:31 +00:00
|
|
|
return task.ID
|
2022-06-17 07:57:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
func (tm *Manager[K]) do(task *Task[K]) {
|
2022-06-18 12:38:14 +00:00
|
|
|
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)
|
2022-06-29 14:06:56 +00:00
|
|
|
case <-task.Ctx.Done():
|
|
|
|
log.Debugf("task [%s] canceled", task.Name)
|
|
|
|
return
|
2022-06-18 12:38:14 +00:00
|
|
|
}
|
2022-06-21 08:14:37 +00:00
|
|
|
// return worker
|
2022-06-18 12:38:14 +00:00
|
|
|
tm.workerC <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
func (tm *Manager[K]) GetAll() []*Task[K] {
|
2022-06-17 07:57:16 +00:00
|
|
|
return tm.tasks.Values()
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
func (tm *Manager[K]) Get(tid K) (*Task[K], bool) {
|
2022-06-18 12:38:14 +00:00
|
|
|
return tm.tasks.Load(tid)
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
func (tm *Manager[K]) MustGet(tid K) *Task[K] {
|
2022-06-18 12:38:14 +00:00
|
|
|
task, _ := tm.Get(tid)
|
|
|
|
return task
|
2022-06-17 07:57:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
func (tm *Manager[K]) Retry(tid K) error {
|
2022-06-18 12:38:14 +00:00
|
|
|
t, ok := tm.Get(tid)
|
2022-06-17 14:09:20 +00:00
|
|
|
if !ok {
|
2022-06-23 13:09:54 +00:00
|
|
|
return errors.WithStack(ErrTaskNotFound)
|
2022-06-17 14:09:20 +00:00
|
|
|
}
|
2022-06-21 08:14:37 +00:00
|
|
|
tm.do(t)
|
2022-06-17 14:09:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
func (tm *Manager[K]) Cancel(tid K) error {
|
2022-06-18 12:38:14 +00:00
|
|
|
t, ok := tm.Get(tid)
|
2022-06-17 14:09:20 +00:00
|
|
|
if !ok {
|
2022-06-23 13:09:54 +00:00
|
|
|
return errors.WithStack(ErrTaskNotFound)
|
2022-06-17 14:09:20 +00:00
|
|
|
}
|
|
|
|
t.Cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-31 13:21:54 +00:00
|
|
|
func (tm *Manager[K]) Remove(tid K) error {
|
|
|
|
t, ok := tm.Get(tid)
|
|
|
|
if !ok {
|
|
|
|
return errors.WithStack(ErrTaskNotFound)
|
|
|
|
}
|
|
|
|
if !t.Done() {
|
|
|
|
return errors.WithStack(ErrTaskRunning)
|
|
|
|
}
|
2022-06-18 12:38:14 +00:00
|
|
|
tm.tasks.Delete(tid)
|
2022-07-31 13:21:54 +00:00
|
|
|
return nil
|
2022-06-17 07:57:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 12:34:58 +00:00
|
|
|
// RemoveAll removes all tasks from the manager, this maybe shouldn't be used
|
|
|
|
// because the task maybe still running.
|
2022-06-22 11:28:41 +00:00
|
|
|
func (tm *Manager[K]) RemoveAll() {
|
2022-06-20 12:34:58 +00:00
|
|
|
tm.tasks.Clear()
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:19:01 +00:00
|
|
|
func (tm *Manager[K]) RemoveByStates(states ...string) {
|
2022-06-17 07:57:16 +00:00
|
|
|
tasks := tm.GetAll()
|
|
|
|
for _, task := range tasks {
|
2022-06-23 13:19:01 +00:00
|
|
|
if utils.SliceContains(states, task.GetState()) {
|
2022-06-17 07:57:16 +00:00
|
|
|
tm.Remove(task.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:19:01 +00:00
|
|
|
func (tm *Manager[K]) GetByStates(states ...string) []*Task[K] {
|
|
|
|
var tasks []*Task[K]
|
|
|
|
tm.tasks.Range(func(key K, value *Task[K]) bool {
|
|
|
|
if utils.SliceContains(states, value.GetState()) {
|
|
|
|
tasks = append(tasks, value)
|
2022-06-17 07:57:16 +00:00
|
|
|
}
|
2022-06-23 13:19:01 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return tasks
|
2022-06-17 07:57:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 10:36:14 +00:00
|
|
|
func (tm *Manager[K]) ListUndone() []*Task[K] {
|
|
|
|
return tm.GetByStates(PENDING, RUNNING, CANCELING)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *Manager[K]) ListDone() []*Task[K] {
|
|
|
|
return tm.GetByStates(SUCCEEDED, CANCELED, ERRORED)
|
|
|
|
}
|
|
|
|
|
2022-07-31 13:21:54 +00:00
|
|
|
func (tm *Manager[K]) ClearDone() {
|
|
|
|
tm.RemoveByStates(SUCCEEDED, CANCELED, ERRORED)
|
|
|
|
}
|
|
|
|
|
2022-06-22 11:28:41 +00:00
|
|
|
func NewTaskManager[K comparable](maxWorker int, updateID ...func(*K)) *Manager[K] {
|
|
|
|
tm := &Manager[K]{
|
|
|
|
tasks: generic_sync.MapOf[K, *Task[K]]{},
|
2022-06-21 08:14:37 +00:00
|
|
|
workerC: make(chan struct{}, maxWorker),
|
|
|
|
}
|
|
|
|
for i := 0; i < maxWorker; i++ {
|
|
|
|
tm.workerC <- struct{}{}
|
|
|
|
}
|
|
|
|
if len(updateID) > 0 {
|
|
|
|
tm.updateID = updateID[0]
|
2022-06-17 13:52:31 +00:00
|
|
|
}
|
2022-06-21 08:14:37 +00:00
|
|
|
return tm
|
2022-06-17 07:57:16 +00:00
|
|
|
}
|