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
@@ -80,6 +80,9 @@ version may break callers; such changes are listed under **Breaking**.
|
|||||||
`module:<name>`, or `ntfy:<subscription>`. The worker's log lines carry
|
`module:<name>`, or `ntfy:<subscription>`. The worker's log lines carry
|
||||||
the source and the ID of the request, or of the ntfy message, that sent
|
the source and the ID of the request, or of the ntfy message, that sent
|
||||||
the job.
|
the job.
|
||||||
|
- `GET /api/v1/jobs/:id` reports `sent_at`, when the first attempt was about
|
||||||
|
to write to the printer. A failed job with `sent_at` reached the printer
|
||||||
|
and may have printed in part; one without it never did.
|
||||||
- `image` elements take `"optional": true`: an image that cannot be fetched
|
- `image` elements take `"optional": true`: an image that cannot be fetched
|
||||||
or decoded prints as `[image unavailable]` instead of failing the job,
|
or decoded prints as `[image unavailable]` instead of failing the job,
|
||||||
and one whose `width` is wider than the paper shrinks to it.
|
and one whose `width` is wider than the paper shrinks to it.
|
||||||
|
|||||||
+5
-1
@@ -118,12 +118,16 @@ curl -H @"$HOME/.uberbringer-auth" https://printer.example.com/api/v1/jobs/job_0
|
|||||||
"attempts": 1,
|
"attempts": 1,
|
||||||
"created_at": "2026-11-29T21:04:05Z",
|
"created_at": "2026-11-29T21:04:05Z",
|
||||||
"started_at": "2026-11-29T21:04:05Z",
|
"started_at": "2026-11-29T21:04:05Z",
|
||||||
|
"sent_at": "2026-11-29T21:04:05Z",
|
||||||
"finished_at": "2026-11-29T21:04:06Z"
|
"finished_at": "2026-11-29T21:04:06Z"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`status` is `queued`, `printing`, `done` or `failed`. A failed job includes an
|
`status` is `queued`, `printing`, `done` or `failed`. A failed job includes an
|
||||||
`error` field. `source` says where the job came from: `print` for
|
`error` field. `sent_at` 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.
|
||||||
|
`source` says where the job came from: `print` for
|
||||||
`/api/v1/print`, `module:<name>` for a [module](#modules) route, or
|
`/api/v1/print`, `module:<name>` for a [module](#modules) route, or
|
||||||
`ntfy:<name>` for an [ntfy subscription](ntfy.md)
|
`ntfy:<name>` for an [ntfy subscription](ntfy.md)
|
||||||
in the config. The last `queue.history` finished jobs are kept.
|
in the config. The last `queue.history` finished jobs are kept.
|
||||||
|
|||||||
@@ -413,6 +413,11 @@
|
|||||||
"pattern": "^job_[0-7][0-9a-hjkmnp-tv-z]{25}$",
|
"pattern": "^job_[0-7][0-9a-hjkmnp-tv-z]{25}$",
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"sent_at": {
|
||||||
|
"description": "sent_at 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.",
|
||||||
|
"format": "date-time",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
"source": {
|
"source": {
|
||||||
"description": "source says where the job came from: print for POST /api/v1/print, module:<name> for a module route, e.g. module:ntfy, or ntfy:<subscription> for an ntfy subscription in the config.",
|
"description": "source says where the job came from: print for POST /api/v1/print, module:<name> for a module route, e.g. module:ntfy, or ntfy:<subscription> for an ntfy subscription in the config.",
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
|||||||
@@ -87,6 +87,10 @@ type Status struct {
|
|||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
// StartedAt is when the worker took the job.
|
// StartedAt is when the worker took the job.
|
||||||
StartedAt *time.Time `json:"started_at,omitempty"`
|
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 is when the job was done or failed.
|
||||||
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -637,20 +641,19 @@ func (e *entry) status() Status {
|
|||||||
Source: e.rec.Source,
|
Source: e.rec.Source,
|
||||||
CreatedAt: e.rec.CreatedAt,
|
CreatedAt: e.rec.CreatedAt,
|
||||||
}
|
}
|
||||||
var started, finished time.Time
|
var started, sent, finished time.Time
|
||||||
for _, d := range e.deliveries {
|
for _, d := range e.deliveries {
|
||||||
st.Attempts += d.Attempts
|
st.Attempts += d.Attempts
|
||||||
if st.Error == "" {
|
if st.Error == "" {
|
||||||
st.Error = d.Error
|
st.Error = d.Error
|
||||||
}
|
}
|
||||||
if !d.StartedAt.IsZero() && (started.IsZero() || d.StartedAt.Before(started)) {
|
started = earliest(started, d.StartedAt)
|
||||||
started = d.StartedAt
|
sent = earliest(sent, d.SentAt)
|
||||||
}
|
|
||||||
if d.FinishedAt.After(finished) {
|
if d.FinishedAt.After(finished) {
|
||||||
finished = d.FinishedAt
|
finished = d.FinishedAt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
st.StartedAt = timePtr(started)
|
st.StartedAt, st.SentAt = timePtr(started), timePtr(sent)
|
||||||
if e.finished {
|
if e.finished {
|
||||||
st.FinishedAt = timePtr(finished)
|
st.FinishedAt = timePtr(finished)
|
||||||
}
|
}
|
||||||
@@ -685,6 +688,14 @@ func jobState(ds []*delivery) State {
|
|||||||
return StateDone
|
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.
|
// timePtr returns &t, or nil for the zero time.
|
||||||
func timePtr(t time.Time) *time.Time {
|
func timePtr(t time.Time) *time.Time {
|
||||||
if t.IsZero() {
|
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
|
// The job the worker renders is the job submitted, read back from the
|
||||||
// store.
|
// store.
|
||||||
func TestJobRoundTripsThroughTheStore(t *testing.T) {
|
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
|
// fakeSender fails the first failures sends as a failed dial does, before
|
||||||
// beforeWrite. Then it calls beforeWrite, fails the next writeFailures
|
// 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 {
|
type fakeSender struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
failures int
|
failures int
|
||||||
writeFailures int
|
writeFailures int
|
||||||
|
script []string
|
||||||
sent []string
|
sent []string
|
||||||
block chan struct{} // if set, Send waits for it to close
|
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.block
|
||||||
}
|
}
|
||||||
f.mu.Lock()
|
f.mu.Lock()
|
||||||
if f.failures > 0 {
|
step := ""
|
||||||
f.failures--
|
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()
|
f.mu.Unlock()
|
||||||
return errors.New("printer offline")
|
return errors.New("printer offline")
|
||||||
}
|
}
|
||||||
|
if step == "write" {
|
||||||
|
f.writeFailures++
|
||||||
|
}
|
||||||
f.mu.Unlock()
|
f.mu.Unlock()
|
||||||
if err := beforeWrite(); err != nil {
|
if err := beforeWrite(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in new issue
Block a user