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:
1 parent
14c5e86ab6
commit
7e6cda16f8
7 files changed
+349
-45
No files matched your search
@@ -260,6 +260,189 @@ func (f rendererFunc) Render(_ context.Context, job receipt.Job) ([]byte, error)
|
||||
return f(job)
|
||||
}
|
||||
|
||||
// When Add fails, Submit returns the error, nothing is queued, the queue
|
||||
// stops and Run returns the error; later jobs are refused as closed.
|
||||
func TestStoreFailureOnAdd(t *testing.T) {
|
||||
store := newTestStore(printqueue.NewMemoryStore())
|
||||
sender := &fakeSender{}
|
||||
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{})
|
||||
stop, done := runQueue(t, q)
|
||||
|
||||
store.failNext("add", errDisk)
|
||||
if _, err := q.Submit(job("x"), printqueue.Meta{}); !errors.Is(err, errDisk) || !errors.Is(err, printqueue.ErrStoreFailed) {
|
||||
t.Fatalf("Submit = %v, want the store's error marked ErrStoreFailed", err)
|
||||
}
|
||||
waitClosed(t, done, "Run's return")
|
||||
if err := stop(); !errors.Is(err, errDisk) || !errors.Is(err, printqueue.ErrStoreFailed) {
|
||||
t.Errorf("Run = %v, want the store's error marked ErrStoreFailed", err)
|
||||
}
|
||||
if q.Pending() != 0 || len(sender.Sent()) != 0 {
|
||||
t.Errorf("pending %d, sent %v; want nothing", q.Pending(), sender.Sent())
|
||||
}
|
||||
if _, err := q.Submit(job("y"), printqueue.Meta{}); !errors.Is(err, printqueue.ErrClosed) {
|
||||
t.Errorf("Submit after the failure = %v, want ErrClosed", err)
|
||||
}
|
||||
}
|
||||
|
||||
// When the sending mark fails, nothing is written, the job never reads
|
||||
// failed and the queue stops.
|
||||
func TestStoreFailureOnSending(t *testing.T) {
|
||||
store := newTestStore(printqueue.NewMemoryStore())
|
||||
sender := &fakeSender{}
|
||||
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{})
|
||||
store.failNext("sending", errDisk)
|
||||
st, err := q.Submit(job("x"), printqueue.Meta{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stop, done := runQueue(t, q)
|
||||
waitClosed(t, done, "Run's return")
|
||||
if err := stop(); !errors.Is(err, errDisk) || !errors.Is(err, printqueue.ErrStoreFailed) {
|
||||
t.Errorf("Run = %v, want the store's error marked ErrStoreFailed", err)
|
||||
}
|
||||
if sent := sender.Sent(); len(sent) != 0 {
|
||||
t.Errorf("sent %v after the mark failed", sent)
|
||||
}
|
||||
if got, _ := q.Status(st.ID); got.State != printqueue.StateQueued || got.Error != "" || got.StartedAt != nil {
|
||||
t.Errorf("status %+v, want queued, as the store has it", got)
|
||||
}
|
||||
for _, c := range store.Commits() {
|
||||
if c.op != "add" {
|
||||
t.Errorf("commit %+v after the failed mark", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When Finish fails, the job keeps the state the store last recorded and
|
||||
// the queue stops.
|
||||
func TestStoreFailureOnFinish(t *testing.T) {
|
||||
store := newTestStore(printqueue.NewMemoryStore())
|
||||
sender := &fakeSender{}
|
||||
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{})
|
||||
store.failNext("finish", errDisk)
|
||||
st, _ := q.Submit(job("x"), printqueue.Meta{})
|
||||
second, _ := q.Submit(job("y"), printqueue.Meta{})
|
||||
stop, done := runQueue(t, q)
|
||||
waitClosed(t, done, "Run's return")
|
||||
if err := stop(); !errors.Is(err, errDisk) {
|
||||
t.Errorf("Run = %v, want the store's error", err)
|
||||
}
|
||||
if got, _ := q.Status(st.ID); got.State != printqueue.StatePrinting || got.FinishedAt != nil {
|
||||
t.Errorf("status %+v, want printing, as the store has it", got)
|
||||
}
|
||||
if got, _ := q.Status(second.ID); got.State != printqueue.StateQueued {
|
||||
t.Errorf("waiting job %+v, want it left queued", got)
|
||||
}
|
||||
if sent := sender.Sent(); !slices.Equal(sent, []string{"x"}) {
|
||||
t.Errorf("sent %v, want only the first job", sent)
|
||||
}
|
||||
}
|
||||
|
||||
// A failed read of a job's content or a failed Idle stops the queue too.
|
||||
func TestStoreFailureOnReadOrIdle(t *testing.T) {
|
||||
for _, op := range []string{"job", "idle"} {
|
||||
t.Run(op, func(t *testing.T) {
|
||||
store := newTestStore(printqueue.NewMemoryStore())
|
||||
q := newQueueOn(store, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{})
|
||||
store.failNext(op, errDisk)
|
||||
var st printqueue.Status
|
||||
if op == "job" {
|
||||
var err error
|
||||
if st, err = q.Submit(job("x"), printqueue.Meta{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
stop, done := runQueue(t, q)
|
||||
waitClosed(t, done, "Run's return")
|
||||
if err := stop(); !errors.Is(err, errDisk) {
|
||||
t.Errorf("Run = %v, want the store's error", err)
|
||||
}
|
||||
if got, _ := q.Status(st.ID); op == "job" && (got.State != printqueue.StateQueued || got.StartedAt != nil) {
|
||||
t.Errorf("status %+v, want queued, as the store has it", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Once a store error in Submit has stopped the queue, the job in hand
|
||||
// writes nothing more to the store: it stays queued, as the store has it.
|
||||
func TestStoreFailureLeavesTheJobInHandQueued(t *testing.T) {
|
||||
store := newTestStore(printqueue.NewDurableMemoryStore())
|
||||
started, release := make(chan struct{}, 1), make(chan struct{})
|
||||
renderer := rendererFunc(func(j receipt.Job) ([]byte, error) {
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return []byte(j.Elements[0].Text), nil
|
||||
})
|
||||
sender := &fakeSender{}
|
||||
q := newQueueOn(store, renderer, sender, discardLogger(), printqueue.Options{})
|
||||
first, _ := q.Submit(job("first"), printqueue.Meta{})
|
||||
stop, done := runQueue(t, q)
|
||||
waitClosed(t, started, "the rendering")
|
||||
|
||||
store.failNext("add", errDisk)
|
||||
if _, err := q.Submit(job("second"), printqueue.Meta{}); !errors.Is(err, printqueue.ErrStoreFailed) {
|
||||
t.Fatalf("Submit = %v, want the store's error marked ErrStoreFailed", err)
|
||||
}
|
||||
close(release)
|
||||
waitClosed(t, done, "Run's return")
|
||||
if err := stop(); !errors.Is(err, printqueue.ErrStoreFailed) {
|
||||
t.Errorf("Run = %v, want the store's error marked ErrStoreFailed", err)
|
||||
}
|
||||
for _, c := range store.Commits() {
|
||||
if c.op != "add" {
|
||||
t.Errorf("commit %+v after the store failed", c)
|
||||
}
|
||||
}
|
||||
if sent := sender.Sent(); len(sent) != 0 {
|
||||
t.Errorf("sent %v after the store failed", sent)
|
||||
}
|
||||
if got, _ := q.Status(first.ID); got.State != printqueue.StateQueued || got.StartedAt != nil {
|
||||
t.Errorf("job in hand %+v, want queued, as the store has it", got)
|
||||
}
|
||||
}
|
||||
|
||||
// An insert that fails while shutdown waits for it stops the queue, and
|
||||
// Run returns its error, with the memory store and with a durable one.
|
||||
func TestStoreFailureDuringShutdownIsReturned(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
store printqueue.Store
|
||||
}{
|
||||
{"memory store", printqueue.NewMemoryStore()},
|
||||
{"durable store", printqueue.NewDurableMemoryStore()},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := newTestStore(tt.store)
|
||||
q := newQueueOn(store, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
ran := make(chan error, 1)
|
||||
go func() { ran <- q.Run(ctx) }()
|
||||
|
||||
inAdd, releaseAdd := store.blockOn("add")
|
||||
defer releaseAdd()
|
||||
store.failNext("add", errDisk)
|
||||
submitted := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := q.Submit(job("x"), printqueue.Meta{})
|
||||
submitted <- err
|
||||
}()
|
||||
waitClosed(t, inAdd, "the insert")
|
||||
cancel()
|
||||
// The worker reaches shutdown, which waits for the insert.
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
releaseAdd()
|
||||
if err := <-submitted; !errors.Is(err, printqueue.ErrStoreFailed) {
|
||||
t.Errorf("Submit = %v, want the store's error marked ErrStoreFailed", err)
|
||||
}
|
||||
if err := <-ran; !errors.Is(err, errDisk) || !errors.Is(err, printqueue.ErrStoreFailed) {
|
||||
t.Errorf("Run = %v, want the store's error marked ErrStoreFailed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// While a commit is held, Status and Pending still answer.
|
||||
func TestStatusAnswersDuringACommit(t *testing.T) {
|
||||
store := newTestStore(printqueue.NewMemoryStore())
|
||||
@@ -356,31 +539,6 @@ func TestHistoryPrunesTheStore(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A store error is returned: by Submit, which then queues nothing, and by
|
||||
// Run, after a sending mark that failed with nothing written.
|
||||
func TestStoreErrorsAreReturned(t *testing.T) {
|
||||
store := newTestStore(printqueue.NewMemoryStore())
|
||||
sender := &fakeSender{}
|
||||
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{})
|
||||
store.failNext("add", errDisk)
|
||||
if _, err := q.Submit(job("x"), printqueue.Meta{}); !errors.Is(err, errDisk) || q.Pending() != 0 {
|
||||
t.Fatalf("Submit = %v with %d pending, want the store's error and nothing queued", err, q.Pending())
|
||||
}
|
||||
|
||||
store.failNext("sending", errDisk)
|
||||
if _, err := q.Submit(job("y"), printqueue.Meta{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stop, done := runQueue(t, q)
|
||||
waitClosed(t, done, "Run's return")
|
||||
if err := stop(); !errors.Is(err, errDisk) {
|
||||
t.Errorf("Run = %v, want the store's error", err)
|
||||
}
|
||||
if sent := sender.Sent(); len(sent) != 0 {
|
||||
t.Errorf("sent %v after the mark failed", sent)
|
||||
}
|
||||
}
|
||||
|
||||
// waitFor polls cond until it holds.
|
||||
func waitFor(t *testing.T, cond func() bool) {
|
||||
t.Helper()
|
||||
|
||||
Reference in new issue
Block a user