Measure server data and registered worktree storage safely
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
type storageInventoryConfig struct {
|
||||
DatabasePath, DataRoot, ArtifactsRoot, AttachmentsRoot, SessionsRoot string
|
||||
WorkspaceRoots, ProtectedWorkspaces []string
|
||||
}
|
||||
|
||||
type storageCategory struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Bytes int64 `json:"bytes"`
|
||||
Files int64 `json:"files"`
|
||||
Partial bool `json:"partial,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
ModifiedAt time.Time `json:"-"`
|
||||
}
|
||||
|
||||
type storageWorktree struct {
|
||||
Path string `json:"path"`
|
||||
Repository string `json:"repository"`
|
||||
Branch string `json:"branch,omitempty"`
|
||||
Bytes int64 `json:"bytes"`
|
||||
Files int64 `json:"files"`
|
||||
ModifiedAt time.Time `json:"modified_at"`
|
||||
Eligible bool `json:"eligible"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
locked bool
|
||||
}
|
||||
|
||||
type storageInventory struct {
|
||||
TotalBytes int64
|
||||
Categories []storageCategory
|
||||
Worktrees []storageWorktree
|
||||
Partial bool
|
||||
Errors []string
|
||||
}
|
||||
|
||||
// Inventory counts logical regular-file bytes. It never follows symlinks, and
|
||||
// each inode is counted once across nested roots and hard-linked files.
|
||||
func inspectStorage(ctx context.Context, config storageInventoryConfig) (storageInventory, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
result := storageInventory{Categories: []storageCategory{}, Worktrees: []storageWorktree{}}
|
||||
trees, discoveryErrors := discoverStorageWorktrees(ctx, config.WorkspaceRoots, config.ProtectedWorkspaces)
|
||||
result.Errors = discoveryErrors
|
||||
result.Partial = len(discoveryErrors) > 0
|
||||
walker := storageWalker{ctx: ctx, seen: map[string]bool{}, inodes: map[[2]uint64]bool{}}
|
||||
add := func(category storageCategory, roots ...string) storageCategory {
|
||||
for _, root := range roots {
|
||||
walker.measure(root, &category)
|
||||
}
|
||||
result.TotalBytes += category.Bytes
|
||||
result.Partial = result.Partial || category.Partial
|
||||
if category.Error != "" {
|
||||
result.Errors = append(result.Errors, category.Error)
|
||||
}
|
||||
result.Categories = append(result.Categories, category)
|
||||
return category
|
||||
}
|
||||
if config.DatabasePath != "" {
|
||||
add(storageCategory{ID: "database", Label: "Database and journal", Path: config.DatabasePath}, config.DatabasePath, config.DatabasePath+"-wal", config.DatabasePath+"-shm")
|
||||
}
|
||||
add(storageCategory{ID: "artifacts", Label: "Run artifacts and exports", Path: config.ArtifactsRoot}, config.ArtifactsRoot)
|
||||
add(storageCategory{ID: "attachments", Label: "Chat attachments", Path: config.AttachmentsRoot}, config.AttachmentsRoot)
|
||||
add(storageCategory{ID: "sessions", Label: "Harness session files", Path: config.SessionsRoot}, config.SessionsRoot)
|
||||
add(storageCategory{ID: "other_data", Label: "Other server data", Path: config.DataRoot}, config.DataRoot)
|
||||
category := storageCategory{ID: "worktrees", Label: "Linked worktrees"}
|
||||
// Nested linked trees are attributed before their parents.
|
||||
sort.Slice(trees, func(i, j int) bool { return len(trees[i].Path) > len(trees[j].Path) })
|
||||
for _, tree := range trees {
|
||||
measured := storageCategory{}
|
||||
walker.measure(tree.Path, &measured)
|
||||
tree.Bytes, tree.Files = measured.Bytes, measured.Files
|
||||
if measured.ModifiedAt.After(tree.ModifiedAt) {
|
||||
tree.ModifiedAt = measured.ModifiedAt
|
||||
}
|
||||
if measured.Partial {
|
||||
tree.Eligible = false
|
||||
tree.Reason = "Storage measurement incomplete"
|
||||
category.Partial = true
|
||||
category.Error = measured.Error
|
||||
}
|
||||
category.Bytes += measured.Bytes
|
||||
category.Files += measured.Files
|
||||
result.Worktrees = append(result.Worktrees, tree)
|
||||
}
|
||||
result.TotalBytes += category.Bytes
|
||||
result.Categories = append(result.Categories, category)
|
||||
result.Partial = result.Partial || category.Partial
|
||||
if category.Error != "" {
|
||||
result.Errors = append(result.Errors, category.Error)
|
||||
}
|
||||
roots := append([]string{}, config.WorkspaceRoots...)
|
||||
sort.Strings(roots)
|
||||
add(storageCategory{ID: "workspaces", Label: "Other project working files"}, roots...)
|
||||
sort.Slice(result.Worktrees, func(i, j int) bool { return result.Worktrees[i].Path < result.Worktrees[j].Path })
|
||||
if ctx.Err() != nil {
|
||||
result.Partial = true
|
||||
result.Errors = append(result.Errors, ctx.Err().Error())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user