fix(diary): retry a daily file that failed to open

- a failed open no longer disables the writer for good: it skips records
  and tries again one minute later by its clock, or at the first record of
  a new date, whichever comes first
- the warning (still 'daily log file disabled') appears once per date
- README and the logging page describe the retry (WR-04)
This commit is contained in:
Jakub Zych
2026-10-22 02:25:18 +02:00
parent 9b62285654
commit c586c6f8a9
4 changed files with 114 additions and 21 deletions

View File

@@ -9,6 +9,7 @@ import (
"strings"
"syscall"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/diary"
)
@@ -268,3 +269,79 @@ func TestPathConfinement(t *testing.T) {
t.Errorf("relative path did not resolve against the root: %d lines", len(lines))
}
}
// TestDailyRetriesAfterFailure (WR-04): a daily file that failed to open is
// retried one minute later by the writer's clock, or on the first record of
// a new date, whichever comes first; the warning appears once per date.
func TestDailyRetriesAfterFailure(t *testing.T) {
start := func(t *testing.T, at time.Time) (root string, clk *clock, buf *lockedBuffer) {
t.Helper()
root = t.TempDir()
blocker := filepath.Join(root, "blocker")
if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
clk = newClock(at)
buf = &lockedBuffer{}
setup(t, diary.Options{Root: root, Stderr: buf, Now: clk.Now},
append(slices.Clone(dailyOnly), "SUMMER_LOG__CHANNELS__DAILY__PATH=blocker/logs/summer.log")...)
return root, clk, buf
}
warnings := func(buf *lockedBuffer) int { return strings.Count(buf.String(), "daily log file disabled") }
blog := diary.For("blog")
t.Run("one minute later", func(t *testing.T) {
root, clk, buf := start(t, day(21, 10, 0, 0))
blog.Info("first")
if n := warnings(buf); n != 1 {
t.Fatalf("%d warnings after the failed open, want 1:\n%s", n, buf.String())
}
if err := os.Remove(filepath.Join(root, "blocker")); err != nil {
t.Fatal(err)
}
file := filepath.Join(root, "blocker", "logs", "summer-2026-10-21.log")
clk.Set(day(21, 10, 0, 30))
blog.Info("within the minute")
if _, err := os.Stat(file); !os.IsNotExist(err) {
t.Fatalf("retried before the minute passed (stat err %v)", err)
}
if n := warnings(buf); n != 1 {
t.Fatalf("%d warnings within the minute, want 1:\n%s", n, buf.String())
}
clk.Set(day(21, 10, 1, 1))
blog.Info("after the minute")
lines := readDaily(t, file)
if len(lines) != 1 || !strings.Contains(lines[0], "after the minute") {
t.Fatalf("daily file after the retry = %q, want the record after the minute", lines)
}
})
t.Run("next date", func(t *testing.T) {
root, clk, buf := start(t, day(21, 23, 59, 59))
blog.Info("before midnight")
if err := os.Remove(filepath.Join(root, "blocker")); err != nil {
t.Fatal(err)
}
clk.Set(day(22, 0, 0, 1))
blog.Info("after midnight")
lines := readDaily(t, filepath.Join(root, "blocker", "logs", "summer-2026-10-22.log"))
if len(lines) != 1 || !strings.Contains(lines[0], "after midnight") {
t.Fatalf("daily file of the new date = %q", lines)
}
if n := warnings(buf); n != 1 {
t.Errorf("%d warnings, want 1:\n%s", n, buf.String())
}
})
t.Run("next date fails too", func(t *testing.T) {
_, clk, buf := start(t, day(21, 23, 59, 59))
blog.Info("before midnight")
clk.Set(day(22, 0, 0, 1))
blog.Info("after midnight")
clk.Set(day(22, 0, 2, 0))
blog.Info("later")
if n := warnings(buf); n != 2 {
t.Errorf("%d warnings over two failed dates, want 2:\n%s", n, buf.String())
}
})
}