From cb06d3a19a52350f463ae05131ff2c62b3221b44 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 31 Jul 2022 21:21:54 +0800 Subject: [PATCH] feat: remove and clear task --- pkg/task/errors.go | 1 + pkg/task/manager.go | 14 ++++++++- pkg/task/task.go | 5 +++ server/handles/task.go | 71 ++++++++++++++++++++++++++++++++++++++++++ server/router.go | 8 +++++ 5 files changed, 98 insertions(+), 1 deletion(-) diff --git a/pkg/task/errors.go b/pkg/task/errors.go index 022a7cf9267..3f2c4302e15 100644 --- a/pkg/task/errors.go +++ b/pkg/task/errors.go @@ -4,4 +4,5 @@ import "errors" var ( ErrTaskNotFound = errors.New("task not found") + ErrTaskRunning = errors.New("task is running") ) diff --git a/pkg/task/manager.go b/pkg/task/manager.go index d8c55bb20f3..3e98581e0a3 100644 --- a/pkg/task/manager.go +++ b/pkg/task/manager.go @@ -72,8 +72,16 @@ func (tm *Manager[K]) Cancel(tid K) error { return nil } -func (tm *Manager[K]) Remove(tid K) { +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 @@ -110,6 +118,10 @@ func (tm *Manager[K]) ListDone() []*Task[K] { return tm.GetByStates(SUCCEEDED, CANCELED, ERRORED) } +func (tm *Manager[K]) ClearDone() { + tm.RemoveByStates(SUCCEEDED, CANCELED, ERRORED) +} + func NewTaskManager[K comparable](maxWorker int, updateID ...func(*K)) *Manager[K] { tm := &Manager[K]{ tasks: generic_sync.MapOf[K, *Task[K]]{}, diff --git a/pkg/task/task.go b/pkg/task/task.go index fcee3db7198..8a44286e642 100644 --- a/pkg/task/task.go +++ b/pkg/task/task.go @@ -3,6 +3,7 @@ package task import ( "context" + "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -91,6 +92,10 @@ func (t *Task[K]) retry() { t.run() } +func (t *Task[K]) Done() bool { + return t.state == SUCCEEDED || t.state == CANCELED || t.state == ERRORED +} + func (t *Task[K]) Cancel() { if t.state == SUCCEEDED || t.state == CANCELED { return diff --git a/server/handles/task.go b/server/handles/task.go index 18b3c0ac50f..a1b0a956b58 100644 --- a/server/handles/task.go +++ b/server/handles/task.go @@ -74,6 +74,20 @@ func CancelDownTask(c *gin.Context) { } } +func RemoveDownTask(c *gin.Context) { + tid := c.Query("tid") + if err := aria2.DownTaskManager.Remove(tid); err != nil { + common.ErrorResp(c, err, 500) + } else { + common.SuccessResp(c) + } +} + +func ClearDoneDownTasks(c *gin.Context) { + aria2.DownTaskManager.ClearDone() + common.SuccessResp(c) +} + func UndoneTransferTask(c *gin.Context) { common.SuccessResp(c, getTaskInfosUint(aria2.TransferTaskManager.ListUndone())) } @@ -96,6 +110,25 @@ func CancelTransferTask(c *gin.Context) { } } +func RemoveTransferTask(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.Remove(tid); err != nil { + common.ErrorResp(c, err, 500) + } else { + common.SuccessResp(c) + } +} + +func ClearDoneTransferTasks(c *gin.Context) { + aria2.TransferTaskManager.ClearDone() + common.SuccessResp(c) +} + func UndoneUploadTask(c *gin.Context) { common.SuccessResp(c, getTaskInfosUint(fs.UploadTaskManager.ListUndone())) } @@ -118,6 +151,25 @@ func CancelUploadTask(c *gin.Context) { } } +func RemoveUploadTask(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.Remove(tid); err != nil { + common.ErrorResp(c, err, 500) + } else { + common.SuccessResp(c) + } +} + +func ClearDoneUploadTasks(c *gin.Context) { + fs.UploadTaskManager.ClearDone() + common.SuccessResp(c) +} + func UndoneCopyTask(c *gin.Context) { common.SuccessResp(c, getTaskInfosUint(fs.CopyTaskManager.ListUndone())) } @@ -139,3 +191,22 @@ func CancelCopyTask(c *gin.Context) { common.SuccessResp(c) } } + +func RemoveCopyTask(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.Remove(tid); err != nil { + common.ErrorResp(c, err, 500) + } else { + common.SuccessResp(c) + } +} + +func ClearDoneCopyTasks(c *gin.Context) { + fs.CopyTaskManager.ClearDone() + common.SuccessResp(c) +} diff --git a/server/router.go b/server/router.go index 212b6a275a2..9309683529f 100644 --- a/server/router.go +++ b/server/router.go @@ -72,15 +72,23 @@ func admin(g *gin.RouterGroup) { task.GET("/down/undone", handles.UndoneDownTask) task.GET("/down/done", handles.DoneDownTask) task.POST("/down/cancel", handles.CancelDownTask) + task.POST("/down/remove", handles.RemoveDownTask) + task.POST("/down/clear_done", handles.ClearDoneDownTasks) task.GET("/transfer/undone", handles.UndoneTransferTask) task.GET("/transfer/done", handles.DoneTransferTask) task.POST("/transfer/cancel", handles.CancelTransferTask) + task.POST("/transfer/remove", handles.RemoveTransferTask) + task.POST("/transfer/clear_done", handles.ClearDoneTransferTasks) task.GET("/upload/undone", handles.UndoneUploadTask) task.GET("/upload/done", handles.DoneUploadTask) task.POST("/upload/cancel", handles.CancelUploadTask) + task.POST("/upload/remove", handles.RemoveUploadTask) + task.POST("/upload/clear_done", handles.ClearDoneUploadTasks) task.GET("/copy/undone", handles.UndoneCopyTask) task.GET("/copy/done", handles.DoneCopyTask) task.POST("/copy/cancel", handles.CancelCopyTask) + task.POST("/copy/remove", handles.RemoveCopyTask) + task.POST("/copy/clear_done", handles.ClearDoneCopyTasks) ms := g.Group("/message") ms.GET("/get", message.PostInstance.GetHandle)