Skip to content

Commit

Permalink
fix: serialize task info
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 29, 2022
1 parent 786e44d commit f54418b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
6 changes: 4 additions & 2 deletions internal/aria2/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ outer:
if err != nil {
return err
}
m.tsk.SetStatus("aria2 download completed, waiting for transfer")
m.tsk.SetStatus("aria2 download completed, transferring")
<-m.finish
m.tsk.SetStatus("completed")
return nil
}

Expand Down Expand Up @@ -88,7 +89,8 @@ func (m *Monitor) Update() (bool, error) {
if err != nil {
downloaded = 0
}
m.tsk.SetProgress(int(float64(downloaded) / float64(total)))
progress := float64(downloaded) / float64(total) * 100
m.tsk.SetProgress(int(progress))
switch info.Status {
case "complete":
err := m.Complete()
Expand Down
17 changes: 16 additions & 1 deletion pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,25 @@ func (t *Task[K]) SetProgress(percentage int) {
t.progress = percentage
}

func (t *Task[K]) GetState() string {
func (t Task[K]) GetProgress() int {
return t.progress
}

func (t Task[K]) GetState() string {
return t.state
}

func (t Task[K]) GetStatus() string {
return t.status
}

func (t Task[K]) GetErrMsg() string {
if t.Error == nil {
return ""
}
return t.Error.Error()
}

func (t *Task[K]) run() {
t.state = RUNNING
defer func() {
Expand Down
64 changes: 56 additions & 8 deletions server/controllers/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,65 @@ 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/pkg/task"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"strconv"
)

type TaskInfo struct {
ID string `json:"id"`
Name string `json:"name"`
State string `json:"state"`
Status string `json:"status"`
Progress int `json:"progress"`
Error string `json:"error"`
}

func getTaskInfoUint(task *task.Task[uint64]) TaskInfo {
return TaskInfo{
ID: strconv.FormatUint(task.ID, 10),
Name: task.Name,
State: task.GetState(),
Status: task.GetStatus(),
Progress: task.GetProgress(),
Error: task.Error.Error(),
}
}

func getTaskInfoStr(task *task.Task[string]) TaskInfo {
return TaskInfo{
ID: task.ID,
Name: task.Name,
State: task.GetState(),
Status: task.GetStatus(),
Progress: task.GetProgress(),
Error: task.GetErrMsg(),
}
}

func getTaskInfosUint(tasks []*task.Task[uint64]) []TaskInfo {
var infos []TaskInfo
for _, t := range tasks {
infos = append(infos, getTaskInfoUint(t))
}
return infos
}

func getTaskInfosStr(tasks []*task.Task[string]) []TaskInfo {
var infos []TaskInfo
for _, t := range tasks {
infos = append(infos, getTaskInfoStr(t))
}
return infos
}

func UndoneDownTask(c *gin.Context) {
common.SuccessResp(c, aria2.DownTaskManager.ListUndone())
common.SuccessResp(c, getTaskInfosStr(aria2.DownTaskManager.ListUndone()))
}

func DoneDownTask(c *gin.Context) {
common.SuccessResp(c, aria2.DownTaskManager.ListDone())
common.SuccessResp(c, getTaskInfosStr(aria2.DownTaskManager.ListDone()))
}

func CancelDownTask(c *gin.Context) {
Expand All @@ -26,11 +74,11 @@ func CancelDownTask(c *gin.Context) {
}

func UndoneTransferTask(c *gin.Context) {
common.SuccessResp(c, aria2.TransferTaskManager.ListUndone())
common.SuccessResp(c, getTaskInfosUint(aria2.TransferTaskManager.ListUndone()))
}

func DoneTransferTask(c *gin.Context) {
common.SuccessResp(c, aria2.TransferTaskManager.ListDone())
common.SuccessResp(c, getTaskInfosUint(aria2.TransferTaskManager.ListDone()))
}

func CancelTransferTask(c *gin.Context) {
Expand All @@ -48,11 +96,11 @@ func CancelTransferTask(c *gin.Context) {
}

func UndoneUploadTask(c *gin.Context) {
common.SuccessResp(c, fs.UploadTaskManager.ListUndone())
common.SuccessResp(c, getTaskInfosUint(fs.UploadTaskManager.ListUndone()))
}

func DoneUploadTask(c *gin.Context) {
common.SuccessResp(c, fs.UploadTaskManager.ListDone())
common.SuccessResp(c, getTaskInfosUint(fs.UploadTaskManager.ListDone()))
}

func CancelUploadTask(c *gin.Context) {
Expand All @@ -70,11 +118,11 @@ func CancelUploadTask(c *gin.Context) {
}

func UndoneCopyTask(c *gin.Context) {
common.SuccessResp(c, fs.CopyTaskManager.ListUndone())
common.SuccessResp(c, getTaskInfosUint(fs.CopyTaskManager.ListUndone()))
}

func DoneCopyTask(c *gin.Context) {
common.SuccessResp(c, fs.CopyTaskManager.ListDone())
common.SuccessResp(c, getTaskInfosUint(fs.CopyTaskManager.ListDone()))
}

func CancelCopyTask(c *gin.Context) {
Expand Down

0 comments on commit f54418b

Please sign in to comment.