Add Download Progress
This commit is contained in:
parent
628847388d
commit
cd04c2d16e
1 changed files with 39 additions and 1 deletions
|
@ -13,6 +13,7 @@ import (
|
|||
"path/filepath"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/config"
|
||||
"git.lastassault.de/speatzle/morffix/constants"
|
||||
|
@ -20,6 +21,17 @@ import (
|
|||
"git.lastassault.de/speatzle/morffix/types"
|
||||
)
|
||||
|
||||
type countReader struct {
|
||||
io.Reader
|
||||
n int
|
||||
}
|
||||
|
||||
func (w *countReader) Read(p []byte) (int, error) {
|
||||
n, err := w.Reader.Read(p)
|
||||
w.n += n
|
||||
return n, err
|
||||
}
|
||||
|
||||
// dropCR drops a terminal \r from the data.
|
||||
func dropCR(data []byte) []byte {
|
||||
if len(data) > 0 && data[len(data)-1] == '\r' {
|
||||
|
@ -109,8 +121,34 @@ func RunHealthCheck(conf config.Config, t *types.Task, data types.HealthCheckDat
|
|||
hash := md5.New()
|
||||
tr := io.TeeReader(resp.Body, hash)
|
||||
|
||||
cr := &countReader{Reader: tr}
|
||||
|
||||
stopProgress := make(chan bool, 1)
|
||||
|
||||
go func() {
|
||||
tik := time.NewTicker(time.Second)
|
||||
|
||||
lastCount := 0
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-tik.C:
|
||||
speed := cr.n - lastCount
|
||||
l.InfoContext(ctx, "Download Progress", "bytes", cr.n, "lastCount", lastCount, "length", resp.ContentLength, "speed", speed)
|
||||
lastCount = cr.n
|
||||
case <-stopProgress:
|
||||
tik.Stop()
|
||||
}
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
stopProgress <- true
|
||||
}()
|
||||
|
||||
defer resp.Body.Close()
|
||||
n, err := io.Copy(out, tr)
|
||||
n, err := io.Copy(out, cr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Reading File: %w", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue