/
context.go
202 lines (176 loc) · 4.67 KB
/
context.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package core
import (
"context"
"fmt"
"path/filepath"
"strconv"
"time"
)
type TaskContext struct {
log CtxLogger
byteDown int64
byteUp int64
timeDown time.Duration
timeUp time.Duration
secStart int64
secEnd int64
parallelism int
failedTask int
invalidTask int
totalTask int
waitTask int
statChan chan int
Cache *LocalCache
Temp *LocalTemp
History *History
TarWriter []*ImageCompressedTarWriter
SingleWriter *SingleTarWriter
CompMeta *CompressionMetadata
SquashfsTar *SquashfsTar
Context context.Context
CancelFunc context.CancelFunc
Notify Notify
DockerTarget string
}
func NewTaskContext(log CtxLogger, lc *LocalCache, lt *LocalTemp) *TaskContext {
return &TaskContext{
log: log,
statChan: make(chan int, 1),
Cache: lc,
Temp: lt,
}
}
func (t *TaskContext) GetLogger() CtxLogger {
return t.log
}
func (t *TaskContext) Info(logStr string) {
t.log.Info(logStr)
}
func (t *TaskContext) Debug(logStr string) {
t.log.Debug(logStr)
}
func (t *TaskContext) Error(logStr string) {
t.log.Error(logStr)
}
func (t *TaskContext) Errorf(format string, args ...interface{}) error {
return t.log.Errorf(format, args...)
}
func (t *TaskContext) StatDown(size int64, duration time.Duration) {
t.statChan <- 1
defer func() {
<-t.statChan
}()
t.byteDown = t.byteDown + size
t.timeDown = t.timeDown + duration
}
func (t *TaskContext) StatUp(size int64, duration time.Duration) {
t.statChan <- 1
defer func() {
<-t.statChan
}()
t.byteUp = t.byteUp + size
t.timeUp = t.timeUp + duration
}
func (t *TaskContext) UpdateCurrentConn(n int) {
t.statChan <- 1
defer func() {
<-t.statChan
}()
t.parallelism = t.parallelism + n
}
func (t *TaskContext) Reset() {
t.byteDown = 0
t.byteUp = 0
t.timeDown = 1
t.timeUp = 1
t.secStart = 0
t.secEnd = 0
t.parallelism = 0
t.failedTask = 0
t.invalidTask = 0
t.totalTask = 0
t.waitTask = 0
t.SingleWriter = nil
t.TarWriter = nil
t.CompMeta = nil
t.SquashfsTar = nil
t.Context, t.CancelFunc = context.WithCancel(context.Background())
}
func (t *TaskContext) CloseTarWriter() {
for _, i := range t.TarWriter {
i.Close()
}
t.TarWriter = nil
}
func (t *TaskContext) CreateSquashfsTar(tempPath string, workPath string, squashfsFileName string) error {
var err error
t.SquashfsTar, err = NewSquashfsTar(tempPath, workPath, squashfsFileName)
return err
}
func (t *TaskContext) CreateSingleWriter(pathname string, filename string, compression string) error {
var err error
if CONF.DockerFile {
filename = filename + "_docker"
}
tarName := filename + "." + compression
t.Info(I18n.Sprintf("Create data file: %s", tarName))
t.CompMeta.AddDatafile(tarName, 0)
t.SingleWriter, err = NewSingleTarWriter(t, filepath.Join(pathname, tarName), compression)
return err
}
func (t *TaskContext) CreateCompressionMetadata(compressor string) error {
var err error
t.CompMeta, err = NewCompressionMetadata(compressor)
return err
}
func (t *TaskContext) CreateTarWriter(pathname string, filename string, compression string, num int) error {
t.TarWriter = make([]*ImageCompressedTarWriter, num)
for i := range t.TarWriter {
tarName := filename + "_" + strconv.Itoa(i) + "." + compression
t.Info(I18n.Sprintf("Create data file: %s", tarName))
t.CompMeta.AddDatafile(tarName, 0)
tar, err := NewImageCompressedTarWriter(filepath.Join(pathname, tarName), compression)
if err != nil {
return err
}
t.TarWriter[i] = tar
}
return nil
}
func (t *TaskContext) UpdateFailedTask(n int) {
t.failedTask = n
}
func (t *TaskContext) UpdateInvalidTask(n int) {
t.invalidTask = n
}
func (t *TaskContext) UpdateWaitTask(n int) {
t.waitTask = n
}
func (t *TaskContext) UpdateTotalTask(n int) {
t.totalTask = n
}
func (t *TaskContext) GetTotalTask() int {
return t.totalTask
}
func (t *TaskContext) Cancel() bool {
return t.Context.Err() != nil
}
func (t *TaskContext) UpdateSecStart(n int64) {
t.secStart = n
}
func (t *TaskContext) UpdateSecEnd(n int64) {
t.secEnd = n
}
func (t *TaskContext) GetStatus() string {
var totalSec int64 = 0
if t.secStart > 0 {
if t.secEnd > 0 {
totalSec = t.secEnd - t.secStart
} else {
totalSec = time.Now().Unix() - t.secStart
}
}
return fmt.Sprint(I18n.Sprintf("Invalid:%v All:%v OK:%v Err:%v Doing:%v Speed:v%s/s ^%s/s Total:v%s ^%s Time:%s",
t.invalidTask, t.totalTask, t.totalTask-t.waitTask-t.failedTask-t.parallelism, t.failedTask,
t.parallelism, FormatByteSize(int64(float64(t.byteDown)/(float64(t.timeDown)/float64(time.Second)))), FormatByteSize(int64(float64(t.byteUp)/(float64(t.timeUp)/float64(time.Second)))), FormatByteSize(t.byteDown), FormatByteSize(t.byteUp), FormatSeconds(totalSec)))
}