|
|
|
@ -59,11 +59,25 @@ func (m *Manager) RegisterModule(name string, initFn func() (services.Service, e |
|
|
|
|
// AddDependency adds a dependency from name(source) to dependsOn(targets)
|
|
|
|
|
// An error is returned if the source module name is not found
|
|
|
|
|
func (m *Manager) AddDependency(name string, dependsOn ...string) error { |
|
|
|
|
if mod, ok := m.modules[name]; ok { |
|
|
|
|
mod.deps = append(mod.deps, dependsOn...) |
|
|
|
|
} else { |
|
|
|
|
mod, ok := m.modules[name] |
|
|
|
|
if !ok { |
|
|
|
|
return fmt.Errorf("no such module: %s", name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Ensure it doesn't introduce any circular dependency.
|
|
|
|
|
for _, newDep := range dependsOn { |
|
|
|
|
if _, ok := m.modules[newDep]; !ok { |
|
|
|
|
return fmt.Errorf("no such module: %s", newDep) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, prevDep := range m.DependenciesForModule(newDep) { |
|
|
|
|
if prevDep == name { |
|
|
|
|
return fmt.Errorf("found a circular dependency: %s depends on %s", newDep, name) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mod.deps = append(mod.deps, dependsOn...) |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -92,7 +106,7 @@ func (m *Manager) initModule(name string, initMap map[string]bool, servicesMap m |
|
|
|
|
deps := m.orderedDeps(name) |
|
|
|
|
deps = append(deps, name) // lastly, initialize the requested module
|
|
|
|
|
|
|
|
|
|
for ix, n := range deps { |
|
|
|
|
for _, n := range deps { |
|
|
|
|
// Skip already initialized modules
|
|
|
|
|
if initMap[n] { |
|
|
|
|
continue |
|
|
|
@ -111,7 +125,7 @@ func (m *Manager) initModule(name string, initMap map[string]bool, servicesMap m |
|
|
|
|
if s != nil { |
|
|
|
|
// We pass servicesMap, which isn't yet complete. By the time service starts,
|
|
|
|
|
// it will be fully built, so there is no need for extra synchronization.
|
|
|
|
|
serv = newModuleServiceWrapper(servicesMap, n, m.logger, s, m.DependenciesForModule(n), m.findInverseDependencies(n, deps[ix+1:])) |
|
|
|
|
serv = newModuleServiceWrapper(servicesMap, n, m.logger, s, m.DependenciesForModule(n), m.inverseDependenciesForModule(n)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -205,12 +219,12 @@ func (m *Manager) orderedDeps(mod string) []string { |
|
|
|
|
return result |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// find modules in the supplied list, that depend on mod
|
|
|
|
|
func (m *Manager) findInverseDependencies(mod string, mods []string) []string { |
|
|
|
|
// inverseDependenciesForModule returns the list of modules depending on the input module, sorted by name.
|
|
|
|
|
func (m *Manager) inverseDependenciesForModule(mod string) []string { |
|
|
|
|
result := []string(nil) |
|
|
|
|
|
|
|
|
|
for _, n := range mods { |
|
|
|
|
for _, d := range m.modules[n].deps { |
|
|
|
|
for n := range m.modules { |
|
|
|
|
for _, d := range m.DependenciesForModule(n) { |
|
|
|
|
if d == mod { |
|
|
|
|
result = append(result, n) |
|
|
|
|
break |
|
|
|
@ -218,6 +232,7 @@ func (m *Manager) findInverseDependencies(mod string, mods []string) []string { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sort.Strings(result) |
|
|
|
|
return result |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|