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
|
|
|
)
|
|
|
|
|
2022-06-21 08:14:37 +00:00
|
|
|
type Func[K comparable, V any] func(task *Task[K, V]) error
|
|
|
|
type Callback[K comparable, V any] func(task *Task[K, V])
|
|
|
|
|
|
|
|
type Task[K comparable, V any] struct {
|
|
|
|
ID K
|
|
|
|
Name string
|
|
|
|
Status string
|
|
|
|
Error error
|
|
|
|
|
|
|
|
Data V
|
|
|
|
|
|
|
|
Func Func[K, V]
|
|
|
|
callback Callback[K, V]
|
2022-06-17 13:23:44 +00:00
|
|
|
|
2022-06-18 12:06:45 +00:00
|
|
|
Ctx context.Context
|
2022-06-20 09:13:19 +00:00
|
|
|
progress int
|
2022-06-18 12:06:45 +00:00
|
|
|
cancel context.CancelFunc
|
2022-06-17 13:23:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 08:14:37 +00:00
|
|
|
func (t *Task[K, V]) SetStatus(status string) {
|
2022-06-17 13:23:44 +00:00
|
|
|
t.Status = status
|
|
|
|
}
|
|
|
|
|
2022-06-21 08:14:37 +00:00
|
|
|
func (t *Task[K, V]) SetProgress(percentage int) {
|
2022-06-20 09:13:19 +00:00
|
|
|
t.progress = percentage
|
2022-06-18 12:06:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 08:14:37 +00:00
|
|
|
func (t *Task[K, V]) 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-21 08:14:37 +00:00
|
|
|
func (t *Task[K, V]) retry() {
|
2022-06-18 12:38:14 +00:00
|
|
|
t.run()
|
2022-06-17 13:23:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 08:14:37 +00:00
|
|
|
func (t *Task[K, V]) 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
|
|
|
|
}
|
2022-06-21 08:14:37 +00:00
|
|
|
|
|
|
|
func WithCancelCtx[K comparable, V any](task *Task[K, V]) *Task[K, V] {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
task.Ctx = ctx
|
|
|
|
task.cancel = cancel
|
|
|
|
task.Status = PENDING
|
|
|
|
return task
|
|
|
|
}
|