2022-07-10 06:45:39 +00:00
|
|
|
// Package task manage task, such as file upload, file copy between storages, offline download, etc.
|
2022-06-17 13:23:44 +00:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-11 07:11:58 +00:00
|
|
|
"runtime"
|
2022-07-31 13:21:54 +00:00
|
|
|
|
2022-06-17 13:23:44 +00:00
|
|
|
"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
|
2023-11-06 08:56:55 +00:00
|
|
|
progress float64
|
2022-06-23 13:09:54 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-06 08:56:55 +00:00
|
|
|
func (t *Task[K]) SetProgress(percentage float64) {
|
2022-06-20 09:13:19 +00:00
|
|
|
t.progress = percentage
|
2022-06-18 12:06:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 08:56:55 +00:00
|
|
|
func (t Task[K]) GetProgress() float64 {
|
2022-06-29 12:28:02 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-04-11 07:11:58 +00:00
|
|
|
func getCurrentGoroutineStack() string {
|
|
|
|
buf := make([]byte, 1<<16)
|
|
|
|
n := runtime.Stack(buf, false)
|
|
|
|
return string(buf[:n])
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-04-11 07:11:58 +00:00
|
|
|
log.Errorf("error [%s] while run task [%s],stack trace:\n%s", err, t.Name, getCurrentGoroutineStack())
|
2022-06-18 12:38:14 +00:00
|
|
|
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-09-15 09:58:32 +00:00
|
|
|
t.SetProgress(100)
|
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-07-31 13:21:54 +00:00
|
|
|
func (t *Task[K]) Done() bool {
|
|
|
|
return t.state == SUCCEEDED || t.state == CANCELED || t.state == ERRORED
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|