Stop the print queue when its store fails

A store write that fails after start, such as on a full disk or a
damaged page, must stop the daemon, and nothing is retried: a write
that failed once seldom succeeds a moment later. The queue now stops on
the first store error. Submit answers that error once and ErrClosed
after, the job in hand writes nothing more to the store and keeps the
state the store last recorded, so one not yet marked sending reads as
queued again, and Run returns the error. A failure that an insert meets
while shutdown waits for it is returned too.

Every store error that stops the queue is marked ErrStoreFailed. serve
logs it once at ERROR, reports it to Sentry once and stops the daemon.
A request whose job could not be stored answers 500 internal_error
without being logged or reported again, so the failure is reported
once whoever submitted the job. docs/configuration.md lists it under
Logs.

With the memory store nothing fails, so the daemon behaves 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:44:43 -06:00
1 parent 14c5e86ab6
commit 7e6cda16f8
7 files changed
+349 -45

No files matched your search

+14 -1
View File
@@ -137,7 +137,7 @@ func run(ctx context.Context, cfg *config.Config, build BuildInfo, out io.Writer
"paper_dots", profile.DotsPerLine)
group, groupCtx := errgroup.WithContext(ctx)
group.Go(func() error { return queue.Run(groupCtx) })
group.Go(func() error { return runQueue(groupCtx, queue, logger) })
group.Go(func() error { return srv.Serve(groupCtx, ln) })
for _, sub := range subscribers {
handler := &ntfyPrinter{name: sub.Status().Name, jobs: queue, env: env, logger: logger}
@@ -214,6 +214,19 @@ func initSentry(cfg config.SentryConfig, build BuildInfo, logger *slog.Logger) (
return func() { sentry.Flush(sentryFlushTimeout) }, nil
}
// runQueue runs queue until ctx is cancelled. A job store error stops the
// queue, and with it the daemon: it is logged and reported to Sentry here,
// once, and returned.
func runQueue(ctx context.Context, queue *printqueue.Queue, logger *slog.Logger) error {
err := queue.Run(ctx)
if err != nil {
logger.Error("print queue stopped: the job store failed", "err", err)
sentry.CurrentHub().Clone().CaptureException(err)
return fmt.Errorf("print queue: %w", err)
}
return nil
}
// closeStore releases the job store once the daemon has stopped.
func closeStore(store printqueue.Store, logger *slog.Logger) {
if err := store.Close(); err != nil {
+35
View File
@@ -4,8 +4,10 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"os"
@@ -16,6 +18,8 @@ import (
"time"
"uberbringer/internal/pkg/config"
"uberbringer/internal/pkg/printqueue"
"uberbringer/internal/pkg/receipt"
)
// fakePrinter records everything sent to it.
@@ -197,3 +201,34 @@ func waitFor(t *testing.T, cond func() bool) {
}
t.Fatal("condition not met within 10s")
}
// idleFailingStore is a memory store whose idle checkpoint fails.
type idleFailingStore struct{ *printqueue.MemoryStore }
func (idleFailingStore) Idle(context.Context) error { return errors.New("disk I/O error") }
// noRender and noSend stand in for the renderer and the printer of a
// queue that never prints.
type noRender struct{}
func (noRender) Render(context.Context, receipt.Job) ([]byte, error) { return nil, nil }
type noSend struct{}
func (noSend) Send(context.Context, []byte, func() error) error { return nil }
// A store error that stops the queue is logged once at ERROR and returned,
// so the daemon stops.
func TestRunQueueReportsAStoreFailureOnce(t *testing.T) {
var logs bytes.Buffer
logger := slog.New(slog.NewTextHandler(&logs, nil))
queue := printqueue.New(noRender{}, noSend{}, idleFailingStore{printqueue.NewMemoryStore()}, logger,
printqueue.Options{Size: 1})
err := runQueue(context.Background(), queue, logger)
if !errors.Is(err, printqueue.ErrStoreFailed) || !strings.Contains(err.Error(), "disk I/O error") {
t.Fatalf("runQueue = %v, want the store's error", err)
}
if n := strings.Count(logs.String(), "level=ERROR"); n != 1 || !strings.Contains(logs.String(), "print queue stopped") {
t.Errorf("log %q, want one ERROR line", logs.String())
}
}
@@ -88,6 +88,10 @@ func (s *Server) submit(c fiber.Ctx, job receipt.Job, source string) error {
return newAPIError(codeQueueFull)
case errors.Is(err, printqueue.ErrClosed):
return newAPIError(codeShuttingDown)
case errors.Is(err, printqueue.ErrStoreFailed):
// The store error has stopped the queue, whose runner logs and
// reports it, once; the request only answers it.
return newAPIError(codeInternal)
case err != nil:
return fmt.Errorf("submitting job: %w", err)
}
@@ -582,6 +582,30 @@ func TestPrintQueueRefusals(t *testing.T) {
}
}
// A job the store could not take answers internal_error without being
// logged or reported here: the store error stopped the queue, whose
// runner reports it once.
func TestStoreFailureInSubmitIsLeftToTheQueue(t *testing.T) {
events := captureSentryEvents(t)
err := fmt.Errorf("%w: storing job: disk I/O error", printqueue.ErrStoreFailed)
s, logs := newLoggedServer(t, &fakeJobs{submitErr: err}, &fakePrinter{})
resp, body := send(t, s, printRequest(t, validJob, "Bearer "+validToken(t)))
expectError(t, resp, body, http.StatusInternalServerError, "internal_error")
if bytes.Contains(body, []byte("disk")) {
t.Errorf("body %s repeats the store's error", body)
}
lines := logs.lines(t)
if failures := errorLines(lines); len(failures) != 0 {
t.Errorf("error lines = %v, want none", failures)
}
if requests := linesAt(lines, slog.LevelWarn.String(), "request"); len(requests) != 1 {
t.Errorf("request lines = %v, want one warning", linesAt(lines, "", "request"))
}
if n := events.count(); n != 0 {
t.Errorf("%d Sentry events, want none", n)
}
}
func TestJobStatus(t *testing.T) {
id := newJobID(t)
jobs := &fakeJobs{statuses: map[printqueue.JobID]printqueue.Status{