github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/health/nodehelth.go (about) 1 package health 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "time" 8 9 "database/sql" 10 11 "github.com/mdaxf/iac-signalr/signalr" 12 "go.mongodb.org/mongo-driver/mongo" 13 14 "github.com/mdaxf/iac/com" 15 16 "github.com/mdaxf/iac/health/checks" 17 "github.com/mdaxf/iac/logger" 18 ) 19 20 // CheckSystemHealth is a function that checks the health of the system. 21 // It takes a gin.Context as input and returns a map[string]interface{} and an error. 22 // The function registers various health checks for different components of the system, 23 // such as HTTP, MongoDB, MySQL, MQTT, and SignalR. 24 // It measures the health of the system and returns the result as a JSON-encoded map. 25 26 func CheckNodeHealth(Node map[string]interface{}, db *sql.DB, mongoClient *mongo.Client, signalRClient signalr.Client) (map[string]interface{}, error) { 27 iLog := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "Node Status Check"} 28 29 startTime := time.Now() 30 defer func() { 31 elapsed := time.Since(startTime) 32 iLog.PerformanceWithDuration("framework.health.CheckNodeHealth", elapsed) 33 }() 34 35 defer func() { 36 if r := recover(); r != nil { 37 fmt.Println("Error:", r) 38 } 39 }() 40 41 h, _ := New(WithComponent(Component{ 42 Name: Node["Name"].(string), 43 Instance: Node["AppID"].(string), 44 InstanceName: Node["Description"].(string), 45 InstanceType: Node["Type"].(string), 46 Version: Node["Version"].(string), 47 })) 48 49 h.systemInfoEnabled = true 50 51 if mongoClient != nil { 52 h.Register(Config{ 53 Name: "mongoDB", 54 Check: func(ctx context.Context) error { 55 return checks.CheckMongoClientStatus(ctx, mongoClient) 56 }, 57 }) 58 } 59 if db != nil { 60 h.Register(Config{ 61 Name: "mysql", 62 Check: func(ctx context.Context) error { 63 return checks.CheckMySQLStatus(ctx, db, "") 64 }, 65 }) 66 } 67 if signalRClient != nil { 68 h.Register(Config{ 69 Name: "signalR", 70 Check: func(ctx context.Context) error { 71 return checks.CheckSignalClientStatus(ctx, signalRClient) 72 }, 73 }) 74 } 75 76 m := h.Measure(context.Background()) 77 data, err := json.Marshal(m) 78 if err != nil { 79 return make(map[string]interface{}), err 80 } 81 82 jdata, err := com.ConvertbytesToMap(data) 83 84 if err != nil { 85 return make(map[string]interface{}), err 86 } 87 88 return jdata, nil 89 }