alist/pkg/task/task.go

112 lines
2.0 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"
2022-06-29 10:36:14 +00:00
SUCCEEDED = "succeeded"
2022-06-17 13:23:44 +00:00
CANCELING = "canceling"
CANCELED = "canceled"
2022-06-18 12:38:14 +00:00
ERRORED = "errored"
2022-06-17 13:23:44 +00:00
)
2022-06-22 11:28:41 +00:00
type Func[K comparable] func(task *Task[K]) error
type Callback[K comparable] func(task *Task[K])
2022-06-21 08:14:37 +00:00
2022-06-22 11:28:41 +00:00
type Task[K comparable] struct {
2022-06-23 13:09:54 +00:00
ID K
Name string
state string // pending, running, finished, canceling, canceled, errored
status string
progress int
Error error
2022-06-21 08:14:37 +00:00
2022-06-22 11:28:41 +00:00
Func Func[K]
callback Callback[K]
2022-06-17 13:23:44 +00:00
2022-06-23 13:09:54 +00:00
Ctx context.Context
cancel context.CancelFunc
2022-06-17 13:23:44 +00:00
}
2022-06-22 11:28:41 +00:00
func (t *Task[K]) SetStatus(status string) {
2022-06-23 13:09:54 +00:00
t.status = status
2022-06-17 13:23:44 +00:00
}
2022-06-22 11:28:41 +00:00
func (t *Task[K]) SetProgress(percentage int) {
2022-06-20 09:13:19 +00:00
t.progress = percentage
2022-06-18 12:06:45 +00:00
}
2022-06-29 12:28:02 +00:00
func (t Task[K]) GetProgress() int {
return t.progress
}
func (t Task[K]) GetState() string {
2022-06-23 13:09:54 +00:00
return t.state
}
2022-06-29 12:28:02 +00:00
func (t Task[K]) GetStatus() string {
return t.status
}
func (t Task[K]) GetErrMsg() string {
if t.Error == nil {
return ""
}
return t.Error.Error()
}
2022-06-22 11:28:41 +00:00
func (t *Task[K]) run() {
2022-06-23 13:09:54 +00:00
t.state = 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)
2022-06-23 13:09:54 +00:00
t.state = ERRORED
2022-06-18 12:38:14 +00:00
}
}()
2022-06-17 13:23:44 +00:00
t.Error = t.Func(t)
2022-06-21 09:37:02 +00:00
if t.Error != nil {
log.Errorf("error [%+v] while run task [%s]", t.Error, t.Name)
}
2022-06-17 13:23:44 +00:00
if errors.Is(t.Ctx.Err(), context.Canceled) {
2022-06-23 13:09:54 +00:00
t.state = CANCELED
2022-06-18 12:38:14 +00:00
} else if t.Error != nil {
2022-06-23 13:09:54 +00:00
t.state = ERRORED
2022-06-17 13:23:44 +00:00
} else {
2022-06-29 10:36:14 +00:00
t.state = SUCCEEDED
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-22 11:28:41 +00:00
func (t *Task[K]) retry() {
2022-06-18 12:38:14 +00:00
t.run()
2022-06-17 13:23:44 +00:00
}
2022-06-22 11:28:41 +00:00
func (t *Task[K]) Cancel() {
2022-06-29 10:36:14 +00:00
if t.state == SUCCEEDED || t.state == CANCELED {
2022-06-20 09:13:19 +00:00
return
}
2022-06-17 13:23:44 +00:00
if t.cancel != nil {
t.cancel()
}
// maybe can't cancel
2022-06-23 13:09:54 +00:00
t.state = CANCELING
2022-06-17 13:23:44 +00:00
}
2022-06-21 08:14:37 +00:00
2022-06-22 11:28:41 +00:00
func WithCancelCtx[K comparable](task *Task[K]) *Task[K] {
2022-06-21 08:14:37 +00:00
ctx, cancel := context.WithCancel(context.Background())
task.Ctx = ctx
task.cancel = cancel
2022-06-23 13:09:54 +00:00
task.state = PENDING
2022-06-21 08:14:37 +00:00
return task
}