Files
archie/internal/daemon/call_waits.go
T
sam 1f99fb592e fix(daemon): let a run waiting on a callee release its capacity
A wait:true workflow.call run held its global slot and its repo's place for
the callee's whole life, and the drain pass waited for its own tasks, so a
callee queued by that pass was never claimed. With containers.max_concurrency
saturated, or a repo that does not allow concurrency, the wait could not end.

A drain pass now returns once the stream and the claim fallback are empty, and
shutdown waits for in-flight work once. Suspend/Resume give a run's slot and
repo place back while it waits on a callee, driven by the workflow call events
on the daemon's bus; a hold records one slot and one place, so a second
re-acquire gives back what it took.
2026-10-08 22:31:07 +11:00

54 lines
1.5 KiB
Go

package daemon
import (
"context"
"github.com/samcharles93/archie-core/internal/events"
)
// callWaitBuffer is the watcher's subscription buffer. The bus carries the
// whole agent-event stream and drops for a subscriber whose buffer is full, so
// this has to absorb a burst rather than overflow on one.
const callWaitBuffer = 1024
// watchCallWaits gives a run's execution capacity back while it waits on a
// callee, and takes it again when the wait ends. The events arrive on the
// daemon's own bus, republished from the container running the workflow, and
// carry the caller's task id.
func (d *Daemon) watchCallWaits(ctx context.Context) {
if d.Bus == nil {
return
}
sub := d.Bus.Subscribe(callWaitBuffer)
dispatcher := d.taskDispatcher()
go func() {
defer sub.Close()
for {
select {
case <-ctx.Done():
return
case e, ok := <-sub.C:
if !ok {
return
}
applyCallWait(dispatcher, e)
}
}
}()
}
// applyCallWait suspends the run a call start names and resumes it when the
// call finishes. It stays non-blocking: a full subscriber buffer drops events,
// and a dropped start leaves a waiting run holding the capacity its callee
// needs, so this must never wait on anything.
func applyCallWait(dispatcher *taskDispatcher, e events.Event) {
switch e.Kind {
case events.KindWorkflowCallStarted:
if waiting, _ := e.Data["wait"].(bool); waiting {
dispatcher.Suspend(e.TaskID)
}
case events.KindWorkflowCallFinished:
dispatcher.Resume(e.TaskID)
}
}