Browse Source

feat: retry when asset download failed

v1.x.x
dnomd343 2 years ago
parent
commit
600c6d1043
  1. 6
      cmd/asset/update.go
  2. 8
      cmd/common/file.go

6
cmd/asset/update.go

@ -25,8 +25,12 @@ func updateAsset(urls map[string]string, assetDir string, updateProxy string) {
if len(urls) != 0 {
log.Info("Start update assets")
for file, url := range urls {
common.DownloadFile(url, path.Join(assetDir, file), updateProxy) // maybe override old asset
if !common.DownloadFile(url, path.Join(assetDir, file), updateProxy) { // maybe override old asset
log.Infof("Try to download asset `%s` again", file)
common.DownloadFile(url, path.Join(assetDir, file), updateProxy) // download retry
}
}
log.Infof("Assets update complete")
}
}

8
cmd/common/file.go

@ -74,7 +74,7 @@ func CopyFile(source string, target string) {
}
}
func DownloadFile(fileUrl string, filePath string, proxyUrl string) {
func DownloadFile(fileUrl string, filePath string, proxyUrl string) bool {
log.Debugf("File download `%s` => `%s`", fileUrl, filePath)
client := http.Client{}
if proxyUrl != "" { // use proxy for download
@ -94,7 +94,7 @@ func DownloadFile(fileUrl string, filePath string, proxyUrl string) {
}()
if err != nil {
log.Errorf("Download `%s` error -> %v", fileUrl, err)
return
return false
}
output, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
defer output.Close()
@ -102,7 +102,9 @@ func DownloadFile(fileUrl string, filePath string, proxyUrl string) {
log.Panicf("Open `%s` error -> %v", filePath, err)
}
if _, err = io.Copy(output, resp.Body); err != nil {
log.Panicf("File `%s` save error -> %v", filePath, err)
log.Errorf("File `%s` save error -> %v", filePath, err)
return false
}
log.Infof("Download success `%s` => `%s`", fileUrl, filePath)
return true
}

Loading…
Cancel
Save