chore: get or remove by states

This commit is contained in:
Noah Hsu
2022-06-23 21:19:01 +08:00
parent 6c61f1d261
commit 92983aa185
5 changed files with 26 additions and 14 deletions

View File

@ -2,6 +2,7 @@ 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"
)
@ -78,22 +79,24 @@ func (tm *Manager[K]) RemoveAll() {
tm.tasks.Clear()
}
func (tm *Manager[K]) RemoveFinished() {
func (tm *Manager[K]) RemoveByStates(states ...string) {
tasks := tm.GetAll()
for _, task := range tasks {
if task.state == FINISHED {
if utils.SliceContains(states, task.GetState()) {
tm.Remove(task.ID)
}
}
}
func (tm *Manager[K]) RemoveError() {
tasks := tm.GetAll()
for _, task := range tasks {
if task.Error != nil {
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 NewTaskManager[K comparable](maxWorker int, updateID ...func(*K)) *Manager[K] {

View File

@ -10,7 +10,7 @@ import (
var (
PENDING = "pending"
RUNNING = "running"
FINISHED = "finished"
Succeeded = "succeeded"
CANCELING = "canceling"
CANCELED = "canceled"
ERRORED = "errored"
@ -65,7 +65,7 @@ func (t *Task[K]) run() {
} else if t.Error != nil {
t.state = ERRORED
} else {
t.state = FINISHED
t.state = Succeeded
if t.callback != nil {
t.callback(t)
}
@ -77,7 +77,7 @@ func (t *Task[K]) retry() {
}
func (t *Task[K]) Cancel() {
if t.state == FINISHED || t.state == CANCELED {
if t.state == Succeeded || t.state == CANCELED {
return
}
if t.cancel != nil {

View File

@ -28,7 +28,7 @@ func TestTask_Manager(t *testing.T) {
t.Errorf("task status not running: %s", task.state)
}
time.Sleep(time.Second)
if task.state != FINISHED {
if task.state != Succeeded {
t.Errorf("task status not finished: %s", task.state)
}
}