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:
@@ -12,23 +12,34 @@ import (
|
||||
|
||||
const dateLayout = "2006-01-02"
|
||||
|
||||
// dailyRetry is how long a daily writer skips records after a file failed
|
||||
// to open before it tries again on the same date.
|
||||
const dailyRetry = time.Minute
|
||||
|
||||
// dailyWriter writes to one file per local date: path storage/logs/summer.log
|
||||
// gives storage/logs/summer-2026-10-21.log. Files open lazily on the first
|
||||
// write (no empty files), are created 0o640 in directories created 0o750,
|
||||
// and on every open the files of the same base older than the retention are
|
||||
// deleted. If a file cannot be opened the writer reports once through warn
|
||||
// and is disabled: later writes succeed without writing anything.
|
||||
// deleted. If a file cannot be opened the writer skips records (writes
|
||||
// still succeed) and tries again one minute later by its clock, or at the
|
||||
// first record of a new date, whichever comes first; it reports through
|
||||
// warn once per date that fails.
|
||||
type dailyWriter struct {
|
||||
mu sync.Mutex
|
||||
dir string
|
||||
base string
|
||||
ext string
|
||||
days int
|
||||
now func() time.Time
|
||||
warn func(path string, err error)
|
||||
file *os.File
|
||||
date string
|
||||
disabled bool
|
||||
mu sync.Mutex
|
||||
dir string
|
||||
base string
|
||||
ext string
|
||||
days int
|
||||
now func() time.Time
|
||||
warn func(path string, err error)
|
||||
file *os.File
|
||||
date string
|
||||
// failedDate is the date whose file failed to open last; no open is
|
||||
// tried for it before retryAt.
|
||||
failedDate string
|
||||
retryAt time.Time
|
||||
// warnedDate is the last date a failure was reported for.
|
||||
warnedDate string
|
||||
}
|
||||
|
||||
func newDailyWriter(path string, days int, now func() time.Time, warn func(string, error)) *dailyWriter {
|
||||
@@ -54,15 +65,19 @@ func (d *dailyWriter) fileName(date string) string {
|
||||
// Write appends p to the current day's file in one write call.
|
||||
func (d *dailyWriter) Write(p []byte) (int, error) {
|
||||
d.mu.Lock()
|
||||
if d.disabled {
|
||||
d.mu.Unlock()
|
||||
return len(p), nil
|
||||
}
|
||||
date := d.now().Local().Format(dateLayout)
|
||||
now := d.now()
|
||||
date := now.Local().Format(dateLayout)
|
||||
if d.file == nil || date != d.date {
|
||||
if date == d.failedDate && now.Before(d.retryAt) {
|
||||
d.mu.Unlock()
|
||||
return len(p), nil
|
||||
}
|
||||
if err := d.open(date); err != nil {
|
||||
d.disabled = true
|
||||
warn := d.warn
|
||||
d.failedDate, d.retryAt = date, now.Add(dailyRetry)
|
||||
var warn func(string, error)
|
||||
if d.warnedDate != date {
|
||||
d.warnedDate, warn = date, d.warn
|
||||
}
|
||||
name := d.fileName(date)
|
||||
d.mu.Unlock()
|
||||
if warn != nil {
|
||||
@@ -70,6 +85,7 @@ func (d *dailyWriter) Write(p []byte) (int, error) {
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
d.failedDate = ""
|
||||
}
|
||||
n, err := d.file.Write(p)
|
||||
d.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user