fix(watcher): don't re-ingest a note that cannot be retired; pin archive-failure keeps original
When both Remove and the marker write fail (read-only raw/), remember the path in memory so later polls skip it instead of writing another archive copy every tick. Adds a test that a failed archive copy leaves the original in raw/, mutation-checked against removing the early return. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
@@ -21,11 +22,19 @@ type Config struct {
|
||||
BrainDir string
|
||||
Interval time.Duration
|
||||
Pipeline pipeline.Config
|
||||
|
||||
// stuck remembers notes that were ingested but could neither be removed
|
||||
// from raw/ nor marked (e.g. a read-only raw/), so later polls skip them
|
||||
// instead of ingesting them again. In memory only: a restart retries once.
|
||||
stuck *sync.Map
|
||||
}
|
||||
|
||||
// Start launches the watcher in a background goroutine.
|
||||
// It returns immediately. The watcher stops when ctx is cancelled.
|
||||
func Start(ctx context.Context, cfg Config) {
|
||||
if cfg.stuck == nil {
|
||||
cfg.stuck = &sync.Map{}
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(cfg.Interval)
|
||||
defer ticker.Stop()
|
||||
@@ -108,6 +117,12 @@ func processDir(ctx context.Context, cfg Config, date string) []error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if cfg.stuck != nil {
|
||||
if _, skip := cfg.stuck.Load(path); skip {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Skip files that have already been processed or permanently failed.
|
||||
if _, err := os.Stat(path + ".processed"); err == nil {
|
||||
return nil
|
||||
@@ -157,7 +172,7 @@ func processFile(ctx context.Context, cfg Config, root *os.Root, rawDir, path, d
|
||||
if cpErr := copyFromRoot(root, rel, dest); cpErr != nil {
|
||||
return fmt.Errorf("copy to failed: %w", cpErr)
|
||||
}
|
||||
retire(root, rel, path+".failed", runErr.Error())
|
||||
retire(cfg, root, rel, path, ".failed", runErr.Error())
|
||||
|
||||
slog.Warn("watcher: file failed", "file", filename, "error", runErr)
|
||||
|
||||
@@ -183,7 +198,7 @@ func processFile(ctx context.Context, cfg Config, root *os.Root, rawDir, path, d
|
||||
if err := copyFromRoot(root, rel, dest); err != nil {
|
||||
return fmt.Errorf("copy to processed: %w", err)
|
||||
}
|
||||
retire(root, rel, path+".processed", date)
|
||||
retire(cfg, root, rel, path, ".processed", date)
|
||||
|
||||
slog.Info("watcher: file processed", "file", filename, "source", source)
|
||||
return nil
|
||||
@@ -191,14 +206,17 @@ func processFile(ctx context.Context, cfg Config, root *os.Root, rawDir, path, d
|
||||
|
||||
// retire removes an archived original from raw/ (inbox zero). If the removal
|
||||
// fails it falls back to a marker file so the watcher still skips the file.
|
||||
func retire(root *os.Root, rel, marker, markerBody string) {
|
||||
func retire(cfg Config, root *os.Root, rel, path, markerSuffix, markerBody string) {
|
||||
rmErr := root.Remove(rel)
|
||||
if rmErr == nil {
|
||||
return
|
||||
}
|
||||
slog.Error("watcher: failed to remove archived original; writing marker", "file", rel, "error", rmErr)
|
||||
if err := os.WriteFile(marker, []byte(markerBody), 0o644); err != nil {
|
||||
slog.Error("watcher: failed to write marker", "marker", marker, "error", err)
|
||||
if err := os.WriteFile(path+markerSuffix, []byte(markerBody), 0o644); err != nil {
|
||||
slog.Error("watcher: failed to write marker; skipping this note until restart", "file", rel, "error", err)
|
||||
if cfg.stuck != nil {
|
||||
cfg.stuck.Store(path, struct{}{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user