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}