diff --git a/internal/pkg/printqueue/queue.go b/internal/pkg/printqueue/queue.go index 1a54a8d..311b60d 100644 --- a/internal/pkg/printqueue/queue.go +++ b/internal/pkg/printqueue/queue.go @@ -135,13 +135,16 @@ type Queue struct { sender Sender logger *slog.Logger options Options - jobs chan queuedJob - // mu guards statuses, finished and closed. + // mu guards statuses, waiting, finished and closed. mu sync.Mutex 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 + + // wake tells the worker that a job is waiting. + wake chan struct{} } // 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, logger: logger, options: options, - jobs: make(chan queuedJob, options.Size), 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() defer q.mu.Unlock() - if q.closed { + switch { + case q.closed: return Status{}, ErrClosed - } - select { - case q.jobs <- queuedJob{id: id, job: job, meta: meta}: - default: + case len(q.waiting) >= q.options.Size: 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()} q.statuses[id] = status + select { + case q.wake <- struct{}{}: + default: + } return *status, nil } @@ -202,26 +208,44 @@ func (q *Queue) Status(id JobID) (Status, error) { // Pending returns the number of jobs waiting to print. 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 // cancelled is finished; jobs still waiting are marked failed. func (q *Queue) Run(ctx context.Context) error { for { - select { - case <-ctx.Done(): + qj, ok := q.next(ctx) + if !ok { q.shutdown() return nil - case qj := <-q.jobs: - // select picks randomly when both cases are ready, so check - // again: no new job may start once shutdown has begun. - if ctx.Err() != nil { - q.finish(qj.id, 0, errShuttingDown) - q.shutdown() - return nil - } - q.process(ctx, qj) + } + q.process(ctx, qj) + } +} + +// next waits for a job and takes it. It returns false once ctx is done: +// no new job starts once shutdown has begun. +func (q *Queue) next(ctx context.Context) (queuedJob, bool) { + 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() { q.mu.Lock() q.closed = true + waiting := q.waiting + q.waiting = nil q.mu.Unlock() - for { - select { - case qj := <-q.jobs: - q.finish(qj.id, 0, errShuttingDown) - default: - return - } + for _, qj := range waiting { + q.finish(qj.id, 0, errShuttingDown) } }