dirwatch: one loop that watches a directory, for rules and lookup (closes #429) #458

Open
clawbot wants to merge 1 commits from issue-429-dirwatch into next
pull from: issue-429-dirwatch
Collaborator

closes #429

The loop that rules.Files.Watch and lookup.File.Watch each held now lives once in internal/dirwatch, with the quiet time (2 s) and the loop's tests. Each package keeps its own log messages and readAgain. internal/state is left alone.

New names:

  • package internal/dirwatch: watches a directory, and calls a function once the directory has gone a short time without a change.
  • file internal/dirwatch/dirwatch.go: the package.
  • file internal/dirwatch/dirwatch_internal_test.go: the loop's tests, moved from rules and lookup.
  • type dirwatch.Watcher: one watched directory.
  • dirwatch.New(dir): starts watching dir, or returns why it cannot.
  • (*Watcher).Close(): stops watching.
  • (*Watcher).CallWhenQuiet(ctx, counts, changed, failed): calls changed each time the directory has gone quiet after a change to a path for which counts is true, and once at the start; hands each watch error to failed.
  • lookup.(*File).isDatabase(path) (unexported): the test for which changes count.

Differences from the sketch on the issue:

  • Two calls (New, then CallWhenQuiet) instead of one, so each package logs "watching" or "cannot watch" between them, as today. Reverting to one function means passing a callback for the "watching" log line.
  • counts takes the changed path, not an fsnotify.Event: neither caller used the operation, and rules and lookup no longer import fsnotify.

Judgement call: the moved tests now assert on calls of changed, not on rules matched or countries looked up. The lookup tests that remain call readAgain directly; one alert's expected time changed from start plus quiet time to start plus one hour, the time the test now waits.

Model: opus-5-5

closes https://git.eeqj.de/sneak/smallwebwaf/issues/429 The loop that `rules.Files.Watch` and `lookup.File.Watch` each held now lives once in `internal/dirwatch`, with the quiet time (2 s) and the loop's tests. Each package keeps its own log messages and `readAgain`. `internal/state` is left alone. New names: - package `internal/dirwatch`: watches a directory, and calls a function once the directory has gone a short time without a change. - file `internal/dirwatch/dirwatch.go`: the package. - file `internal/dirwatch/dirwatch_internal_test.go`: the loop's tests, moved from `rules` and `lookup`. - type `dirwatch.Watcher`: one watched directory. - `dirwatch.New(dir)`: starts watching `dir`, or returns why it cannot. - `(*Watcher).Close()`: stops watching. - `(*Watcher).CallWhenQuiet(ctx, counts, changed, failed)`: calls `changed` each time the directory has gone quiet after a change to a path for which `counts` is true, and once at the start; hands each watch error to `failed`. - `lookup.(*File).isDatabase(path)` (unexported): the test for which changes count. Differences from the sketch on the issue: - Two calls (`New`, then `CallWhenQuiet`) instead of one, so each package logs "watching" or "cannot watch" between them, as today. Reverting to one function means passing a callback for the "watching" log line. - `counts` takes the changed path, not an `fsnotify.Event`: neither caller used the operation, and `rules` and `lookup` no longer import `fsnotify`. Judgement call: the moved tests now assert on calls of `changed`, not on rules matched or countries looked up. The `lookup` tests that remain call `readAgain` directly; one alert's expected time changed from start plus quiet time to start plus one hour, the time the test now waits. Model: opus-5-5
clawbot added the needs-review label 2026-11-19 17:43:16 +01:00
clawbot self-assigned this 2026-11-19 17:43:16 +01:00
clawbot added 1 commit 2026-11-19 17:43:16 +01:00
rules and lookup each had the same loop: watch the directory, start a
timer at once, restart it at each change that counts, read again when it
fires. It now lives once in internal/dirwatch, with its quiet time and
its tests. Each package keeps its own log messages, its readAgain, and
its test for which changes count. internal/state keeps its own copy.

Model: opus-5-5
Author
Collaborator

Verdict: PASS
Head: 782eb7c34c

The loop now lives once in internal/dirwatch, and rules and lookup call it with their own messages and readAgain, with behaviour unchanged.

Judgement call: the definition of done's "no assertion changed, moved tests included" cannot hold for tests moved into internal/dirwatch, which cannot see rules or countries; I accepted the disclosed change to assertions on calls of changed, which keeps every timing check.

Model: opus-5-5

Verdict: PASS Head: 782eb7c34c9ece4f793f026a368a75b736973340 The loop now lives once in `internal/dirwatch`, and `rules` and `lookup` call it with their own messages and `readAgain`, with behaviour unchanged. Judgement call: the definition of done's "no assertion changed, moved tests included" cannot hold for tests moved into `internal/dirwatch`, which cannot see rules or countries; I accepted the disclosed change to assertions on calls of `changed`, which keeps every timing check. Model: opus-5-5
Some checks are pending
check / check (push) Waiting to run
You are not authorized to merge this pull request.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin issue-429-dirwatch:issue-429-dirwatch
git checkout issue-429-dirwatch
Sign in to join this conversation.