Files
archie/internal/app/controlplane/playbooks_client.go
T

115 lines
3.9 KiB
Go

package controlplane
import (
"context"
"encoding/json"
"log/slog"
"sync"
"time"
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"
)
const playbookQueryTimeout = 5 * time.Second
// LivePlaybooks serves the daemon the org's eda-playbooks resource. Each call
// reads the resource's version and recompiles only when it moved, so an
// install or an operator edit applies to the next task. A control plane that
// cannot be reached, or a document that no longer compiles, leaves the last
// good set in force.
type LivePlaybooks struct {
rpc pb.ControlPlaneServiceClient
log *slog.Logger
ApplyStatus *applystatus.Reporter
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, orgs: map[org.OrgID]*orgPlaybooks{}}
}
// Keep loads the default org's playbooks now and every interval after, so the
// apply status is reported without waiting for the first dispatch.
func (l *LivePlaybooks) Keep(ctx context.Context, interval time.Duration) {
keepLoading(ctx, interval, func() { l.current(ctx) })
}
// keepLoading runs load now and every interval until ctx ends.
func keepLoading(ctx context.Context, interval time.Duration, load func()) {
load()
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
load()
}
}
}()
}
// 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 held.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 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 held.store
}
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(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 {
return l.current(ctx).Run(ctx, ledger, log, input)
}