-
Notifications
You must be signed in to change notification settings - Fork 1
/
planner.go
47 lines (37 loc) · 1 KB
/
planner.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
package planner
import "github.com/khulnasoft/meshplay/server/models/pattern/core"
// Plan struct represents a node of an execution plan
type Plan struct {
Data core.Pattern
*Graph
}
// IsFeasible returns true if the plan execution is feasible
func (p *Plan) IsFeasible() bool {
return !p.DetectCycle()
}
// Execute traverses the plan and calls the callback function
// on each of the node
func (p *Plan) Execute(cb func(string, core.Service) bool) error {
parallelGraph := NewParallelProcessGraph(p.Graph)
parallelGraph.Traverse(cb)
return nil
}
// CreatePlan takes in the application components and creates a plan of execution for it
func CreatePlan(pattern core.Pattern, invert bool) (*Plan, error) {
g := NewGraph()
for name, svc := range pattern.Services {
g.AddNode(name, *svc)
}
for name, svc := range pattern.Services {
for _, dep := range svc.DependsOn {
from := dep
to := name
if invert {
from = name
to = dep
}
g.AddEdge(from, to)
}
}
return &Plan{pattern, g}, nil
}