Skip to content

Commit

Permalink
feat: task manage api
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 29, 2022
1 parent c88680b commit 0bf724f
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 20 deletions.
4 changes: 2 additions & 2 deletions internal/aria2/aria2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
14 changes: 0 additions & 14 deletions internal/aria2/manage.go

This file was deleted.

8 changes: 8 additions & 0 deletions pkg/task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]]{},
Expand Down
6 changes: 3 additions & 3 deletions pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var (
PENDING = "pending"
RUNNING = "running"
Succeeded = "succeeded"
SUCCEEDED = "succeeded"
CANCELING = "canceling"
CANCELED = "canceled"
ERRORED = "errored"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
91 changes: 91 additions & 0 deletions server/controllers/task.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
14 changes: 14 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0bf724f

Please sign in to comment.