-
Notifications
You must be signed in to change notification settings - Fork 193
feat(sessions): add zero sessions prune, which never removes a session another process has open #1089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vasanthdev2004
wants to merge
14
commits into
main
Choose a base branch
from
feat/971-sessions-prune
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(sessions): add zero sessions prune, which never removes a session another process has open #1089
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8fc019e
feat(sessions): prune old sessions, never one another process has open
Vasanthdev2004 ac1efe3
feat(cli): add zero sessions prune, with a retention setting read fro…
Vasanthdev2004 9aabaf6
test(sessions): a dry run reports a session another process has open …
Vasanthdev2004 316c898
test(sessions): pin the dry run for undated sessions and the lease a …
Vasanthdev2004 4f1dbd9
test(sessions): report every failure when a session is written while …
Vasanthdev2004 5fe047a
docs(readme): list sessions prune in the Chinese README as well
Vasanthdev2004 c0f4742
fix(sessions): a fork or child holds its parent, and is refused while…
Vasanthdev2004 4cfbb88
docs(sessions): say that nothing calls Release yet, and what it is for
Vasanthdev2004 2bacb7e
fix(sessions): let go of the lease before removing a pruned session's…
Vasanthdev2004 ff28d8f
test(cli): an empty --older-than is a usage error, not the retention …
Vasanthdev2004 7fa2017
fix(sessions): refuse to continue a session prune holds, rather than …
Vasanthdev2004 dc3ec65
test(sessions): a lease that cannot be taken keeps the session, and d…
Vasanthdev2004 94c9087
fix(sessions): keep a parent forked after the plan, and refuse sessio…
Vasanthdev2004 ff04d2f
fix(sessions): make a dry run decide at removal time like a real prune
Vasanthdev2004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/Gitlawb/zero/internal/config" | ||
| "github.com/Gitlawb/zero/internal/redaction" | ||
| "github.com/Gitlawb/zero/internal/sessions" | ||
| ) | ||
|
|
||
| // maxPruneDays keeps a day count from overflowing time.Duration. | ||
| const maxPruneDays = 36500 | ||
|
|
||
| // runSessionsPrune removes sessions last updated before a cutoff, or with | ||
| // --dry-run lists what would go and why. It runs only when asked (#971). | ||
| func runSessionsPrune(store *sessions.Store, options sessionCommandOptions, stdout io.Writer, stderr io.Writer, deps appDeps) int { | ||
| olderThan, err := pruneCutoff(options, deps) | ||
| if err != nil { | ||
| return writeExecUsageError(stderr, err.Error()) | ||
| } | ||
| report, err := store.Prune(sessions.PruneOptions{OlderThan: olderThan, DryRun: options.dryRun}) | ||
| if err != nil { | ||
| return writeSessionCommandError(stderr, err) | ||
| } | ||
| if options.json { | ||
| if err := writePrettyJSON(stdout, redaction.RedactValue(report, redaction.Options{})); err != nil { | ||
| return exitCrash | ||
| } | ||
| } else if _, err := fmt.Fprint(stdout, formatPruneReport(report)); err != nil { | ||
| return exitCrash | ||
| } | ||
| if len(report.Failed) > 0 { | ||
| return exitCrash | ||
| } | ||
| return exitSuccess | ||
| } | ||
|
|
||
| // pruneCutoff is --older-than, or else sessions.retentionDays from the user's | ||
| // own config, and never below sessions.MinimumPruneAge. Project config has no | ||
| // say in it: see config.SessionsConfig. | ||
| func pruneCutoff(options sessionCommandOptions, deps appDeps) (time.Duration, error) { | ||
| var age time.Duration | ||
| if options.olderThan != "" { | ||
| parsed, err := parsePruneAge(options.olderThan) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| age = parsed | ||
| } else { | ||
| days := 0 | ||
| if deps.userConfigPath != nil { | ||
| path, err := deps.userConfigPath() | ||
| if err != nil { | ||
| return 0, fmt.Errorf("sessions prune could not find your config: %w", err) | ||
| } | ||
| settings, err := config.ReadSessionsConfig(path) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("sessions prune could not read sessions.retentionDays: %w", err) | ||
| } | ||
| days = settings.RetentionDays | ||
| } | ||
| if days == 0 { | ||
| return 0, execUsageError{"sessions prune needs a cutoff: pass --older-than (for example --older-than 30d) or set sessions.retentionDays in your user config"} | ||
| } | ||
| if days > maxPruneDays { | ||
| return 0, execUsageError{fmt.Sprintf("sessions.retentionDays %d is more than %d", days, maxPruneDays)} | ||
| } | ||
| age = time.Duration(days) * 24 * time.Hour | ||
| } | ||
| if age < sessions.MinimumPruneAge { | ||
| return 0, execUsageError{fmt.Sprintf("sessions prune does not remove anything updated in the last %d hours; use --older-than 1d or more", int(sessions.MinimumPruneAge.Hours()))} | ||
| } | ||
| return age, nil | ||
| } | ||
|
|
||
| // parsePruneAge accepts a whole number of days ("30d") or a Go duration | ||
| // ("720h"). | ||
| func parsePruneAge(value string) (time.Duration, error) { | ||
| invalid := execUsageError{fmt.Sprintf("invalid --older-than %q: expected days like 30d or a duration like 720h", value)} | ||
| trimmed := strings.TrimSpace(value) | ||
| if days, ok := strings.CutSuffix(trimmed, "d"); ok { | ||
| count, err := strconv.Atoi(days) | ||
| if err != nil || count <= 0 || count > maxPruneDays { | ||
| return 0, invalid | ||
| } | ||
| return time.Duration(count) * 24 * time.Hour, nil | ||
| } | ||
| age, err := time.ParseDuration(trimmed) | ||
| if err != nil || age <= 0 { | ||
| return 0, invalid | ||
| } | ||
| return age, nil | ||
| } | ||
|
|
||
| func formatPruneReport(report sessions.PruneReport) string { | ||
| var out strings.Builder | ||
| verb := "Removed" | ||
| if report.DryRun { | ||
| verb = "Would remove" | ||
| } | ||
| if len(report.Removed) == 0 { | ||
| fmt.Fprintf(&out, "No sessions to remove: nothing last updated before %s can go.\n", report.Cutoff) | ||
| } else { | ||
| var total int64 | ||
| for _, entry := range report.Removed { | ||
| total += entry.Bytes | ||
| } | ||
| fmt.Fprintf(&out, "%s %d %s last updated before %s (%s):\n", verb, len(report.Removed), plural(len(report.Removed), "session", "sessions"), report.Cutoff, formatPruneBytes(total)) | ||
| for _, entry := range report.Removed { | ||
| fmt.Fprintf(&out, " %s\n", formatPruneEntry(entry, redact(entry.Title))) | ||
| } | ||
| } | ||
| if len(report.Kept) > 0 { | ||
| fmt.Fprintf(&out, "Kept %d old enough to remove:\n", len(report.Kept)) | ||
| for _, entry := range report.Kept { | ||
| fmt.Fprintf(&out, " %s\n", formatPruneEntry(entry, entry.Reason)) | ||
| } | ||
| } | ||
| if len(report.Failed) > 0 { | ||
| failed := "Could not remove" | ||
| if report.DryRun { | ||
| failed = "Could not check" | ||
| } | ||
| fmt.Fprintf(&out, "%s %d:\n", failed, len(report.Failed)) | ||
| for _, entry := range report.Failed { | ||
| fmt.Fprintf(&out, " %s\n", formatPruneEntry(entry, redact(entry.Reason))) | ||
| } | ||
| } | ||
| if report.DryRun { | ||
| out.WriteString("Dry run: nothing was removed.\n") | ||
| } | ||
| return out.String() | ||
| } | ||
|
|
||
| func formatPruneEntry(entry sessions.PruneEntry, note string) string { | ||
| updated := entry.UpdatedAt | ||
| if len(updated) >= len("2006-01-02") { | ||
| updated = updated[:len("2006-01-02")] | ||
| } | ||
| line := redact(entry.SessionID) + " " + updated | ||
| if note = strings.TrimSpace(note); note != "" { | ||
| line += " " + note | ||
| } | ||
| return line | ||
| } | ||
|
|
||
| func formatPruneBytes(bytes int64) string { | ||
| const unit = 1024 | ||
| if bytes < unit { | ||
| return fmt.Sprintf("%d B", bytes) | ||
| } | ||
| value, suffix := float64(bytes)/unit, "KB" | ||
| for _, next := range []string{"MB", "GB", "TB"} { | ||
| if value < unit { | ||
| break | ||
| } | ||
| value, suffix = value/unit, next | ||
| } | ||
| return fmt.Sprintf("%.1f %s", value, suffix) | ||
| } | ||
|
|
||
| func plural(count int, one, many string) string { | ||
| if count == 1 { | ||
| return one | ||
| } | ||
| return many | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.