-
Notifications
You must be signed in to change notification settings - Fork 6
/
step_definition.go
84 lines (65 loc) · 2.06 KB
/
step_definition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package asyncjob
import (
"context"
"github.com/Azure/go-asyncjob/graph"
)
type stepType string
const stepTypeTask stepType = "task"
const stepTypeRoot stepType = "root"
// StepDefinitionMeta is the interface for a step definition
type StepDefinitionMeta interface {
// GetName return name of the step
GetName() string
// DependsOn return the list of step names that this step depends on
DependsOn() []string
// DotSpec used for generating graphviz graph
DotSpec() *graph.DotNodeSpec
// Instantiate a new step instance
createStepInstance(context.Context, JobInstanceMeta) StepInstanceMeta
}
// StepDefinition defines a step and it's dependencies in a job definition.
type StepDefinition[T any] struct {
name string
stepType stepType
executionOptions *StepExecutionOptions
instanceCreator func(context.Context, JobInstanceMeta) StepInstanceMeta
}
func newStepDefinition[T any](stepName string, stepType stepType, optionDecorators ...ExecutionOptionPreparer) *StepDefinition[T] {
step := &StepDefinition[T]{
name: stepName,
executionOptions: &StepExecutionOptions{},
stepType: stepType,
}
for _, decorator := range optionDecorators {
step.executionOptions = decorator(step.executionOptions)
}
return step
}
func (sd *StepDefinition[T]) GetName() string {
return sd.name
}
func (sd *StepDefinition[T]) DependsOn() []string {
return sd.executionOptions.DependOn
}
func (sd *StepDefinition[T]) createStepInstance(ctx context.Context, jobInstance JobInstanceMeta) StepInstanceMeta {
return sd.instanceCreator(ctx, jobInstance)
}
func (sd *StepDefinition[T]) DotSpec() *graph.DotNodeSpec {
return &graph.DotNodeSpec{
Name: sd.GetName(),
DisplayName: sd.GetName(),
Shape: "box",
Style: "filled",
FillColor: "gray",
Tooltip: "",
}
}
func connectStepDefinition(stepFrom, stepTo StepDefinitionMeta) *graph.DotEdgeSpec {
edgeSpec := &graph.DotEdgeSpec{
FromNodeName: stepFrom.GetName(),
ToNodeName: stepTo.GetName(),
Color: "black",
Style: "bold",
}
return edgeSpec
}