alist/pkg/task/task.go

93 lines
1.7 KiB
Go
Raw Normal View History

2022-06-17 13:23:44 +00:00
// Package task manage task, such as file upload, file copy between accounts, offline download, etc.
package task
import (
"context"
"github.com/pkg/errors"
2022-06-18 12:38:14 +00:00
log "github.com/sirupsen/logrus"
2022-06-17 13:23:44 +00:00
)
var (
PENDING = "pending"
RUNNING = "running"
FINISHED = "finished"
CANCELING = "canceling"
CANCELED = "canceled"
2022-06-18 12:38:14 +00:00
ERRORED = "errored"
2022-06-17 13:23:44 +00:00
)
type Func func(task *Task) error
2022-06-20 09:13:19 +00:00
type Callback func(task *Task)
2022-06-17 13:23:44 +00:00
type Task struct {
2022-06-18 12:06:45 +00:00
ID uint64
Name string
Status string
Error error
Func Func
Ctx context.Context
2022-06-20 09:13:19 +00:00
progress int
callback Callback
2022-06-18 12:06:45 +00:00
cancel context.CancelFunc
2022-06-17 13:23:44 +00:00
}
2022-06-20 09:13:19 +00:00
func newTask(name string, func_ Func, callbacks ...Callback) *Task {
2022-06-17 13:23:44 +00:00
ctx, cancel := context.WithCancel(context.Background())
2022-06-20 09:13:19 +00:00
t := &Task{
2022-06-17 13:23:44 +00:00
Name: name,
Status: PENDING,
Func: func_,
Ctx: ctx,
cancel: cancel,
}
2022-06-20 09:13:19 +00:00
if len(callbacks) > 0 {
t.callback = callbacks[0]
}
return t
2022-06-17 13:23:44 +00:00
}
func (t *Task) SetStatus(status string) {
t.Status = status
}
2022-06-18 12:06:45 +00:00
func (t *Task) SetProgress(percentage int) {
2022-06-20 09:13:19 +00:00
t.progress = percentage
2022-06-18 12:06:45 +00:00
}
2022-06-18 12:38:14 +00:00
func (t *Task) run() {
2022-06-17 13:23:44 +00:00
t.Status = RUNNING
2022-06-18 12:38:14 +00:00
defer func() {
if err := recover(); err != nil {
log.Errorf("error [%+v] while run task [%s]", err, t.Name)
t.Error = errors.Errorf("panic: %+v", err)
t.Status = ERRORED
}
}()
2022-06-17 13:23:44 +00:00
t.Error = t.Func(t)
if errors.Is(t.Ctx.Err(), context.Canceled) {
t.Status = CANCELED
2022-06-18 12:38:14 +00:00
} else if t.Error != nil {
t.Status = ERRORED
2022-06-17 13:23:44 +00:00
} else {
t.Status = FINISHED
2022-06-20 09:13:19 +00:00
if t.callback != nil {
t.callback(t)
}
2022-06-17 13:23:44 +00:00
}
}
2022-06-18 12:38:14 +00:00
func (t *Task) retry() {
t.run()
2022-06-17 13:23:44 +00:00
}
func (t *Task) Cancel() {
2022-06-20 09:13:19 +00:00
if t.Status == FINISHED || t.Status == CANCELED {
return
}
2022-06-17 13:23:44 +00:00
if t.cancel != nil {
t.cancel()
}
// maybe can't cancel
t.Status = CANCELING
}