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:
1 parent
dbfabd1613
commit
14c5e86ab6
5 files changed
+914
-104
No files matched your search
@@ -0,0 +1,104 @@
|
||||
package printqueue
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// deliveryKind is a delivery's state as the status rule reads it.
|
||||
type deliveryKind struct {
|
||||
name string
|
||||
state StoredState
|
||||
taken bool
|
||||
}
|
||||
|
||||
var deliveryKinds = []deliveryKind{
|
||||
{"waiting", StoredQueued, false},
|
||||
{"taken", StoredQueued, true},
|
||||
{"sending", StoredSending, true},
|
||||
{"done", StoredDone, false},
|
||||
{"failed", StoredFailed, false},
|
||||
}
|
||||
|
||||
// A job's status follows from its deliveries': queued while every one
|
||||
// waits, printing while any is unfinished once one was taken or finished,
|
||||
// and once all have finished, failed over done. Every mix of one, two and
|
||||
// three deliveries is checked.
|
||||
func TestJobStateFromDeliveries(t *testing.T) {
|
||||
for n := 1; n <= 3; n++ {
|
||||
for _, mix := range mixes(n) {
|
||||
var ds []*delivery
|
||||
var names []string
|
||||
waiting, finished, failed := 0, 0, 0
|
||||
for _, k := range mix {
|
||||
ds = append(ds, &delivery{Delivery: Delivery{State: k.state}, taken: k.taken})
|
||||
names = append(names, k.name)
|
||||
switch k.name {
|
||||
case "waiting":
|
||||
waiting++
|
||||
case "done":
|
||||
finished++
|
||||
case "failed":
|
||||
finished++
|
||||
failed++
|
||||
}
|
||||
}
|
||||
want := StateDone
|
||||
switch {
|
||||
case waiting == n:
|
||||
want = StateQueued
|
||||
case finished < n:
|
||||
want = StatePrinting
|
||||
case failed > 0:
|
||||
want = StateFailed
|
||||
}
|
||||
if got := jobState(ds); got != want {
|
||||
t.Errorf("%s: %s, want %s", strings.Join(names, ", "), got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
mix []string
|
||||
want State
|
||||
}{
|
||||
{[]string{"waiting", "waiting"}, StateQueued},
|
||||
{[]string{"waiting", "done"}, StatePrinting},
|
||||
{[]string{"failed", "waiting", "sending"}, StatePrinting},
|
||||
{[]string{"done", "failed", "done"}, StateFailed},
|
||||
{[]string{"done", "done", "done"}, StateDone},
|
||||
} {
|
||||
var ds []*delivery
|
||||
for _, name := range tt.mix {
|
||||
k := kindNamed(name)
|
||||
ds = append(ds, &delivery{Delivery: Delivery{State: k.state}, taken: k.taken})
|
||||
}
|
||||
if got := jobState(ds); got != tt.want {
|
||||
t.Errorf("%v: %s, want %s", tt.mix, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// mixes returns every sequence of n delivery kinds.
|
||||
func mixes(n int) [][]deliveryKind {
|
||||
if n == 0 {
|
||||
return [][]deliveryKind{nil}
|
||||
}
|
||||
var out [][]deliveryKind
|
||||
for _, rest := range mixes(n - 1) {
|
||||
for _, k := range deliveryKinds {
|
||||
out = append(out, append(append([]deliveryKind(nil), rest...), k))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func kindNamed(name string) deliveryKind {
|
||||
for _, k := range deliveryKinds {
|
||||
if k.name == name {
|
||||
return k
|
||||
}
|
||||
}
|
||||
panic(fmt.Sprintf("no delivery kind %q", name))
|
||||
}
|
||||
Reference in new issue
Block a user