File Watcher Core (fsnotify) #66

Open
opened 2026-02-23 10:03:19 +00:00 by ottomata · 0 comments
Owner

Tasks

  • Add dependency: go get github.com/fsnotify/fsnotify
  • Create internal/watcher/watcher.go:
type Watcher struct {
    fsw     *fsnotify.Watcher
    handler func(event FileEvent)
    dirs    map[string]struct{}
    mu      sync.Mutex
}

type FileEvent struct {
    Filename string
    FullPath string
    DirPath  string
}
  • Methods:
    • NewWatcher(handler func(FileEvent)) (*Watcher, error)
    • Watch(dir string) error — adds dir to fsnotify, records in dirs map
    • Unwatch(dir string) error — removes dir from fsnotify and dirs map
    • Start() — starts event loop goroutine
    • Stop() — closes fsnotify watcher, exits event loop
  • Event loop: listen for fsnotify.Create events only (ignore modify/delete/rename)
  • Debounce per file path: wait 200ms after last event for the same path before firing handler (handles partial writes)
  • On startup in main.go: load all watched_dirs from DB and call watcher.Watch(dir.Path)

Acceptance Criteria

  • Creating a file in a watched directory fires the handler within 300ms
  • Rapid sequential writes to the same file trigger the handler only once (debounce)
  • Stop() cleanly exits the event loop
  • Integration test: watch a temp dir, create a file, verify handler is called
### Tasks - [ ] Add dependency: `go get github.com/fsnotify/fsnotify` - [ ] Create `internal/watcher/watcher.go`: ```go type Watcher struct { fsw *fsnotify.Watcher handler func(event FileEvent) dirs map[string]struct{} mu sync.Mutex } type FileEvent struct { Filename string FullPath string DirPath string } ``` - [ ] Methods: - `NewWatcher(handler func(FileEvent)) (*Watcher, error)` - `Watch(dir string) error` — adds dir to fsnotify, records in `dirs` map - `Unwatch(dir string) error` — removes dir from fsnotify and `dirs` map - `Start()` — starts event loop goroutine - `Stop()` — closes fsnotify watcher, exits event loop - [ ] Event loop: listen for `fsnotify.Create` events only (ignore modify/delete/rename) - [ ] Debounce per file path: wait 200ms after last event for the same path before firing handler (handles partial writes) - [ ] On startup in `main.go`: load all `watched_dirs` from DB and call `watcher.Watch(dir.Path)` ### Acceptance Criteria - [ ] Creating a file in a watched directory fires the handler within 300ms - [ ] Rapid sequential writes to the same file trigger the handler only once (debounce) - [ ] `Stop()` cleanly exits the event loop - [ ] Integration test: watch a temp dir, create a file, verify handler is called
ottomata added this to the Phase 7 project 2026-02-23 10:09:05 +00:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ottomata/acsm#66
No description provided.