Skip to content

Commit

Permalink
Feature: bi5 downloader will download to temp folder before moving it…
Browse files Browse the repository at this point in the history
… to final destination location.

This reduce the likelyhood for file to be corrupted in the even of program being stopped while downloading
Bug: Panic will occur when the requested ticks#Goto target is during weekend.
  • Loading branch information
edward-yakop committed Jan 27, 2021
1 parent 4c253c7 commit e638106
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
6 changes: 3 additions & 3 deletions api/tickdata/ticks/ticks.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (t *Ticks) Goto(to time.Time) (isSuccess bool, err error) {
return false, errors.New("[" + to.String() + "] is after [" + t.end.String() + "]")
}

to = to.In(time.UTC) // Done to ease debugging
to = to.In(time.UTC) // To ease debugging
t.isCompleted = false
for currTime := t.timeToHour(to); currTime.Before(t.end); currTime = currTime.Add(time.Hour) {
if t.ticksDayHour.Equal(currTime) {
Expand All @@ -80,12 +80,12 @@ func (t *Ticks) Goto(to time.Time) (isSuccess bool, err error) {
// Download might return errors when there's no tick data during weekend or holiday
if bi.Download() == nil {
t.ticks, err = bi.Ticks()
t.ticksIdx = 0
t.ticksDayHour = currTime
if err != nil {
t.complete()
return
} else if len(t.ticks) != 0 {
t.ticksIdx = 0
t.ticksDayHour = currTime
t.seek(to)
return true, nil
}
Expand Down
34 changes: 24 additions & 10 deletions internal/core/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (h HTTPDownload) Download(URL string, toFilePath string) (httpStatusCode in
break
}

log.Warn("[%d] Download %s failed %d:%s. Retrying", retry, URL, httpStatusCode, resp.Status)
log.Trace("[%d] Download %s failed %d:%s. Retrying", retry, URL, httpStatusCode, resp.Status)
err = fmt.Errorf("http error %d:%s", resp.StatusCode, resp.Status)
h.delay()
continue
Expand All @@ -69,26 +70,39 @@ func (h HTTPDownload) delay() {
}

func (h HTTPDownload) saveBodyToDisk(body io.ReadCloser, path string) (filesize int64, err error) {
// Create dir if not exists
tempFile, err := ioutil.TempFile(os.TempDir(), "go-duka.download.*.temp")
if err != nil {
return 0, errors.New("Failed to create temp file for download")
}
tempFileName := tempFile.Name()
defer func() {
if err != nil {
_ = tempFile.Close()
_ = os.Remove(tempFileName)
}
}()

filesize, err = io.Copy(tempFile, body)
if err != nil {
err = errors.Wrap(err, "Saving tick data ["+tempFileName+"] Failed")
return
}

dir := filepath.Dir(path)
err = os.MkdirAll(dir, 0755)
if err != nil {
err = errors.Wrap(err, "Create folder ["+dir+"] failed")
return
}

f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
err = tempFile.Close()
if err != nil {
err = errors.Wrap(err, "Create file ["+path+"] failed")
err = errors.Wrap(err, "Failed to close tick data ["+tempFileName+"] file")
return
}

defer f.Close()
filesize, err = io.Copy(f, body)
err = os.Rename(tempFileName, path)
if err != nil {
err = errors.Wrap(err, "Saving tick data ["+path+"] failed")
return
err = errors.Wrap(err, "Failed to move tick data to ["+path+"]")
}

return
}

0 comments on commit e638106

Please sign in to comment.