fix(controlplane): playbook dispatch resolves and caches per the task's org
Issue: archie-core-5t4z7
This commit is contained in:
2 files changed
+31
-17
No files matched your search
@@ -10,6 +10,7 @@ import (
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
)
|
||||
@@ -26,50 +27,63 @@ type LivePlaybooks struct {
|
||||
log *slog.Logger
|
||||
ApplyStatus *applystatus.Reporter
|
||||
|
||||
mu sync.Mutex
|
||||
mu sync.Mutex
|
||||
orgs map[org.OrgID]*orgPlaybooks
|
||||
}
|
||||
|
||||
// orgPlaybooks is one org's compiled set and the version it came from.
|
||||
type orgPlaybooks struct {
|
||||
version int64
|
||||
store *playbook.Store
|
||||
}
|
||||
|
||||
func NewLivePlaybooks(client pb.ControlPlaneServiceClient, log *slog.Logger) *LivePlaybooks {
|
||||
return &LivePlaybooks{rpc: client, log: log}
|
||||
return &LivePlaybooks{rpc: client, log: log, orgs: map[org.OrgID]*orgPlaybooks{}}
|
||||
}
|
||||
|
||||
// current reads the playbooks of the org ctx acts in; the query carries that
|
||||
// org, so a task is matched against its own org's document.
|
||||
func (l *LivePlaybooks) current(ctx context.Context) *playbook.Store {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
id := org.OrgFromContext(ctx)
|
||||
held := l.orgs[id]
|
||||
if held == nil {
|
||||
held = &orgPlaybooks{}
|
||||
l.orgs[id] = held
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, playbookQueryTimeout)
|
||||
defer cancel()
|
||||
response, err := l.rpc.Query(ctx, &pb.QueryRequest{Kind: PlaybooksKind})
|
||||
if err != nil {
|
||||
l.ApplyStatus.Report(ctx, PlaybooksKind, 0, err)
|
||||
l.log.Warn("eda playbooks unavailable; keeping the last good set", "err", controlplanerpc.ClientError(err))
|
||||
return l.store
|
||||
return held.store
|
||||
}
|
||||
if l.store != nil && response.Resource.Version == l.version {
|
||||
l.ApplyStatus.Report(ctx, PlaybooksKind, l.version, nil)
|
||||
return l.store
|
||||
if held.store != nil && response.Resource.Version == held.version {
|
||||
l.ApplyStatus.Report(ctx, PlaybooksKind, held.version, nil)
|
||||
return held.store
|
||||
}
|
||||
var collection PlaybookCollection
|
||||
if err := json.Unmarshal(response.Resource.ValueJson, &collection); err != nil {
|
||||
l.ApplyStatus.Report(ctx, PlaybooksKind, response.Resource.Version, err)
|
||||
l.log.Error("eda playbooks undecodable; keeping the last good set", "err", err)
|
||||
return l.store
|
||||
return held.store
|
||||
}
|
||||
store, err := CompilePlaybooks(collection)
|
||||
if err != nil {
|
||||
l.ApplyStatus.Report(ctx, PlaybooksKind, response.Resource.Version, err)
|
||||
l.log.Error("eda playbooks do not compile; keeping the last good set", "err", err)
|
||||
return l.store
|
||||
return held.store
|
||||
}
|
||||
l.version, l.store = response.Resource.Version, store
|
||||
l.ApplyStatus.Report(ctx, PlaybooksKind, l.version, nil)
|
||||
l.log.Info("eda playbooks loaded", "version", l.version, "playbooks", len(store.Playbooks))
|
||||
held.version, held.store = response.Resource.Version, store
|
||||
l.ApplyStatus.Report(ctx, PlaybooksKind, held.version, nil)
|
||||
l.log.Info("eda playbooks loaded", "org", id, "version", held.version, "playbooks", len(store.Playbooks))
|
||||
return store
|
||||
}
|
||||
|
||||
func (l *LivePlaybooks) Dispatch(input playbook.DispatchInput) (playbook.Decision, bool) {
|
||||
return l.current(context.Background()).Dispatch(input)
|
||||
func (l *LivePlaybooks) Dispatch(ctx context.Context, input playbook.DispatchInput) (playbook.Decision, bool) {
|
||||
return l.current(ctx).Dispatch(input)
|
||||
}
|
||||
|
||||
func (l *LivePlaybooks) Run(ctx context.Context, ledger storecontract.PlaybookDispatcher, log *slog.Logger, input playbook.DispatchInput) error {
|
||||
|
||||
@@ -196,7 +196,7 @@ type Daemon struct {
|
||||
// Playbooks are the loaded EDA playbooks, consulted before kind/label
|
||||
// bindings when pinning a workflow. Nil means none.
|
||||
Playbooks interface {
|
||||
Dispatch(playbook.DispatchInput) (playbook.Decision, bool)
|
||||
Dispatch(context.Context, playbook.DispatchInput) (playbook.Decision, bool)
|
||||
Run(context.Context, storecontract.PlaybookDispatcher, *slog.Logger, playbook.DispatchInput) error
|
||||
}
|
||||
// PlaybookLedger is the at-most-once gate action playbooks run through.
|
||||
@@ -2132,11 +2132,11 @@ func (d *Daemon) runActionPlaybooks(ctx context.Context, task *workflow.Task) {
|
||||
|
||||
// resolveWorkflowID picks the definition to pin: a matching playbook, then
|
||||
// the kind defaults. A task already naming a workflow keeps it.
|
||||
func (d *Daemon) resolveWorkflowID(task *workflow.Task, available map[string]struct{}) (string, error) {
|
||||
func (d *Daemon) resolveWorkflowID(ctx context.Context, task *workflow.Task, available map[string]struct{}) (string, error) {
|
||||
if d.Playbooks == nil || task.Workflow != "" {
|
||||
return workflow.ResolveWorkflowID(task, available)
|
||||
}
|
||||
decision, matched := d.Playbooks.Dispatch(playbookInput(task))
|
||||
decision, matched := d.Playbooks.Dispatch(ctx, playbookInput(task))
|
||||
if !matched {
|
||||
return workflow.ResolveWorkflowID(task, available)
|
||||
}
|
||||
@@ -2181,7 +2181,7 @@ func (d *Daemon) pinWorkflowFromCollection(ctx context.Context, task *workflow.T
|
||||
for _, definition := range collection.Definitions {
|
||||
available[definition.ID] = struct{}{}
|
||||
}
|
||||
id, err := d.resolveWorkflowID(task, available)
|
||||
id, err := d.resolveWorkflowID(ctx, task, available)
|
||||
if err != nil {
|
||||
return pinFailure{taskstate.ParkNeedsHuman, err}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user