mirror of https://github.com/Xhofe/alist
feat: task manage api
parent
c88680b495
commit
0bf724f447
|
@ -60,7 +60,7 @@ func TestDown(t *testing.T) {
|
|||
for {
|
||||
tsk := tasks[0]
|
||||
t.Logf("task: %+v", tsk)
|
||||
if tsk.GetState() == task.Succeeded {
|
||||
if tsk.GetState() == task.SUCCEEDED {
|
||||
break
|
||||
}
|
||||
if tsk.GetState() == task.ERRORED {
|
||||
|
@ -74,7 +74,7 @@ func TestDown(t *testing.T) {
|
|||
}
|
||||
tsk := TransferTaskManager.GetAll()[0]
|
||||
t.Logf("task: %+v", tsk)
|
||||
if tsk.GetState() == task.Succeeded {
|
||||
if tsk.GetState() == task.SUCCEEDED {
|
||||
break
|
||||
}
|
||||
if tsk.GetState() == task.ERRORED {
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
package aria2
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/pkg/task"
|
||||
)
|
||||
|
||||
func ListFinished(ctx context.Context) []*task.Task[string] {
|
||||
return DownTaskManager.GetByStates(task.Succeeded, task.CANCELED, task.ERRORED)
|
||||
}
|
||||
|
||||
func ListUndone(ctx context.Context) []*task.Task[string] {
|
||||
return DownTaskManager.GetByStates(task.PENDING, task.RUNNING, task.CANCELING)
|
||||
}
|
|
@ -99,6 +99,14 @@ func (tm *Manager[K]) GetByStates(states ...string) []*Task[K] {
|
|||
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 NewTaskManager[K comparable](maxWorker int, updateID ...func(*K)) *Manager[K] {
|
||||
tm := &Manager[K]{
|
||||
tasks: generic_sync.MapOf[K, *Task[K]]{},
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
var (
|
||||
PENDING = "pending"
|
||||
RUNNING = "running"
|
||||
Succeeded = "succeeded"
|
||||
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 = Succeeded
|
||||
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 == Succeeded || 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 != Succeeded {
|
||||
if task.state != SUCCEEDED {
|
||||
t.Errorf("task status not finished: %s", task.state)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,92 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/aria2"
|
||||
"github.com/alist-org/alist/v3/internal/fs"
|
||||
"github.com/alist-org/alist/v3/server/common"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func UndoneDownTask(c *gin.Context) {
|
||||
common.SuccessResp(c, aria2.DownTaskManager.ListUndone())
|
||||
}
|
||||
|
||||
func DoneDownTask(c *gin.Context) {
|
||||
common.SuccessResp(c, aria2.DownTaskManager.ListDone())
|
||||
}
|
||||
|
||||
func CancelDownTask(c *gin.Context) {
|
||||
tid := c.Query("tid")
|
||||
if err := aria2.DownTaskManager.Cancel(tid); err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func UndoneTransferTask(c *gin.Context) {
|
||||
common.SuccessResp(c, aria2.TransferTaskManager.ListUndone())
|
||||
}
|
||||
|
||||
func DoneTransferTask(c *gin.Context) {
|
||||
common.SuccessResp(c, aria2.TransferTaskManager.ListDone())
|
||||
}
|
||||
|
||||
func CancelTransferTask(c *gin.Context) {
|
||||
id := c.Query("tid")
|
||||
tid, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if err := aria2.TransferTaskManager.Cancel(tid); err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func UndoneUploadTask(c *gin.Context) {
|
||||
common.SuccessResp(c, fs.UploadTaskManager.ListUndone())
|
||||
}
|
||||
|
||||
func DoneUploadTask(c *gin.Context) {
|
||||
common.SuccessResp(c, fs.UploadTaskManager.ListDone())
|
||||
}
|
||||
|
||||
func CancelUploadTask(c *gin.Context) {
|
||||
id := c.Query("tid")
|
||||
tid, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if err := fs.UploadTaskManager.Cancel(tid); err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
||||
func UndoneCopyTask(c *gin.Context) {
|
||||
common.SuccessResp(c, fs.CopyTaskManager.ListUndone())
|
||||
}
|
||||
|
||||
func DoneCopyTask(c *gin.Context) {
|
||||
common.SuccessResp(c, fs.CopyTaskManager.ListDone())
|
||||
}
|
||||
|
||||
func CancelCopyTask(c *gin.Context) {
|
||||
id := c.Query("tid")
|
||||
tid, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if err := fs.CopyTaskManager.Cancel(tid); err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
} else {
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,6 +53,20 @@ func Init(r *gin.Engine) {
|
|||
setting.POST("/reset_token", controllers.ResetToken)
|
||||
setting.POST("/set_aria2", controllers.SetAria2)
|
||||
|
||||
task := admin.Group("/task")
|
||||
task.GET("/down/undone", controllers.UndoneDownTask)
|
||||
task.GET("/down/done", controllers.DoneDownTask)
|
||||
task.POST("/down/cancel", controllers.CancelDownTask)
|
||||
task.POST("/transfer/undone", controllers.UndoneTransferTask)
|
||||
task.POST("/transfer/done", controllers.DoneTransferTask)
|
||||
task.POST("/transfer/cancel", controllers.CancelTransferTask)
|
||||
task.POST("/upload/undone", controllers.UndoneUploadTask)
|
||||
task.POST("/upload/done", controllers.DoneUploadTask)
|
||||
task.POST("/upload/cancel", controllers.CancelUploadTask)
|
||||
task.POST("/copy/undone", controllers.UndoneCopyTask)
|
||||
task.POST("/copy/done", controllers.DoneCopyTask)
|
||||
task.POST("/copy/cancel", controllers.CancelCopyTask)
|
||||
|
||||
// guest can
|
||||
public := api.Group("/public")
|
||||
public.GET("/settings", controllers.PublicSettings)
|
||||
|
|
Loading…
Reference in New Issue