Merge pull request #76 from jmorganca/fix-pull

fix pull race
This commit is contained in:
Michael Yang 2023-07-12 19:21:13 -07:00 committed by GitHub
commit f2863cc7f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View file

@ -76,22 +76,10 @@ func saveModel(model *Model, fn func(total, completed int64)) error {
return fmt.Errorf("failed to download model: %w", err) return fmt.Errorf("failed to download model: %w", err)
} }
// check if completed file exists
fi, err := os.Stat(model.FullName())
switch {
case errors.Is(err, os.ErrNotExist):
// noop, file doesn't exist so create it
case err != nil:
return fmt.Errorf("stat: %w", err)
default:
fn(fi.Size(), fi.Size())
return nil
}
var size int64 var size int64
// completed file doesn't exist, check partial file // completed file doesn't exist, check partial file
fi, err = os.Stat(model.TempFile()) fi, err := os.Stat(model.TempFile())
switch { switch {
case errors.Is(err, os.ErrNotExist): case errors.Is(err, os.ErrNotExist):
// noop, file doesn't exist so create it // noop, file doesn't exist so create it

View file

@ -105,6 +105,24 @@ func pull(c *gin.Context) {
return return
} }
// check if completed file exists
fi, err := os.Stat(remote.FullName())
switch {
case errors.Is(err, os.ErrNotExist):
// noop, file doesn't exist so create it
case err != nil:
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
default:
c.JSON(http.StatusOK, api.PullProgress{
Total: fi.Size(),
Completed: fi.Size(),
Percent: 100,
})
return
}
ch := make(chan any) ch := make(chan any)
go stream(c, ch) go stream(c, ch)