Skip to content

Commit

Permalink
feat: remove and clear task
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jul 31, 2022
1 parent be452aa commit cb06d3a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/task/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import "errors"

var (
ErrTaskNotFound = errors.New("task not found")
ErrTaskRunning = errors.New("task is running")
)
14 changes: 13 additions & 1 deletion pkg/task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]]{},
Expand Down
5 changes: 5 additions & 0 deletions pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package task

import (
"context"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions server/handles/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand All @@ -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()))
}
Expand All @@ -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()))
}
Expand All @@ -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)
}
8 changes: 8 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cb06d3a

Please sign in to comment.