Keep print jobs in the job store

The queue now keeps its jobs in a Store, the memory store for now, so
that a store on disk can take its place. Submit stores the job as JSON
before it answers, and the worker reads it back and decodes it when it
takes it, so what prints is what was stored. Each job has one delivery,
for the printer "default", and its status derives from its deliveries:
queued while all wait, printing while any is unfinished, then failed
over done once all have finished.

Every change is committed to the store first and made in memory after,
so a status never shows what the store lacks. Once the printer's
connection is open and before the first byte, the worker records the
attempt as sending; a failed dial records nothing. A failure is
committed with a sync, since one rolled back to queued would print
after a restart. A job's content goes in the commit that finishes it,
as it went from memory before, and statuses past queue.history leave
the store in the commit that finishes the job pushing them out. A job
whose stored content no longer decodes fails without its content in
the logs.

A submit lock covers the capacity check, the insert and the append to
the waiting list; the status lock is never held across a store call, so
Status and Pending answer during a commit. A store error is returned,
by Submit with nothing queued, or by Run, which ends; shutdown is as
before.

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:42:49 -06:00
1 parent dbfabd1613
commit 14c5e86ab6
5 files changed
+914 -104

No files matched your search

+28 -9
View File
@@ -23,24 +23,37 @@ func (f fakeRenderer) Render(_ context.Context, job receipt.Job) ([]byte, error)
return []byte(job.Elements[0].Text), nil
}
// fakeSender fails the first failures sends, then records data.
// 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.
type fakeSender struct {
mu sync.Mutex
failures int
sent []string
block chan struct{} // if set, Send waits for it to close
mu sync.Mutex
failures int
writeFailures int
sent []string
block chan struct{} // if set, Send waits for it to close
}
func (f *fakeSender) Send(_ context.Context, data []byte, _ func() error) error {
func (f *fakeSender) Send(_ context.Context, data []byte, beforeWrite func() error) error {
if f.block != nil {
<-f.block
}
f.mu.Lock()
defer f.mu.Unlock()
if f.failures > 0 {
f.failures--
f.mu.Unlock()
return errors.New("printer offline")
}
f.mu.Unlock()
if err := beforeWrite(); err != nil {
return err
}
f.mu.Lock()
defer f.mu.Unlock()
if f.writeFailures > 0 {
f.writeFailures--
return errors.New("connection reset")
}
f.sent = append(f.sent, string(data))
return nil
}
@@ -60,6 +73,12 @@ func discardLogger() *slog.Logger {
}
func newQueue(r printqueue.Renderer, s printqueue.Sender, opts printqueue.Options) *printqueue.Queue {
return newQueueOn(printqueue.NewMemoryStore(), r, s, discardLogger(), opts)
}
// newQueueOn returns a queue on store, with test defaults for the options
// left zero.
func newQueueOn(store printqueue.Store, r printqueue.Renderer, s printqueue.Sender, logger *slog.Logger, opts printqueue.Options) *printqueue.Queue {
if opts.Size == 0 {
opts.Size = 10
}
@@ -69,7 +88,7 @@ func newQueue(r printqueue.Renderer, s printqueue.Sender, opts printqueue.Option
if opts.RetryDelay == 0 {
opts.RetryDelay = time.Millisecond
}
return printqueue.New(r, s, discardLogger(), opts)
return printqueue.New(r, s, store, logger, opts)
}
// startQueue runs q until the test ends. cancel cancels Run's context and
@@ -153,7 +172,7 @@ func (b *lockedBuffer) String() string {
func TestMetaInStatusAndLogs(t *testing.T) {
var logs lockedBuffer
// One failed send, so that the retry is logged too.
q := printqueue.New(fakeRenderer{}, &fakeSender{failures: 1}, slog.New(slog.NewTextHandler(&logs, nil)),
q := newQueueOn(printqueue.NewMemoryStore(), fakeRenderer{}, &fakeSender{failures: 1}, slog.New(slog.NewTextHandler(&logs, nil)),
printqueue.Options{Size: 1, History: 1, Retries: 1, RetryDelay: time.Millisecond})
startQueue(t, q)