Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/task/task.go
1560 views
1
// Package task manage task, such as file upload, file copy between storages, offline download, etc.
2
package task
3
4
import (
5
"context"
6
"runtime"
7
8
"github.com/pkg/errors"
9
log "github.com/sirupsen/logrus"
10
)
11
12
var (
13
PENDING = "pending"
14
RUNNING = "running"
15
SUCCEEDED = "succeeded"
16
CANCELING = "canceling"
17
CANCELED = "canceled"
18
ERRORED = "errored"
19
)
20
21
type Func[K comparable] func(task *Task[K]) error
22
type Callback[K comparable] func(task *Task[K])
23
24
type Task[K comparable] struct {
25
ID K
26
Name string
27
state string // pending, running, finished, canceling, canceled, errored
28
status string
29
progress float64
30
31
Error error
32
33
Func Func[K]
34
callback Callback[K]
35
36
Ctx context.Context
37
cancel context.CancelFunc
38
}
39
40
func (t *Task[K]) SetStatus(status string) {
41
t.status = status
42
}
43
44
func (t *Task[K]) SetProgress(percentage float64) {
45
t.progress = percentage
46
}
47
48
func (t Task[K]) GetProgress() float64 {
49
return t.progress
50
}
51
52
func (t Task[K]) GetState() string {
53
return t.state
54
}
55
56
func (t Task[K]) GetStatus() string {
57
return t.status
58
}
59
60
func (t Task[K]) GetErrMsg() string {
61
if t.Error == nil {
62
return ""
63
}
64
return t.Error.Error()
65
}
66
67
func getCurrentGoroutineStack() string {
68
buf := make([]byte, 1<<16)
69
n := runtime.Stack(buf, false)
70
return string(buf[:n])
71
}
72
73
func (t *Task[K]) run() {
74
t.state = RUNNING
75
defer func() {
76
if err := recover(); err != nil {
77
log.Errorf("error [%s] while run task [%s],stack trace:\n%s", err, t.Name, getCurrentGoroutineStack())
78
t.Error = errors.Errorf("panic: %+v", err)
79
t.state = ERRORED
80
}
81
}()
82
t.Error = t.Func(t)
83
if t.Error != nil {
84
log.Errorf("error [%+v] while run task [%s]", t.Error, t.Name)
85
}
86
if errors.Is(t.Ctx.Err(), context.Canceled) {
87
t.state = CANCELED
88
} else if t.Error != nil {
89
t.state = ERRORED
90
} else {
91
t.state = SUCCEEDED
92
t.SetProgress(100)
93
if t.callback != nil {
94
t.callback(t)
95
}
96
}
97
}
98
99
func (t *Task[K]) retry() {
100
t.run()
101
}
102
103
func (t *Task[K]) Done() bool {
104
return t.state == SUCCEEDED || t.state == CANCELED || t.state == ERRORED
105
}
106
107
func (t *Task[K]) Cancel() {
108
if t.state == SUCCEEDED || t.state == CANCELED {
109
return
110
}
111
if t.cancel != nil {
112
t.cancel()
113
}
114
// maybe can't cancel
115
t.state = CANCELING
116
}
117
118
func WithCancelCtx[K comparable](task *Task[K]) *Task[K] {
119
ctx, cancel := context.WithCancel(context.Background())
120
task.Ctx = ctx
121
task.cancel = cancel
122
task.state = PENDING
123
return task
124
}
125
126