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:
1 parent
7e6cda16f8
commit
9b25c1bfd3
6 files changed
+89
-9
No files matched your search
@@ -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() {
|
||||
|
||||
@@ -222,6 +222,51 @@ func TestCommitsAndSyncs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// sent_at is set by the first attempt that was about to write, and kept
|
||||
// through later attempts; a failed dial sets nothing.
|
||||
func TestSentAt(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
script []string
|
||||
state printqueue.State
|
||||
marks int
|
||||
}{
|
||||
{"printed", nil, printqueue.StateDone, 1},
|
||||
{"after a failed dial", []string{"dial"}, printqueue.StateDone, 1},
|
||||
{"never dialled", []string{"dial", "dial"}, printqueue.StateFailed, 0},
|
||||
{"a write failed, then a dial", []string{"write", "dial"}, printqueue.StateFailed, 1},
|
||||
{"a write failed, then printed", []string{"write"}, printqueue.StateDone, 2},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := newTestStore(printqueue.NewMemoryStore())
|
||||
sender := &fakeSender{script: tt.script}
|
||||
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{Retries: 1})
|
||||
startQueue(t, q)
|
||||
before := time.Now().UTC()
|
||||
st, _ := q.Submit(job("x"), printqueue.Meta{})
|
||||
got := waitForState(t, q, st.ID, tt.state)
|
||||
var marks int
|
||||
for _, c := range store.Commits() {
|
||||
if c.op == "sending" {
|
||||
marks++
|
||||
}
|
||||
}
|
||||
if marks != tt.marks {
|
||||
t.Errorf("%d sending marks, want %d", marks, tt.marks)
|
||||
}
|
||||
if tt.marks == 0 {
|
||||
if got.SentAt != nil {
|
||||
t.Errorf("sent_at %v, want none", got.SentAt)
|
||||
}
|
||||
return
|
||||
}
|
||||
if got.SentAt == nil || got.SentAt.Before(before) || got.SentAt.Before(*got.StartedAt) || got.SentAt.After(*got.FinishedAt) {
|
||||
t.Errorf("status %+v, want sent_at between started_at and finished_at", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The job the worker renders is the job submitted, read back from the
|
||||
// store.
|
||||
func TestJobRoundTripsThroughTheStore(t *testing.T) {
|
||||
|
||||
@@ -25,11 +25,14 @@ func (f fakeRenderer) Render(_ context.Context, job receipt.Job) ([]byte, error)
|
||||
|
||||
// fakeSender fails the first failures sends as a failed dial does, before
|
||||
// beforeWrite. Then it calls beforeWrite, fails the next writeFailures
|
||||
// sends as a failed write does, and records data.
|
||||
// sends as a failed write does, and records data. script, while it lasts,
|
||||
// says how each send ends instead: "dial" or "write" fails it there, and
|
||||
// anything else goes on as above.
|
||||
type fakeSender struct {
|
||||
mu sync.Mutex
|
||||
failures int
|
||||
writeFailures int
|
||||
script []string
|
||||
sent []string
|
||||
block chan struct{} // if set, Send waits for it to close
|
||||
}
|
||||
@@ -39,11 +42,20 @@ func (f *fakeSender) Send(_ context.Context, data []byte, beforeWrite func() err
|
||||
<-f.block
|
||||
}
|
||||
f.mu.Lock()
|
||||
if f.failures > 0 {
|
||||
f.failures--
|
||||
step := ""
|
||||
if len(f.script) > 0 {
|
||||
step, f.script = f.script[0], f.script[1:]
|
||||
}
|
||||
if step == "dial" || f.failures > 0 {
|
||||
if step != "dial" {
|
||||
f.failures--
|
||||
}
|
||||
f.mu.Unlock()
|
||||
return errors.New("printer offline")
|
||||
}
|
||||
if step == "write" {
|
||||
f.writeFailures++
|
||||
}
|
||||
f.mu.Unlock()
|
||||
if err := beforeWrite(); err != nil {
|
||||
return err
|
||||
|
||||
Reference in new issue
Block a user