Add run size_bytes and delete_runs to free disk space
Every run summary carries size_bytes (artifact tree, server-created Pi session directories and an estimate of the run's database rows), cached in pipeline_runs.size_bytes, invalidated by triggers, measured when a run finishes and backfilled in the background. delete_runs removes terminal runs with every scoped row and server-owned file, refuses active ones with a reason, and pushes runs_removed to subscribers. pipelines runs rm is the CLI form; retention apply now shares the complete row deletion.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// removeRuns implements `pipelines runs rm <id...>`: one delete_runs request
|
||||
// for every named run. The server refuses active and unknown runs individually;
|
||||
// any refusal makes the command exit non-zero after reporting the result.
|
||||
func (a App) removeRuns(o *options, args []string, call callFunc) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("runs rm requires at least one run ID; use pipelines help runs")
|
||||
}
|
||||
ids := make([]int64, 0, len(args))
|
||||
for _, value := range args {
|
||||
id, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return fmt.Errorf("run ID must be positive: %q", value)
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
if err := a.confirmAction(fmt.Sprintf("Permanently delete %d run(s) with their artifacts and history?", len(ids)), o.yes); err != nil {
|
||||
return err
|
||||
}
|
||||
r, err := call(protocol.Message{Type: protocol.DeleteRuns, RunIDs: ids})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r.RunsDeleted == nil {
|
||||
return fmt.Errorf("server did not return a deletion result; update pipelines-server")
|
||||
}
|
||||
result := r.RunsDeleted
|
||||
if o.json {
|
||||
if err := a.output(result); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
for _, id := range result.Deleted {
|
||||
fmt.Fprintf(a.Out, "deleted\t%d\n", id)
|
||||
}
|
||||
for _, refusal := range result.Refused {
|
||||
fmt.Fprintf(a.Out, "refused\t%d\t%s\n", refusal.RunID, refusal.Reason)
|
||||
}
|
||||
fmt.Fprintf(a.Out, "Freed %s\n", formatSize(result.FreedBytes))
|
||||
}
|
||||
if len(result.Refused) > 0 {
|
||||
return &ExitError{Code: 1}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// formatSize renders a byte count with a binary unit for human output.
|
||||
func formatSize(bytes int64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
value, suffix := float64(bytes), "B"
|
||||
for _, next := range []string{"KiB", "MiB", "GiB", "TiB"} {
|
||||
if value < unit {
|
||||
break
|
||||
}
|
||||
value /= unit
|
||||
suffix = next
|
||||
}
|
||||
return fmt.Sprintf("%.1f %s", value, suffix)
|
||||
}
|
||||
Reference in New Issue
Block a user