This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

fix(health): improve healthcheck, make sure we close nats connections after checking

+51 -8
+42 -8
health.go
··· 10 10 "firebase.google.com/go/v4/messaging" 11 11 ll "github.com/gwennlbh/label-logger-go" 12 12 "github.com/nats-io/nats.go" 13 + "github.com/nats-io/nats.go/jetstream" 13 14 ) 14 15 15 16 type HealthResponse struct { ··· 19 20 Firebase bool `json:"firebase"` 20 21 } 21 22 23 + func (r HealthResponse) AllGood() bool { 24 + return r.Redis && r.NATS && r.ChurrosDatabase && r.Firebase 25 + } 26 + 22 27 func healthHandler(w http.ResponseWriter, r *http.Request) { 23 28 ll.Debug("Checking health due to request from %s", r.RemoteAddr) 24 29 // Set the content type to JSON 25 30 w.Header().Set("Content-Type", "application/json") 26 31 27 32 // Example response (you can modify this with your own business logic) 33 + response := CheckHealth() 34 + 35 + // Marshal the response to JSON and write it to the response writer 36 + if err := json.NewEncoder(w).Encode(response); err != nil { 37 + http.Error(w, "Unable to encode JSON", http.StatusInternalServerError) 38 + return 39 + } 40 + } 41 + 42 + func CheckHealth() HealthResponse { 28 43 response := HealthResponse{} 29 44 30 45 if err := CheckRedisHealth(); err != nil { ··· 50 65 } else { 51 66 response.Firebase = true 52 67 } 53 - 54 - // Marshal the response to JSON and write it to the response writer 55 - if err := json.NewEncoder(w).Encode(response); err != nil { 56 - http.Error(w, "Unable to encode JSON", http.StatusInternalServerError) 57 - return 58 - } 68 + return response 59 69 } 60 70 61 71 func StartHealthCheckEndpoint(port int) { ··· 79 89 return fmt.Errorf("could not connect to NATS at %s: %w", config.NatsURL, err) 80 90 } 81 91 82 - _, err = nc.JetStream() 92 + defer nc.Close() 93 + 94 + js, err := jetstream.New(nc) 83 95 if err != nil { 84 96 return fmt.Errorf("could not connect to Jetstream: %w", err) 85 97 } 86 98 87 - return nil 99 + stream, err := js.CreateStream(context.Background(), jetstream.StreamConfig{ 100 + Name: StreamName, 101 + Subjects: []string{SubjectName}, 102 + }) 103 + if err != nil { 104 + return fmt.Errorf("could not create stream: %w", err) 105 + } 106 + 107 + consumers := stream.ListConsumers(context.Background()) 108 + if consumers.Err() != nil { 109 + return fmt.Errorf("could not list consumers: %w", consumers.Err()) 110 + } 111 + 112 + for info := range consumers.Info() { 113 + if consumers.Err() != nil { 114 + return fmt.Errorf("could not get consumer info: %w", consumers.Err()) 115 + } 116 + if info.Name == ConsumerName { 117 + return nil 118 + } 119 + } 120 + 121 + return fmt.Errorf("%s not connected to stream", ConsumerName) 88 122 } 89 123 90 124 func CheckChurrosDatabaseHealth() error {
+9
server/main.go
··· 140 140 // Start healthcheck endpoint 141 141 go notella.StartHealthCheckEndpoint(config.HealthCheckPort) 142 142 143 + // Self-check 144 + ll.Log("Checking", "cyan", "health") 145 + result := notella.CheckHealth() 146 + if result.AllGood() { 147 + ll.Log("Health check", "green", "succeeded") 148 + } else { 149 + ll.Error("Health check failed: %#v", result) 150 + } 151 + 143 152 // Send EventShowScheduledJobs to the stream every 5 minutes and save schedule to redis 144 153 go func() { 145 154 for {