/
status.go
166 lines (146 loc) · 2.97 KB
/
status.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package flow
import (
"fmt"
"sync"
"time"
)
type Status uint8
var statusMap = map[uint8]string{
0: "NOT_EXIST",
1: "WAIT_TO_START",
2: "PROCESSING",
3: "ERROR",
4: "CANCELLING",
5: "CANCELLED",
6: "FINISHED",
}
func (s Status) String() string {
return statusMap[uint8(s)]
}
const (
NOT_EXIST Status = iota
WAIT_TO_START
PROCESSING
ERROR
CANCELLING
CANCELLED
FINISHED
)
type flowStatus struct {
mu sync.Mutex
status Status
started, ended time.Time
description string
countRead uint64
countWrite uint64
countMax uint64
}
type FlowStatus struct {
Status Status
Started, Ended time.Time
Description string
CountRead uint64
CountWrite uint64
CountMax uint64
}
func newFlowStatus() *flowStatus {
return &flowStatus{
mu: sync.Mutex{},
status: WAIT_TO_START,
}
}
func (fs *flowStatus) updateCounts(countRead, countWrite, countMax uint64) {
fs.mu.Lock()
fs.countRead = countRead
fs.countWrite = countWrite
fs.countMax = countMax
fs.mu.Unlock()
}
func (fs *flowStatus) isStartable() bool {
fs.mu.Lock()
status := fs.status
fs.mu.Unlock()
return status == WAIT_TO_START
}
func (fs *flowStatus) isRestartable() bool {
fs.mu.Lock()
status := fs.status
fs.mu.Unlock()
return status == WAIT_TO_START || status == FINISHED
}
func (fs *flowStatus) isCancellable() bool {
fs.mu.Lock()
status := fs.status
fs.mu.Unlock()
return status == WAIT_TO_START || status == PROCESSING
}
func (fs *flowStatus) isRunning() bool {
fs.mu.Lock()
status := fs.status
fs.mu.Unlock()
return status == PROCESSING
}
func (fs *flowStatus) start() error {
if !fs.isStartable() {
return fmt.Errorf("cannot start because the Flow is in status: %s", fs.status)
}
fs.mu.Lock()
defer fs.mu.Unlock()
fs.status = PROCESSING
fs.started = time.Now()
fs.ended = time.Time{}
fs.description = ""
return nil
}
func (fs *flowStatus) cancelling() error {
if !fs.isCancellable() {
return fmt.Errorf("cannot cancell because the Flow is in status: %s", fs.status)
}
fs.mu.Lock()
defer fs.mu.Unlock()
fs.status = CANCELLING
return nil
}
func (fs *flowStatus) cancell() {
fs.mu.Lock()
fs.status = CANCELLED
fs.ended = time.Now()
fs.mu.Unlock()
}
func (fs *flowStatus) error(err string) {
fs.mu.Lock()
if fs.status == NOT_EXIST {
fs.started = time.Now()
}
fs.status = ERROR
fs.ended = time.Now()
fs.description = err
fs.mu.Unlock()
}
func (fs *flowStatus) finish() {
fs.mu.Lock()
fs.status = FINISHED
fs.ended = time.Now()
fs.mu.Unlock()
}
func (fs *flowStatus) restart() error {
if !fs.isRestartable() {
return fmt.Errorf("cannot restart flow, because status is: %v", fs.status)
}
fs.mu.Lock()
defer fs.mu.Unlock()
fs = &flowStatus{
mu: sync.Mutex{},
status: WAIT_TO_START,
}
return nil
}
func (fs *flowStatus) get() FlowStatus {
fs.mu.Lock()
status := FlowStatus{
fs.status, fs.started, fs.ended,
fs.description, fs.countRead, fs.countWrite, fs.countMax,
}
fs.mu.Unlock()
return status
}