github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/health/signalrser.go (about)

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