fix(daemon): drain tasks in their own loop, not on the poll tick

This commit is contained in:
sam committed 2026-10-25 21:45:57 +11:00
1 parent e2f9388071
commit 8eaf7b00c3
1 file changed
+29 -6
+29 -6
View File
@@ -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) {