forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhc.go
55 lines (51 loc) · 1.35 KB
/
hc.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
// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package router
import (
"github.com/tsuru/config"
"github.com/tsuru/tsuru/hc"
)
// BuildHealthCheck creates a healthcheck function for the given routerName.
//
// It will call the HealthCheck() method in the router (only if it's also a
// HealthChecker), for each instance of it (including the "main" instance and
// all custom routers).
func BuildHealthCheck(routerName string) func() error {
return func() error {
routerConfig, err := config.Get("routers")
if err != nil {
return hc.ErrDisabledComponent
}
routers, _ := routerConfig.(map[interface{}]interface{})
checkCount := 0
for ifaceName := range routers {
name := ifaceName.(string)
if name != routerName {
namedRouter := routers[name].(map[interface{}]interface{})
if tp, _ := namedRouter["type"].(string); tp != routerName {
continue
}
}
checkCount++
err := healthCheck(name)
if err != nil {
return err
}
}
if checkCount == 0 {
return hc.ErrDisabledComponent
}
return nil
}
}
func healthCheck(name string) error {
router, err := Get(name)
if err != nil {
return err
}
if hrouter, ok := router.(HealthChecker); ok {
return hrouter.HealthCheck()
}
return hc.ErrDisabledComponent
}