At start, a saved offset whose fingerprint was not the current file's meant "start on the new file": what Postfix wrote between the panel's stop and the rotation was never read - delivery statuses, stored lines, refusals, inbound journal rows, the deferred counter. - tracker.resume marks the case (behind, prev); follow calls readRotated before the new file: it opens mail.log.1 (delaycompress leaves it plain), checks by fingerprint that it is the file the place belongs to, reads it from the saved offset to its end through handle (catching up: the once-protections apply) and flushes the stored lines. - Not found, not that file, or the offset past its end (rotated twice): logged, with the saved place's time - from when nothing was read. - The fingerprint gains the file's identity (device.inode, fileid_unix.go; empty elsewhere): two logs that begin alike are two files - the review-2026-08-followups row on collisions. A fingerprint saved before (hash alone) still matches by its hash (sameLog), so the first start after the upgrade resumes where it was. - store.LogtailState reads back updated_at. Tests: stop, append, rotate, append, start - both halves read, once each, the earlier line not again; a mail.log.1 that is another file is not read from the place; sameLog with an old and a new fingerprint. CHANGELOG [Unreleased], audit plan line ticked, roadmap 23/62. Evidence: GOOS=linux go vet ./internal/logtail clean; go test ./internal/logtail ok on Windows but for TestFollowTailsAndRotates; the server run follows before the push. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// LogtailState is the persisted read position of the mail.log tailer: how far
|
|
// into the log it had got, and a fingerprint of the file that offset refers to.
|
|
// It lets the tailer resume after a panel restart instead of jumping to
|
|
// end-of-file and losing the delivery lines written while it was down.
|
|
type LogtailState struct {
|
|
Fingerprint string
|
|
Offset int64
|
|
// UpdatedAt is when the position was saved; read back only (zero on save).
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
// LogtailState returns the stored read position for path. ok is false the first
|
|
// time a path is followed (nothing persisted yet), which the tailer treats as
|
|
// "start at the end".
|
|
func (s *Store) LogtailState(path string) (LogtailState, bool, error) {
|
|
var (
|
|
st LogtailState
|
|
updated string
|
|
)
|
|
err := s.db.QueryRow(
|
|
`SELECT fingerprint, read_offset, updated_at FROM logtail_state WHERE path = ?`,
|
|
path,
|
|
).Scan(&st.Fingerprint, &st.Offset, &updated)
|
|
st.UpdatedAt, _ = time.Parse(time.RFC3339, updated)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return LogtailState{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return LogtailState{}, false, fmt.Errorf("read logtail state for %q: %w", path, err)
|
|
}
|
|
return st, true, nil
|
|
}
|
|
|
|
// SaveLogtailState records the tailer's read position for path, replacing any
|
|
// previous one. It is called on a timer while tailing, so it is a single small
|
|
// upsert rather than a transaction.
|
|
func (s *Store) SaveLogtailState(path string, st LogtailState) error {
|
|
_, err := s.db.Exec(
|
|
`INSERT INTO logtail_state (path, fingerprint, read_offset, updated_at)
|
|
VALUES (?, ?, ?, ?)
|
|
ON CONFLICT(path) DO UPDATE SET
|
|
fingerprint = excluded.fingerprint,
|
|
read_offset = excluded.read_offset,
|
|
updated_at = excluded.updated_at`,
|
|
path, st.Fingerprint, st.Offset, time.Now().UTC().Format(time.RFC3339),
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("save logtail state for %q: %w", path, err)
|
|
}
|
|
return nil
|
|
}
|