Replace the print queue's channel with a waiting list

The queue held its waiting jobs in a channel whose capacity was
queue.size. A queue that restores jobs from a store at start must hold
however many were stored, even after queue.size was lowered, so it
needs a list it can grow. The waiting jobs are now a slice in
submission order, under the status lock, with a channel of one that
wakes the worker. Pending reads the slice's length, and the capacity
check runs against it.

Nothing else changes: the existing tests pass unchanged.

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:41:17 -06:00
1 parent 888470bbdc
commit dbfabd1613
1 file changed
+49 -28
+49 -28
View File
@@ -135,13 +135,16 @@ type Queue struct {
sender Sender sender Sender
logger *slog.Logger logger *slog.Logger
options Options options Options
jobs chan queuedJob
// mu guards statuses, finished and closed. // mu guards statuses, waiting, finished and closed.
mu sync.Mutex mu sync.Mutex
statuses map[JobID]*Status statuses map[JobID]*Status
finished []JobID // oldest first, for history eviction waiting []queuedJob // in submission order
finished []JobID // oldest first, for history eviction
closed bool closed bool
// wake tells the worker that a job is waiting.
wake chan struct{}
} }
// New returns a Queue. Call Run to start printing. // New returns a Queue. Call Run to start printing.
@@ -160,8 +163,8 @@ func New(renderer Renderer, sender Sender, logger *slog.Logger, options Options)
sender: sender, sender: sender,
logger: logger, logger: logger,
options: options, options: options,
jobs: make(chan queuedJob, options.Size),
statuses: make(map[JobID]*Status), statuses: make(map[JobID]*Status),
wake: make(chan struct{}, 1),
} }
} }
@@ -176,16 +179,19 @@ func (q *Queue) Submit(job receipt.Job, meta Meta) (Status, error) {
q.mu.Lock() q.mu.Lock()
defer q.mu.Unlock() defer q.mu.Unlock()
if q.closed { switch {
case q.closed:
return Status{}, ErrClosed return Status{}, ErrClosed
} case len(q.waiting) >= q.options.Size:
select {
case q.jobs <- queuedJob{id: id, job: job, meta: meta}:
default:
return Status{}, ErrQueueFull return Status{}, ErrQueueFull
} }
q.waiting = append(q.waiting, queuedJob{id: id, job: job, meta: meta})
status := &Status{ID: id, State: StateQueued, Source: meta.Source, CreatedAt: time.Now().UTC()} status := &Status{ID: id, State: StateQueued, Source: meta.Source, CreatedAt: time.Now().UTC()}
q.statuses[id] = status q.statuses[id] = status
select {
case q.wake <- struct{}{}:
default:
}
return *status, nil return *status, nil
} }
@@ -202,26 +208,44 @@ func (q *Queue) Status(id JobID) (Status, error) {
// Pending returns the number of jobs waiting to print. // Pending returns the number of jobs waiting to print.
func (q *Queue) Pending() int { func (q *Queue) Pending() int {
return len(q.jobs) q.mu.Lock()
defer q.mu.Unlock()
return len(q.waiting)
} }
// Run prints jobs until ctx is cancelled. A job that is printing when ctx is // Run prints jobs until ctx is cancelled. A job that is printing when ctx is
// cancelled is finished; jobs still waiting are marked failed. // cancelled is finished; jobs still waiting are marked failed.
func (q *Queue) Run(ctx context.Context) error { func (q *Queue) Run(ctx context.Context) error {
for { for {
select { qj, ok := q.next(ctx)
case <-ctx.Done(): if !ok {
q.shutdown() q.shutdown()
return nil return nil
case qj := <-q.jobs: }
// select picks randomly when both cases are ready, so check q.process(ctx, qj)
// again: no new job may start once shutdown has begun. }
if ctx.Err() != nil { }
q.finish(qj.id, 0, errShuttingDown)
q.shutdown() // next waits for a job and takes it. It returns false once ctx is done:
return nil // no new job starts once shutdown has begun.
} func (q *Queue) next(ctx context.Context) (queuedJob, bool) {
q.process(ctx, qj) for {
q.mu.Lock()
switch {
case ctx.Err() != nil:
q.mu.Unlock()
return queuedJob{}, false
case len(q.waiting) > 0:
qj := q.waiting[0]
q.waiting[0] = queuedJob{}
q.waiting = q.waiting[1:]
q.mu.Unlock()
return qj, true
}
q.mu.Unlock()
select {
case <-ctx.Done():
case <-q.wake:
} }
} }
} }
@@ -310,13 +334,10 @@ func (q *Queue) update(id JobID, fn func(*Status)) {
func (q *Queue) shutdown() { func (q *Queue) shutdown() {
q.mu.Lock() q.mu.Lock()
q.closed = true q.closed = true
waiting := q.waiting
q.waiting = nil
q.mu.Unlock() q.mu.Unlock()
for { for _, qj := range waiting {
select { q.finish(qj.id, 0, errShuttingDown)
case qj := <-q.jobs:
q.finish(qj.id, 0, errShuttingDown)
default:
return
}
} }
} }