From 98ff4218de2d5e2ea2cb457bab02e4aa3b68c63d Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 21:45:36 +1100 Subject: [PATCH] fix(daemon): drain tasks in their own loop, not on the poll tick --- internal/daemon/daemon.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 7207c0a52..17b21f833 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -334,6 +334,14 @@ func (d *Daemon) Run(ctx context.Context) error { // the ctx is cancelled. defer d.WaitForTasks() d.watchCallWaits(ctx) + if d.Tasks != nil { + drained := make(chan struct{}) + defer func() { <-drained }() + go func() { + defer close(drained) + d.drainLoop(ctx) + }() + } interval := d.Cfg.Get().PollInterval.Std() ticker := time.NewTicker(interval) defer ticker.Stop() @@ -353,6 +361,23 @@ func (d *Daemon) Run(ctx context.Context) error { } } +// drainLoop is the only task drainer while Run is active. It runs apart from +// the poll tick so a queued task starts within about a second regardless of +// poll_interval or forge latency. Submit never blocks, so a full dispatcher +// just leaves tasks waiting on slots and the loop stays paced by the ticker. +func (d *Daemon) drainLoop(ctx context.Context) { + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + d.drainNATS(ctx) + select { + case <-ctx.Done(): + return + case <-ticker.C: + } + } +} + // pollIssuesWithConfig returns the repo's issues the dispatch rule makes // work, using the given forge client and dispatch config. complete is false when any part of the poll failed or was skipped, so // the result must not be read as the whole eligible set. @@ -421,6 +446,7 @@ func (d *Daemon) RunOnce(ctx context.Context) { if d.Tasks == nil { return } + d.drainNATS(ctx) ticker := time.NewTicker(time.Second) defer ticker.Stop() for d.taskDispatcher().inFlight() > 0 { @@ -433,9 +459,9 @@ func (d *Daemon) RunOnce(ctx context.Context) { } } -// Cycle is one poll-and-drain pass: enqueue newly assigned issues, -// reconcile open PRs, run the binding dispatch loop, then process -// queued tasks concurrently up to containers.max_concurrency. +// Cycle is one poll pass: enqueue newly assigned issues, reconcile open PRs +// and run the binding dispatch loop. Draining queued tasks is the caller's job +// (drainLoop in Run, drainNATS in RunOnce) so there is one drainer. func (d *Daemon) Cycle(ctx context.Context) { d.cleanupExpiredStorage(ctx) d.poll(ctx) @@ -443,9 +469,6 @@ func (d *Daemon) Cycle(ctx context.Context) { d.scanPRReviews(ctx) d.dispatchBindings(ctx) d.drainReactions(ctx) - if d.Tasks != nil { - d.drainNATS(ctx) - } } func (d *Daemon) cleanupExpiredStorage(ctx context.Context) {