forked from xhofe/tache
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.go
More file actions
100 lines (79 loc) · 1.6 KB
/
Copy pathbase.go
File metadata and controls
100 lines (79 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package tache
import "context"
// Base is the base struct for all tasks to implement TaskBase interface
type Base struct {
ID string `json:"id"`
State State `json:"state"`
Retry int `json:"retry"`
MaxRetry int `json:"max_retry"`
progress float64
err error
ctx context.Context
cancel context.CancelFunc
persist func()
}
func (b *Base) SetProgress(progress float64) {
b.progress = progress
b.Persist()
}
func (b *Base) GetProgress() float64 {
return b.progress
}
func (b *Base) SetState(state State) {
b.State = state
b.Persist()
}
func (b *Base) GetState() State {
return b.State
}
func (b *Base) GetID() string {
return b.ID
}
func (b *Base) SetID(id string) {
b.ID = id
b.Persist()
}
func (b *Base) SetErr(err error) {
b.err = err
b.Persist()
}
func (b *Base) GetErr() error {
return b.err
}
func (b *Base) CtxDone() <-chan struct{} {
return b.Ctx().Done()
}
func (b *Base) SetCtx(ctx context.Context) {
b.ctx = ctx
}
func (b *Base) SetCancelFunc(cancelFunc context.CancelFunc) {
b.cancel = cancelFunc
}
func (b *Base) GetRetry() (int, int) {
return b.Retry, b.MaxRetry
}
func (b *Base) SetRetry(retry int, maxRetry int) {
b.Retry, b.MaxRetry = retry, maxRetry
}
func (b *Base) Cancel() {
switch b.State {
case StateSucceeded, StateCanceled, StateFailed:
return
}
if !isCanceled(b.ctx) {
b.SetState(StateCanceling)
b.cancel()
}
}
func (b *Base) Ctx() context.Context {
return b.ctx
}
func (b *Base) Persist() {
if b.persist != nil {
b.persist()
}
}
func (b *Base) SetPersist(persist func()) {
b.persist = persist
}
var _ TaskBase = (*Base)(nil)