Skip to content

Commit 228618b

Browse files
committed
Cleanup processed/unreadable Santa spool files during processing
1 parent d6d9add commit 228618b

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

cmd/santamon/main.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,28 @@ func runCommand() {
417417
logutil.Success("Shutdown complete")
418418
return
419419
}
420-
// Skip if we've already processed this file (journaled)
420+
spoolArchivePath := ""
421+
if cfg.Santa.ArchiveDir != "" {
422+
spoolArchivePath = filepath.Join(cfg.Santa.ArchiveDir, filepath.Base(filePath))
423+
}
424+
spoolContext := map[string]any{}
425+
if spoolArchivePath != "" {
426+
spoolContext["spool_archive_path"] = spoolArchivePath
427+
}
428+
429+
// Skip if we've already processed this file (journaled) and clean it up
421430
if je, _ := db.GetJournalEntry(filePath); je != nil {
422431
if info, err := os.Stat(filePath); err == nil {
423-
// If file hasn't changed since last processed, skip
432+
// If file hasn't changed since last processed, archive/delete it
424433
if !info.ModTime().After(je.ProcessedTS) {
425-
if os.Getenv("SANTAMON_DEBUG") == "1" {
426-
log.Printf("Skipping already-processed spool file: %s", filePath)
434+
if err := watcher.ArchiveFile(filePath); err != nil {
435+
log.Printf("Warning: Failed to archive already-processed spool file %s: %v", filePath, err)
436+
} else if os.Getenv("SANTAMON_DEBUG") == "1" {
437+
if spoolArchivePath != "" {
438+
log.Printf("Archived already-processed spool file %s to %s", filePath, spoolArchivePath)
439+
} else {
440+
log.Printf("Deleted already-processed spool file: %s", filePath)
441+
}
427442
}
428443
continue
429444
}
@@ -433,21 +448,15 @@ func runCommand() {
433448
log.Printf("Processing file: %s", filePath)
434449
}
435450

436-
spoolArchivePath := ""
437-
if cfg.Santa.ArchiveDir != "" {
438-
spoolArchivePath = filepath.Join(cfg.Santa.ArchiveDir, filepath.Base(filePath))
439-
}
440-
spoolContext := map[string]any{}
441-
if spoolArchivePath != "" {
442-
spoolContext["spool_archive_path"] = spoolArchivePath
443-
}
444-
445451
fileHasSignals := false
446452

447453
// Decode events from file
448454
messages, err := decoder.DecodeEvents(filePath)
449455
if err != nil {
450456
log.Printf("Failed to decode file: %v", err)
457+
if err := watcher.ArchiveFile(filePath); err != nil {
458+
log.Printf("Warning: Failed to archive unreadable spool file %s: %v", filePath, err)
459+
}
451460
// Update journal even on error to avoid reprocessing
452461
if err := db.UpdateJournal(filePath, 0); err != nil {
453462
log.Printf("Warning: Failed to update journal: %v", err)

0 commit comments

Comments
 (0)