alist/pkg/task/manager.go

111 lines
2.1 KiB
Go
Raw Normal View History

2022-06-17 07:57:16 +00:00
package task
import (
"github.com/alist-org/alist/v3/pkg/generic_sync"
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-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-18 12:38:14 +00:00
return 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-18 12:38:14 +00:00
return ErrTaskNotFound
2022-06-17 14:09:20 +00:00
}
t.Cancel()
return nil
}
2022-06-22 11:28:41 +00:00
func (tm *Manager[K]) Remove(tid K) {
2022-06-18 12:38:14 +00:00
tm.tasks.Delete(tid)
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-22 11:28:41 +00:00
func (tm *Manager[K]) RemoveFinished() {
2022-06-17 07:57:16 +00:00
tasks := tm.GetAll()
for _, task := range tasks {
if task.Status == FINISHED {
tm.Remove(task.ID)
}
}
}
2022-06-22 11:28:41 +00:00
func (tm *Manager[K]) RemoveError() {
2022-06-17 07:57:16 +00:00
tasks := tm.GetAll()
for _, task := range tasks {
if task.Error != nil {
tm.Remove(task.ID)
}
}
}
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
}