Skip to content

Commit

Permalink
Add CheckHealthy
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Sep 19, 2023
1 parent b3e9e4a commit 3d0c67c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var ErrModuleDependError = errors.New("depend error")

type Module interface {
Name() moduleName
DependOn() []Module
CheckHealthy(context.Context) error
}

Expand Down Expand Up @@ -38,6 +39,10 @@ func (m ModuleType[T]) Name() moduleName {
return m.name
}

func (m ModuleType[T]) DependOn() []Module {
return m.depends
}

func (m ModuleType[T]) Value(ctx context.Context) T {
var n T
v := ctx.Value(m.name)
Expand Down Expand Up @@ -66,3 +71,38 @@ func (m *ModuleType[T]) CheckHealthy(ctx context.Context) (err error) {
err = m.Value(ctx).CheckHealthy(ctx)
return
}

func CheckHealthy(ctx context.Context, rootModules []Module) map[moduleName]error {
ret := map[moduleName]error{}

checkModuleHealthy(ctx, rootModules, ret)

return ret
}

func checkModuleHealthy(ctx context.Context, modules []Module, errs map[moduleName]error) {
for _, m := range modules {
if _, ok := errs[m.Name()]; ok {
continue
}

depHealthy := true
if deps := m.DependOn(); len(deps) != 0 {
checkModuleHealthy(ctx, deps, errs)

for _, dep := range deps {
if errs[dep.Name()] != nil {
depHealthy = false
break
}
}
}

if !depHealthy {
errs[m.Name()] = ErrModuleDependError
continue
}

errs[m.Name()] = m.CheckHealthy(ctx)
}
}

0 comments on commit 3d0c67c

Please sign in to comment.