MCPcopy
hub / github.com/docker/compose / NewGraph

Function NewGraph

pkg/compose/dependencies.go:251–286  ·  view source on GitHub ↗

NewGraph returns the dependency graph of the services

(project *types.Project, initialStatus ServiceStatus)

Source from the content-addressed store, hash-verified

249
250// NewGraph returns the dependency graph of the services
251func NewGraph(project *types.Project, initialStatus ServiceStatus) (*Graph, error) {
252 graph := &Graph{
253 lock: sync.RWMutex{},
254 Vertices: map[string]*Vertex{},
255 }
256
257 for _, s := range project.Services {
258 graph.AddVertex(s.Name, s.Name, initialStatus)
259 }
260
261 for index, s := range project.Services {
262 for _, name := range s.GetDependencies() {
263 err := graph.AddEdge(s.Name, name)
264 if err != nil {
265 if !s.DependsOn[name].Required {
266 delete(s.DependsOn, name)
267 project.Services[index] = s
268 continue
269 }
270 if api.IsNotFoundError(err) {
271 ds, err := project.GetDisabledService(name)
272 if err == nil {
273 return nil, fmt.Errorf("service %s is required by %s but is disabled. Can be enabled by profiles %s", name, s.Name, ds.Profiles)
274 }
275 }
276 return nil, err
277 }
278 }
279 }
280
281 if b, err := graph.HasCycles(); b {
282 return nil, err
283 }
284
285 return graph, nil
286}
287
288// NewVertex is the constructor function for the Vertex
289func NewVertex(key string, service string, initialStatus ServiceStatus) *Vertex {

Callers 5

TestBuildGraphFunction · 0.85
TestBuildGraphDependsOnFunction · 0.85
reconcileContainersMethod · 0.85
InDependencyOrderFunction · 0.85
InReverseDependencyOrderFunction · 0.85

Calls 4

AddVertexMethod · 0.95
AddEdgeMethod · 0.95
HasCyclesMethod · 0.95
IsNotFoundErrorFunction · 0.92

Tested by 2

TestBuildGraphFunction · 0.68
TestBuildGraphDependsOnFunction · 0.68