diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 76e9eb1c1..98039fba9 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -374,6 +374,9 @@ func (d *Daemon) Run(ctx context.Context) error { ticker := time.NewTicker(interval) defer ticker.Stop() for { + if d.Tasks == nil { + d.dispatchBindings(ctx) + } d.Cycle(ctx) select { case <-ctx.Done(): @@ -389,15 +392,17 @@ 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 +// drainLoop is the only task drainer and capture dispatcher while Run is +// active. It runs apart from the poll tick so a captured event becomes a +// running task 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.markPass("drain") + d.dispatchBindings(ctx) d.drainNATS(ctx) select { case <-ctx.Done(): @@ -470,6 +475,7 @@ func (d *Daemon) RunOnce(ctx context.Context) { d.Log.WarnContext(ctx, "built-in job failed", "job", job.ID, "err", err) } } + d.dispatchBindings(ctx) d.Cycle(ctx) if d.Tasks == nil { return @@ -487,12 +493,11 @@ func (d *Daemon) RunOnce(ctx context.Context) { } } -// Cycle is one poll pass: turn captured events and review reactions into -// tasks; maintenance runs as built-in schedules. Draining queued tasks is the caller's job -// (drainLoop in Run, drainNATS in RunOnce) so there is one drainer. +// Cycle is one poll pass: turn review reactions into tasks; maintenance runs +// as built-in schedules. Captured events are dispatched by the caller +// (drainLoop in Run, once in RunOnce) so they start within about a second. func (d *Daemon) Cycle(ctx context.Context) { d.markPass("cycle") - d.dispatchBindings(ctx) d.drainReactions(ctx) d.retryTransientParks(ctx) } @@ -615,9 +620,11 @@ func (d *Daemon) dispatchOrgBindings(ctx context.Context) { return } enabled := make([]string, 0, len(bindings)) + armed := make([]binding.Binding, 0, len(bindings)) for _, b := range bindings { if b.OrgID == org.OrgFromContext(ctx) && b.Status == binding.StatusArmed && b.Matcher.Source != "" && enablement.Enabled(b.OrgID, b.Workflow) { enabled = append(enabled, b.ID) + armed = append(armed, b) } } if len(enabled) == 0 { @@ -635,21 +642,16 @@ func (d *Daemon) dispatchOrgBindings(ctx context.Context) { return } for _, c := range captures { - d.dispatchCapture(ctx, c, workflows, enablement) + d.dispatchCapture(ctx, c, armed, workflows) } } -// dispatchCapture offers one capture to every armed binding on its source. A -// disabled workflow's binding is skipped like an unarmed one, so the capture -// waits rather than recording a failure every cycle. -func (d *Daemon) dispatchCapture(ctx context.Context, c storecontract.CapturedEvent, workflows workflow.WorkflowDefinitionCollection, enablement workflowtask.WorkflowEnablement) { - armed, err := d.BindingDispatcher.ArmedBindingsForSource(ctx, c.Source) - if err != nil { - d.Log.WarnContext(ctx, "armed bindings lookup", "source", c.Source, "error", err) - return - } +// dispatchCapture offers one capture to every armed binding that matches it. +// armed holds only the org's armed bindings on enabled workflows, so a +// disabled workflow's capture waits rather than recording a failure every pass. +func (d *Daemon) dispatchCapture(ctx context.Context, c storecontract.CapturedEvent, armed []binding.Binding, workflows workflow.WorkflowDefinitionCollection) { for _, b := range armed { - if b.OrgID == org.OrgFromContext(ctx) && b.Matcher.Matches(c.Source, c.Dispatchable()) && enablement.Enabled(b.OrgID, b.Workflow) { + if b.Matcher.Matches(c.Source, c.Dispatchable()) { d.dispatchOneBinding(ctx, b, c, workflows) } }