more cart
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -201,6 +202,9 @@ func (s *DiskStorage[V]) SaveLoop(duration time.Duration) {
|
||||
}
|
||||
|
||||
func (s *DiskStorage[V]) save() {
|
||||
if s.queue == nil {
|
||||
return
|
||||
}
|
||||
carts := 0
|
||||
lines := 0
|
||||
s.queue.Range(func(key, value any) bool {
|
||||
@@ -295,3 +299,65 @@ func (s *DiskStorage[V]) openWriterCount() int {
|
||||
defer s.writersMu.Unlock()
|
||||
return len(s.writers)
|
||||
}
|
||||
|
||||
func (s *DiskStorage[V]) PurgeOlderThan(ctx context.Context, retention time.Duration, isGrainActive func(id uint64) bool) (int, error) {
|
||||
if err := s.ensureDir(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
files, err := os.ReadDir(s.path)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("read storage dir: %w", err)
|
||||
}
|
||||
|
||||
cutoff := time.Now().Add(-retention)
|
||||
purgedCount := 0
|
||||
|
||||
for _, entry := range files {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := entry.Name()
|
||||
if !strings.HasSuffix(name, ".events.log") {
|
||||
continue
|
||||
}
|
||||
|
||||
var id uint64
|
||||
_, err := fmt.Sscanf(name, "%d.events.log", &id)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Printf("failed to get file info for %s: %v", name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if info.ModTime().Before(cutoff) {
|
||||
// Check if grain is active in memory
|
||||
if isGrainActive != nil && isGrainActive(id) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if we have an active writer open
|
||||
s.writersMu.Lock()
|
||||
_, hasWriter := s.writers[id]
|
||||
s.writersMu.Unlock()
|
||||
if hasWriter {
|
||||
continue
|
||||
}
|
||||
|
||||
// Delete the file
|
||||
fullPath := filepath.Join(s.path, name)
|
||||
if err := os.Remove(fullPath); err != nil {
|
||||
log.Printf("failed to delete expired cart log %s: %v", fullPath, err)
|
||||
} else {
|
||||
purgedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return purgedCount, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user