workgraph

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package workgraph runs a set of interdependent steps, stacks to deploy or destroy and assets to build and publish, in dependency order within concurrency limits: upstream's api/work-graph (I.1).

A Graph holds Node values and the dependencies between them; Build makes the graph for deploying stacks and publishing their assets, which the deploy and publish-assets actions share; Run runs a graph with an Actions value per kind of node and returns the stack actions' results. The destroy action's graph is built from the stacks alone (G.8). Graph.RemovePublished prunes the asset publishes already done, and the builds left unused, before a run, as deploy and publish-assets do unless forced (I.2).

Design

Upstream's run is a promise loop over a mutable record of nodes; here the goroutine that calls Run owns the run's state, keeps each node's state itself, so that a graph is never changed by running it, and runs each action on a goroutine of its own, which reports its outcome on a channel. Its order is upstream's: each pass appends newly ready nodes after those already waiting, sorts them by priority, keeping their order otherwise, and starts what the limits allow, skipping a node whose kind is full for a lower one behind it. A node with no action completes as soon as its pass ends, as upstream's NOOP settles before any real work does, so with a total limit of 1 the order nodes start in depends on the graph alone. Errors are joined rather than the last kept (EI-1), the stack results completed before a failure are returned beside it (EI-4), and every action gets Run's own context, never one cancelled because a sibling failed (G.10-D3).

Node IDs: a stack's is its hierarchical ID, as cloudassembly tells stacks apart; an asset's follow upstream's scheme with the port's own hash, since two manifests can give the same asset and destination IDs different buckets, which must be two publishes (I.1-D2). The builder takes the selected stacks, and reads each one's asset manifests from its dependencies, as both upstream callers flatten them; upstream's branch for a nested assembly artifact isn't ported, since no caller reaches it.

Upstream's defects that are fixed:

  • a limit below 1 for a kind the graph has nodes of is refused before anything starts, where upstream's run starts nothing and never settles, which a concurrency of zero reaches through ?? defaults in publish-assets and deploy (I.1-D4);
  • a cycle is found before anything starts, where upstream runs everything outside it first, deploying or destroying those stacks before it fails (I.1-D6);
  • removing the waits the builder adds for display never removes a publish's wait for its build, where upstream's can, with just-in-time builds, so that a container image is pushed before it is built (I.1-D7).

Index

Constants

View Source
const (
	PriorityMarker       = 100
	PriorityAssetBuild   = 10
	PriorityStack        = 5
	PriorityAssetPublish = 0
)

Priorities are the builder's node priorities, as upstream's: markers first, so that their messages time the work; then asset builds, so that prebuilt assets are built before stacks where they can be; then stacks, before publishes.

Variables

This section is empty.

Functions

func Run

func Run[R any](ctx context.Context, io *iohelper.Helper, g *Graph, limits Limits, actions Actions[R]) ([]R, error)

Run runs every node of g after the nodes it depends on, within limits, and returns the stack actions' results in the order they finished, beside their errors joined: upstream's doParallel. A stack node with no action adds no result.

Each pass appends the nodes newly ready, in the order they were added, after those already waiting, sorts them by priority, highest first, keeping their order otherwise, and starts each one whose kind and the total are under their limits, as upstream's does. A node with no action completes once the pass that started it ends, before any action's completion is read, as upstream's NOOP settles before any real work; then a pass follows each completion. The goroutine that calls Run owns the run's state, and every action runs on a goroutine of its own, with ctx.

Before starting anything it fails with a cdkerr.KindToolkit error when a kind the graph has nodes of has a limit below 1, or the total is negative, where upstream's run never settles (I.1-D4), and with Code "DependencyCycle" when the graph has a cycle, which it traces with the graph, where upstream runs everything outside the cycle first (I.1-D6).

When an action fails, nothing new starts, the running actions finish, and the errors are joined in the order they came, where upstream keeps only the last (EI-1). When ctx is done, nothing new starts, the running actions, which have ctx, are waited for, and if any node was left unstarted the error also holds one that wraps ctx's error (G.10-D3, I.1-D5). Run never returns while an action is running.

Types

type Actions

type Actions[R any] struct {
	Stack        func(context.Context, *StackNode) (R, error)
	AssetBuild   func(context.Context, *AssetBuildNode) error
	AssetPublish func(context.Context, *AssetPublishNode) error
	Marker       func(context.Context, *MarkerNode) error
}

Actions are what Run does for each kind of node: upstream's WorkGraphActions. A nil action does nothing, as upstream's WorkGraph.NOOP, and its node completes as soon as it starts.

type AssetBuildNode

type AssetBuildNode struct {
	NodeInfo

	// Manifest is the asset manifest artifact the asset is in.
	Manifest *cloudassembly.AssetManifestArtifact

	// Entry is the asset, with the destination of the first entry that asked for the build.
	Entry cloudassembly.AssetEntry

	// ParentStack is the stack whose asset manifest it is, whose environment the asset is built for.
	ParentStack *cloudassembly.Stack
}

AssetBuildNode builds an asset, once for all its destinations.

type AssetPublishNode

type AssetPublishNode struct {
	NodeInfo

	// Manifest is the asset manifest artifact the asset is in.
	Manifest *cloudassembly.AssetManifestArtifact

	// Entry is the asset and the destination to publish it to.
	Entry cloudassembly.AssetEntry

	// ParentStack is the stack whose asset manifest it is, whose environment the asset is published for.
	ParentStack *cloudassembly.Stack
}

AssetPublishNode publishes an asset to one destination.

type BuildOptions

type BuildOptions struct {
	// JustInTime builds an asset only once the stacks its stack depends on are done, upstream's AssetBuildTime.JUST_IN_TIME, rather than as early as it can, the default, upstream's prebuild.
	JustInTime bool
}

BuildOptions are the options for Build.

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph is a set of nodes and the dependencies between them: upstream's WorkGraph, without the run's state, so that a graph can be run more than once. A dependency on a node the graph doesn't have is ignored, so that a dependency can be added before its nodes, and one on a stack that wasn't selected drops out, as upstream's removeUnavailableDependencies drops it.

A Graph isn't safe for concurrent use; Run only reads it.

func Build

func Build(stacks []*cloudassembly.Stack, opts BuildOptions) (*Graph, error)

Build returns the graph that deploys stacks and publishes their assets: upstream's WorkGraphBuilder, given the stacks and their asset manifests as both its callers give them (I.1-D1).

Each stack is a *StackNode keyed by its hierarchical ID, depending on the stacks it depends on (I.1-D2). Each asset of the stacks' asset manifests has a start and an end *MarkerNode, "start-<asset ID>" and "end-<asset ID>"; an *AssetBuildNode, "build-<asset ID>-<hash of its source>", once for all its destinations, after the start marker and the stacks its manifest depends on, and, with opts.JustInTime, those its stack depends on; and an *AssetPublishNode for each destination, whose ID adds a hash of the destination, after the build, with the end marker after every publish and the stack after its publishes. A publish also waits for the stacks its stack depends on, so that its progress doesn't interleave with theirs, as upstream's does. Dependencies on stacks that weren't given drop out.

The waits added only for display, the publishes' on their stack's dependencies and, with opts.JustInTime, the builds', are removed where they close a cycle; a publish's wait for its build never is, where upstream can remove it, so that a container image is pushed before it is built (I.1-D7).

func BuildDestroy

func BuildDestroy(stacks []*cloudassembly.Stack) (*Graph, error)

BuildDestroy returns the graph that destroys stacks: upstream's buildDestroyWorkGraph. Each stack is a *StackNode keyed by its hierarchical ID, with priority 0, and the dependencies are reversed, so that a stack is destroyed before the stacks it depends on (EI-3, G.8-D1). Only direct dependencies between the given stacks count, as upstream's; one through a stack that wasn't given doesn't order the two.

func New

func New() *Graph

New returns an empty graph.

func (*Graph) Add

func (g *Graph) Add(nodes ...Node) error

Add adds nodes to the graph. It fails with a cdkerr.KindToolkit error with Code "DuplicateNodeId" for a node whose ID the graph already has, as upstream's addNodes does, having added the nodes before it.

func (*Graph) AddDependency

func (g *Graph) AddDependency(from, to string)

AddDependency makes the node from depend on the node to; either may be added to the graph later.

func (*Graph) Dependents

func (g *Graph) Dependents(id string) iter.Seq[Node]

Dependents returns the nodes that depend on the node with the ID, in the order they were added: upstream's dependees.

func (*Graph) FindCycle

func (g *Graph) FindCycle() []string

FindCycle returns a cycle among the graph's dependencies, as the path from a node back to itself, or nil when there is none: upstream's findCycle. It walks the nodes in the order they were added and their dependencies in the order those were added, as upstream's does, so it finds the cycle upstream finds.

func (*Graph) Node

func (g *Graph) Node(id string) (Node, bool)

Node returns the node with the ID, and whether the graph has it.

func (*Graph) Nodes

func (g *Graph) Nodes() iter.Seq[Node]

Nodes returns the graph's nodes, in the order they were added.

func (*Graph) Remove

func (g *Graph) Remove(id string)

Remove removes the node, and every dependency on it.

func (*Graph) RemovePublished

func (g *Graph) RemovePublished(ctx context.Context, io *iohelper.Helper, isPublished func(context.Context, *AssetPublishNode) (bool, error)) error

RemovePublished removes the asset publishes isPublished reports as done, checking at most 8 at once, and then the asset builds no node depends on any more: upstream's removeUnnecessaryAssets (I.2). The start and end markers stay.

When a check fails, no new check starts, the running ones finish, nothing is removed, and the errors are returned joined, where upstream rejects with the last (EI-1); a done ctx stops it the same way, with ctx's error among those returned (I.2-D2).

func (*Graph) String

func (g *Graph) String() string

String returns the graph in Graphviz's DOT language, as upstream's toString writes it, with long runs of hexadecimal digits in IDs shortened.

type Limits

type Limits struct {
	Stack, AssetBuild, AssetPublish int

	// Total limits the nodes running at once whatever their kind; zero means no limit beyond each kind's.
	Total int
}

Limits are how many nodes of each kind Run runs at once, and in all; markers run one at a time, as every upstream caller has them. Upstream's number form, n for every kind and in all, is Limits{Stack: n, AssetBuild: n, AssetPublish: n, Total: n}.

type Marker

type Marker int

Marker is the point in a run a MarkerNode marks.

const (
	// MarkerStartAsset is reached before an asset's build.
	MarkerStartAsset Marker = iota + 1

	// MarkerEndAsset is reached once every destination of an asset has been published.
	MarkerEndAsset
)

type MarkerNode

type MarkerNode struct {
	NodeInfo
	Marker Marker

	// Entry is the asset the marker is for.
	Entry cloudassembly.AssetEntry
}

MarkerNode marks a point in a run, such as the start of an asset's work, for an action to report.

type Node

type Node interface {
	// Info returns what every node has.
	Info() *NodeInfo
	// contains filtered or unexported methods
}

Node is a unit of work: a *StackNode, *AssetBuildNode, *AssetPublishNode or *MarkerNode. Use a type switch to tell them apart.

type NodeInfo

type NodeInfo struct {
	// ID identifies the node in its graph.
	ID string

	// Note is a readable name for the node, shown in the graph's DOT text when its ID isn't readable.
	Note string

	// Priority orders the nodes ready to start: higher starts first.
	Priority int
}

NodeInfo is what every node has: upstream's WorkNodeCommon, without the state, which Run keeps.

func (*NodeInfo) Info

func (n *NodeInfo) Info() *NodeInfo

Info returns n.

type StackNode

type StackNode struct {
	NodeInfo
	Stack *cloudassembly.Stack
}

StackNode deploys or destroys a stack.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL