Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/task/base.go
1560 views
1
package task
2
3
import (
4
"context"
5
"github.com/alist-org/alist/v3/internal/conf"
6
"github.com/alist-org/alist/v3/internal/model"
7
"github.com/xhofe/tache"
8
"sync"
9
"time"
10
)
11
12
type TaskExtension struct {
13
tache.Base
14
ctx context.Context
15
ctxInitMutex sync.Mutex
16
Creator *model.User
17
startTime *time.Time
18
endTime *time.Time
19
totalBytes int64
20
}
21
22
func (t *TaskExtension) SetCreator(creator *model.User) {
23
t.Creator = creator
24
t.Persist()
25
}
26
27
func (t *TaskExtension) GetCreator() *model.User {
28
return t.Creator
29
}
30
31
func (t *TaskExtension) SetStartTime(startTime time.Time) {
32
t.startTime = &startTime
33
}
34
35
func (t *TaskExtension) GetStartTime() *time.Time {
36
return t.startTime
37
}
38
39
func (t *TaskExtension) SetEndTime(endTime time.Time) {
40
t.endTime = &endTime
41
}
42
43
func (t *TaskExtension) GetEndTime() *time.Time {
44
return t.endTime
45
}
46
47
func (t *TaskExtension) ClearEndTime() {
48
t.endTime = nil
49
}
50
51
func (t *TaskExtension) SetTotalBytes(totalBytes int64) {
52
t.totalBytes = totalBytes
53
}
54
55
func (t *TaskExtension) GetTotalBytes() int64 {
56
return t.totalBytes
57
}
58
59
func (t *TaskExtension) Ctx() context.Context {
60
if t.ctx == nil {
61
t.ctxInitMutex.Lock()
62
if t.ctx == nil {
63
t.ctx = context.WithValue(t.Base.Ctx(), "user", t.Creator)
64
}
65
t.ctxInitMutex.Unlock()
66
}
67
return t.ctx
68
}
69
70
func (t *TaskExtension) ReinitCtx() {
71
if !conf.Conf.Tasks.AllowRetryCanceled {
72
return
73
}
74
select {
75
case <-t.Base.Ctx().Done():
76
ctx, cancel := context.WithCancel(context.Background())
77
t.SetCtx(ctx)
78
t.SetCancelFunc(cancel)
79
t.ctx = nil
80
default:
81
}
82
}
83
84
type TaskExtensionInfo interface {
85
tache.TaskWithInfo
86
GetCreator() *model.User
87
GetStartTime() *time.Time
88
GetEndTime() *time.Time
89
GetTotalBytes() int64
90
}
91
92