Use a separate work dir to keep the site available until the upload has been extracted

This commit is contained in:
Melon 2025-01-08 18:35:07 +00:00
parent 5f50ec5a56
commit 20de90afe8
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 18 additions and 7 deletions

View File

@ -113,6 +113,7 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
siteBranchPath := filepath.Join(site, "@"+branch)
siteBranchOldPath := filepath.Join(site, "old@"+branch)
siteBranchWorkPath := filepath.Join(site, "work@"+branch)
// try the new "old@[...]" and old "@[...].old" paths
err = h.storageFs.RemoveAll(siteBranchPath + ".old")
@ -124,16 +125,11 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
return fmt.Errorf("failed to remove old site branch %s: %w", siteBranchPath, err)
}
err = h.storageFs.Rename(siteBranchPath, siteBranchOldPath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to save an old copy of the site: %w", err)
}
err = h.storageFs.MkdirAll(siteBranchPath, fs.ModePerm)
err = h.storageFs.MkdirAll(siteBranchWorkPath, fs.ModePerm)
if err != nil {
return fmt.Errorf("failed to make site directory: %w", err)
}
branchFs := afero.NewBasePathFs(h.storageFs, siteBranchPath)
branchFs := afero.NewBasePathFs(h.storageFs, siteBranchWorkPath)
// decompress gzip wrapper
gzipReader, err := gzip.NewReader(fileData)
@ -169,6 +165,18 @@ func (h *Handler) extractTarGzUpload(fileData io.Reader, site, branch string) er
}
}
// TODO(melon): I would love to use unix.Renameat2 but due to afero this will not work
err = h.storageFs.Rename(siteBranchPath, siteBranchOldPath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to save an old copy of the site: %w", err)
}
err = h.storageFs.Rename(siteBranchWorkPath, siteBranchPath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to save an old copy of the site: %w", err)
}
n := time.Now().UTC()
err = h.db.AddBranch(context.Background(), database.AddBranchParams{

View File

@ -53,6 +53,9 @@ func assertUploadedFile(t *testing.T, fs afero.Fs, branch string) {
assert.False(t, stat.IsDir())
assert.Equal(t, int64(13), stat.Size())
stat, err = fs.Stat("example.com/work@" + branch + "/test.txt")
assert.Error(t, err)
// check contents
o, err := fs.Open("example.com/@" + branch + "/test.txt")
assert.NoError(t, err)