|
| 1 | +package gc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "path/filepath" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/substantialcattle5/sietch/internal/config" |
| 10 | + "github.com/substantialcattle5/sietch/internal/deduplication" |
| 11 | + "github.com/substantialcattle5/sietch/internal/fs" |
| 12 | +) |
| 13 | + |
| 14 | +// Manager manages automatic garbage collection for Sietch vaults |
| 15 | +type Manager struct { |
| 16 | + vaultRoot string |
| 17 | + monitor *Monitor |
| 18 | + config config.VaultConfig |
| 19 | + started bool |
| 20 | +} |
| 21 | + |
| 22 | +// NewManager creates a new GC manager for a vault |
| 23 | +func NewManager(vaultRoot string) (*Manager, error) { |
| 24 | + // Check if vault is initialized |
| 25 | + if !fs.IsVaultInitialized(vaultRoot) { |
| 26 | + return nil, fmt.Errorf("vault not initialized") |
| 27 | + } |
| 28 | + |
| 29 | + // Load vault configuration |
| 30 | + vaultConfig, err := config.LoadVaultConfig(vaultRoot) |
| 31 | + if err != nil { |
| 32 | + return nil, fmt.Errorf("failed to load vault configuration: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + return &Manager{ |
| 36 | + vaultRoot: vaultRoot, |
| 37 | + config: *vaultConfig, |
| 38 | + started: false, |
| 39 | + }, nil |
| 40 | +} |
| 41 | + |
| 42 | +// Start begins automatic GC monitoring for the vault |
| 43 | +func (m *Manager) Start(ctx context.Context) error { |
| 44 | + if m.started { |
| 45 | + return fmt.Errorf("GC manager already started") |
| 46 | + } |
| 47 | + |
| 48 | + // Check if automatic GC is enabled |
| 49 | + if !m.config.Deduplication.AutoGC.Enabled { |
| 50 | + return fmt.Errorf("automatic GC is disabled in vault configuration") |
| 51 | + } |
| 52 | + |
| 53 | + // Parse check interval |
| 54 | + checkInterval, err := time.ParseDuration(m.config.Deduplication.AutoGC.CheckInterval) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("invalid check interval '%s': %w", m.config.Deduplication.AutoGC.CheckInterval, err) |
| 57 | + } |
| 58 | + |
| 59 | + // Create monitor configuration |
| 60 | + monitorConfig := MonitorConfig{ |
| 61 | + Enabled: m.config.Deduplication.AutoGC.Enabled, |
| 62 | + CheckInterval: checkInterval, |
| 63 | + AutoGCThreshold: m.getEffectiveGCThreshold(), |
| 64 | + EnableLogging: m.config.Deduplication.AutoGC.EnableLogging, |
| 65 | + LogFile: m.getEffectiveLogFile(), |
| 66 | + AlertThreshold: m.config.Deduplication.AutoGC.AlertThreshold, |
| 67 | + AlertWebhook: m.config.Deduplication.AutoGC.AlertWebhook, |
| 68 | + } |
| 69 | + |
| 70 | + // Create and start monitor |
| 71 | + monitor, err := NewMonitor(m.vaultRoot, monitorConfig, m.config.Deduplication) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("failed to create GC monitor: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + if err := monitor.Start(ctx); err != nil { |
| 77 | + return fmt.Errorf("failed to start GC monitor: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + m.monitor = monitor |
| 81 | + m.started = true |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// Stop halts automatic GC monitoring |
| 87 | +func (m *Manager) Stop() error { |
| 88 | + if !m.started { |
| 89 | + return fmt.Errorf("GC manager not started") |
| 90 | + } |
| 91 | + |
| 92 | + err := m.monitor.Stop() |
| 93 | + m.started = false |
| 94 | + return err |
| 95 | +} |
| 96 | + |
| 97 | +// IsRunning returns whether the GC manager is currently running |
| 98 | +func (m *Manager) IsRunning() bool { |
| 99 | + return m.started && m.monitor.IsRunning() |
| 100 | +} |
| 101 | + |
| 102 | +// GetStats returns current deduplication statistics |
| 103 | +func (m *Manager) GetStats() (deduplication.DeduplicationStats, error) { |
| 104 | + if !m.started { |
| 105 | + return deduplication.DeduplicationStats{}, fmt.Errorf("GC manager not started") |
| 106 | + } |
| 107 | + return m.monitor.GetStats(), nil |
| 108 | +} |
| 109 | + |
| 110 | +// getEffectiveGCThreshold returns the effective GC threshold to use |
| 111 | +func (m *Manager) getEffectiveGCThreshold() int { |
| 112 | + if m.config.Deduplication.AutoGC.AutoGCThreshold > 0 { |
| 113 | + return m.config.Deduplication.AutoGC.AutoGCThreshold |
| 114 | + } |
| 115 | + return m.config.Deduplication.GCThreshold |
| 116 | +} |
| 117 | + |
| 118 | +// getEffectiveLogFile returns the effective log file path |
| 119 | +func (m *Manager) getEffectiveLogFile() string { |
| 120 | + if m.config.Deduplication.AutoGC.LogFile != "" { |
| 121 | + // Convert relative path to absolute path within vault |
| 122 | + if !filepath.IsAbs(m.config.Deduplication.AutoGC.LogFile) { |
| 123 | + return filepath.Join(m.vaultRoot, m.config.Deduplication.AutoGC.LogFile) |
| 124 | + } |
| 125 | + return m.config.Deduplication.AutoGC.LogFile |
| 126 | + } |
| 127 | + return filepath.Join(m.vaultRoot, ".sietch", "logs", "gc.log") |
| 128 | +} |
| 129 | + |
| 130 | +// Global variable to hold the active GC manager |
| 131 | +var activeManager *Manager |
| 132 | + |
| 133 | +// StartGlobalGC starts automatic GC for the vault in the current directory |
| 134 | +func StartGlobalGC(ctx context.Context) error { |
| 135 | + vaultRoot, err := fs.FindVaultRoot() |
| 136 | + if err != nil { |
| 137 | + // Not in a vault, silently ignore |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + manager, err := NewManager(vaultRoot) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("failed to create GC manager: %w", err) |
| 144 | + } |
| 145 | + |
| 146 | + if err := manager.Start(ctx); err != nil { |
| 147 | + return fmt.Errorf("failed to start GC manager: %w", err) |
| 148 | + } |
| 149 | + |
| 150 | + activeManager = manager |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +// StopGlobalGC stops the global GC manager |
| 155 | +func StopGlobalGC() error { |
| 156 | + if activeManager == nil { |
| 157 | + return nil |
| 158 | + } |
| 159 | + |
| 160 | + err := activeManager.Stop() |
| 161 | + activeManager = nil |
| 162 | + return err |
| 163 | +} |
| 164 | + |
| 165 | +// GetGlobalGCManager returns the active GC manager |
| 166 | +func GetGlobalGCManager() *Manager { |
| 167 | + return activeManager |
| 168 | +} |
0 commit comments