forked from AlistGo/alist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
141 lines (123 loc) · 2.92 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package task
import (
"github.com/alist-org/alist/v3/pkg/generic_sync"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type Manager[K comparable] struct {
curID K
workerC chan struct{}
updateID func(*K)
tasks generic_sync.MapOf[K, *Task[K]]
}
func (tm *Manager[K]) Submit(task *Task[K]) K {
if tm.updateID != nil {
tm.updateID(&tm.curID)
task.ID = tm.curID
}
tm.tasks.Store(task.ID, task)
tm.do(task)
return task.ID
}
func (tm *Manager[K]) do(task *Task[K]) {
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)
case <-task.Ctx.Done():
log.Debugf("task [%s] canceled", task.Name)
return
}
// return worker
tm.workerC <- struct{}{}
}()
}
func (tm *Manager[K]) GetAll() []*Task[K] {
return tm.tasks.Values()
}
func (tm *Manager[K]) Get(tid K) (*Task[K], bool) {
return tm.tasks.Load(tid)
}
func (tm *Manager[K]) MustGet(tid K) *Task[K] {
task, _ := tm.Get(tid)
return task
}
func (tm *Manager[K]) Retry(tid K) error {
t, ok := tm.Get(tid)
if !ok {
return errors.WithStack(ErrTaskNotFound)
}
tm.do(t)
return nil
}
func (tm *Manager[K]) Cancel(tid K) error {
t, ok := tm.Get(tid)
if !ok {
return errors.WithStack(ErrTaskNotFound)
}
t.Cancel()
return nil
}
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)
}
tm.tasks.Delete(tid)
return nil
}
// RemoveAll removes all tasks from the manager, this maybe shouldn't be used
// because the task maybe still running.
func (tm *Manager[K]) RemoveAll() {
tm.tasks.Clear()
}
func (tm *Manager[K]) RemoveByStates(states ...string) {
tasks := tm.GetAll()
for _, task := range tasks {
if utils.SliceContains(states, task.GetState()) {
_ = tm.Remove(task.ID)
}
}
}
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)
}
return true
})
return tasks
}
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)
}
func (tm *Manager[K]) ClearDone() {
tm.RemoveByStates(SUCCEEDED, CANCELED, ERRORED)
}
func (tm *Manager[K]) RawTasks() *generic_sync.MapOf[K, *Task[K]] {
return &tm.tasks
}
func NewTaskManager[K comparable](maxWorker int, updateID ...func(*K)) *Manager[K] {
tm := &Manager[K]{
tasks: generic_sync.MapOf[K, *Task[K]]{},
workerC: make(chan struct{}, maxWorker),
}
for i := 0; i < maxWorker; i++ {
tm.workerC <- struct{}{}
}
if len(updateID) > 0 {
tm.updateID = updateID[0]
}
return tm
}