mirror of
https://github.com/AlistGo/alist.git
synced 2025-04-23 05:44:04 +08:00
chore: get or remove by states
This commit is contained in:
parent
6c61f1d261
commit
92983aa185
@ -61,7 +61,7 @@ func TestDown(t *testing.T) {
|
||||
for {
|
||||
tsk := tasks[0]
|
||||
t.Logf("task: %+v", tsk)
|
||||
if tsk.GetState() == task.FINISHED {
|
||||
if tsk.GetState() == task.Succeeded {
|
||||
break
|
||||
}
|
||||
if tsk.GetState() == task.ERRORED {
|
||||
@ -75,7 +75,7 @@ func TestDown(t *testing.T) {
|
||||
}
|
||||
tsk := transferTaskManager.GetAll()[0]
|
||||
t.Logf("task: %+v", tsk)
|
||||
if tsk.GetState() == task.FINISHED {
|
||||
if tsk.GetState() == task.Succeeded {
|
||||
break
|
||||
}
|
||||
if tsk.GetState() == task.ERRORED {
|
||||
|
@ -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] {
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -11,3 +11,12 @@ func SliceEqual[T comparable](a, b []T) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func SliceContains[T comparable](arr []T, v T) bool {
|
||||
for _, vv := range arr {
|
||||
if vv == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user