Show when a job was first sent to the printer

A failed job's error is its last attempt's, so it cannot say whether an
earlier attempt wrote part of the receipt: a first attempt cut off by a
write error and a second that fails to dial end with a dial error. Job
status now has sent_at, the time the first attempt was about to write
to the printer, taken from the delivery's first sending mark. A failed
job with sent_at reached the printer and may have printed in part; one
without it never did.

Part of stage 1c of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-12-01 13:44:46 -06:00
1 parent 7e6cda16f8
commit 9b25c1bfd3
6 files changed
+89 -9

No files matched your search

+16 -5
View File
@@ -87,6 +87,10 @@ type Status struct {
CreatedAt time.Time `json:"created_at"`
// StartedAt is when the worker took the job.
StartedAt *time.Time `json:"started_at,omitempty"`
// SentAt is when the first attempt was about to write to the printer.
// A failed job with sent_at reached the printer, and part of it may have
// printed; one without it never reached the printer.
SentAt *time.Time `json:"sent_at,omitempty"`
// FinishedAt is when the job was done or failed.
FinishedAt *time.Time `json:"finished_at,omitempty"`
}
@@ -637,20 +641,19 @@ func (e *entry) status() Status {
Source: e.rec.Source,
CreatedAt: e.rec.CreatedAt,
}
var started, finished time.Time
var started, sent, finished time.Time
for _, d := range e.deliveries {
st.Attempts += d.Attempts
if st.Error == "" {
st.Error = d.Error
}
if !d.StartedAt.IsZero() && (started.IsZero() || d.StartedAt.Before(started)) {
started = d.StartedAt
}
started = earliest(started, d.StartedAt)
sent = earliest(sent, d.SentAt)
if d.FinishedAt.After(finished) {
finished = d.FinishedAt
}
}
st.StartedAt = timePtr(started)
st.StartedAt, st.SentAt = timePtr(started), timePtr(sent)
if e.finished {
st.FinishedAt = timePtr(finished)
}
@@ -685,6 +688,14 @@ func jobState(ds []*delivery) State {
return StateDone
}
// earliest returns the earlier of a and b, ignoring a zero time.
func earliest(a, b time.Time) time.Time {
if a.IsZero() || (!b.IsZero() && b.Before(a)) {
return b
}
return a
}
// timePtr returns &t, or nil for the zero time.
func timePtr(t time.Time) *time.Time {
if t.IsZero() {