diff --git a/CHANGELOG.md b/CHANGELOG.md index 5de1a38..56043b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,9 @@ version may break callers; such changes are listed under **Breaking**. `module:`, or `ntfy:`. The worker's log lines carry the source and the ID of the request, or of the ntfy message, that sent 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 or decoded prints as `[image unavailable]` instead of failing the job, and one whose `width` is wider than the paper shrinks to it. diff --git a/docs/api.md b/docs/api.md index b6f79e8..5fa32b7 100644 --- a/docs/api.md +++ b/docs/api.md @@ -118,12 +118,16 @@ curl -H @"$HOME/.uberbringer-auth" https://printer.example.com/api/v1/jobs/job_0 "attempts": 1, "created_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" } ``` `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:` for a [module](#modules) route, or `ntfy:` for an [ntfy subscription](ntfy.md) in the config. The last `queue.history` finished jobs are kept. diff --git a/docs/swagger.json b/docs/swagger.json index 7af2268..a498054 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -413,6 +413,11 @@ "pattern": "^job_[0-7][0-9a-hjkmnp-tv-z]{25}$", "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": { "description": "source says where the job came from: print for POST /api/v1/print, module: for a module route, e.g. module:ntfy, or ntfy: for an ntfy subscription in the config.", "type": "string" diff --git a/internal/pkg/printqueue/queue.go b/internal/pkg/printqueue/queue.go index b720072..2f5624d 100644 --- a/internal/pkg/printqueue/queue.go +++ b/internal/pkg/printqueue/queue.go @@ -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() { diff --git a/internal/pkg/printqueue/queue_store_test.go b/internal/pkg/printqueue/queue_store_test.go index 04e920e..00fa7bf 100644 --- a/internal/pkg/printqueue/queue_store_test.go +++ b/internal/pkg/printqueue/queue_store_test.go @@ -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) { diff --git a/internal/pkg/printqueue/queue_test.go b/internal/pkg/printqueue/queue_test.go index 865c47d..74f3269 100644 --- a/internal/pkg/printqueue/queue_test.go +++ b/internal/pkg/printqueue/queue_test.go @@ -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