v0.21.0 73bf644

cleanup_sessions.go RawHistoryFolder

1package usecase
2
3import (
4	"context"
5	"time"
6
7	"github.com/bnema/dumber/internal/domain/repository"
8	"github.com/bnema/dumber/internal/logging"
9)
10
11// CleanupSessionsUseCase handles automatic cleanup of old exited sessions.
12type CleanupSessionsUseCase struct {
13	sessionRepo repository.SessionRepository
14}
15
16// NewCleanupSessionsUseCase creates a new CleanupSessionsUseCase.
17func NewCleanupSessionsUseCase(sessionRepo repository.SessionRepository) *CleanupSessionsUseCase {
18	return &CleanupSessionsUseCase{
19		sessionRepo: sessionRepo,
20	}
21}
22
23// CleanupSessionsInput contains the cleanup configuration.
24type CleanupSessionsInput struct {
25	// MaxExitedSessions is the maximum number of exited sessions to keep.
26	// Sessions beyond this count will be deleted (oldest first).
27	MaxExitedSessions int
28
29	// MaxExitedSessionAgeDays is the maximum age in days for exited sessions.
30	// Sessions older than this will be deleted.
31	// A value of 0 disables age-based cleanup.
32	MaxExitedSessionAgeDays int
33}
34
35// CleanupSessionsOutput contains the cleanup results.
36type CleanupSessionsOutput struct {
37	// DeletedByAge is the number of sessions deleted due to age.
38	DeletedByAge int64
39
40	// DeletedByCount is the number of sessions deleted due to count limit.
41	DeletedByCount int64
42
43	// TotalDeleted is the total number of sessions deleted.
44	TotalDeleted int64
45}
46
47// Execute cleans up old exited sessions based on the provided configuration.
48// It first deletes sessions older than MaxExitedSessionAgeDays,
49// then enforces the MaxExitedSessions count limit.
50func (uc *CleanupSessionsUseCase) Execute(ctx context.Context, input CleanupSessionsInput) (CleanupSessionsOutput, error) {
51	log := logging.FromContext(ctx)
52	output := CleanupSessionsOutput{}
53
54	// 1. Delete by age first (if enabled)
55	if input.MaxExitedSessionAgeDays > 0 {
56		cutoff := time.Now().AddDate(0, 0, -input.MaxExitedSessionAgeDays)
57		deleted, err := uc.sessionRepo.DeleteExitedBefore(ctx, cutoff)
58		if err != nil {
59			log.Warn().Err(err).Msg("failed to delete sessions by age")
60		} else {
61			output.DeletedByAge = deleted
62			if deleted > 0 {
63				log.Info().
64					Int64("deleted", deleted).
65					Int("max_age_days", input.MaxExitedSessionAgeDays).
66					Msg("cleaned up old sessions by age")
67			}
68		}
69	}
70
71	// 2. Enforce count limit on remaining sessions
72	if input.MaxExitedSessions >= 0 {
73		deleted, err := uc.sessionRepo.DeleteOldestExited(ctx, input.MaxExitedSessions)
74		if err != nil {
75			log.Warn().Err(err).Msg("failed to delete sessions by count")
76		} else {
77			output.DeletedByCount = deleted
78			if deleted > 0 {
79				log.Info().
80					Int64("deleted", deleted).
81					Int("max_count", input.MaxExitedSessions).
82					Msg("cleaned up old sessions by count")
83			}
84		}
85	}
86
87	output.TotalDeleted = output.DeletedByAge + output.DeletedByCount
88
89	if output.TotalDeleted > 0 {
90		log.Info().
91			Int64("total_deleted", output.TotalDeleted).
92			Int64("by_age", output.DeletedByAge).
93			Int64("by_count", output.DeletedByCount).
94			Msg("session cleanup completed")
95	}
96
97	return output, nil
98}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close