Skip to content

Commit

Permalink
Dependency handling community service
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushroshan committed May 28, 2024
1 parent 57c75d2 commit 3fd4695
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions services/community/api/seed/seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ func LoadMongoData(mongoClient *mongo.Client, db *gorm.DB) {
log.Println(postData) // Use the returned values as needed
}
}
log.Println("Data seeded successfully")
}
36 changes: 36 additions & 0 deletions services/community/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package api

import (
"fmt"
"log"
"net/http"
"os"
"time"

"crapi.proj/goservice/api/config"
"crapi.proj/goservice/api/router"
Expand All @@ -34,6 +37,37 @@ func init() {
}
}

func identityServiceHealthCheck() {
if os.Getenv("IDENTITY_SERVICE") == "" {
time.Sleep(5 * time.Second)
log.Fatal("IDENTITY_SERVICE is not set")
}
var attempts = 0
for (attempts <= 5) {
identityHealthCheckUrl := fmt.Sprintf("http://%s/identity/health_check", os.Getenv("IDENTITY_SERVICE"))
resp, err := http.Get(identityHealthCheckUrl)
if err != nil {
log.Printf("Error while checking the health of identity service: %v", err)
log.Printf("Retrying in 5 seconds...")
time.Sleep(5 * time.Second)
attempts++
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Identity service is not healthy: %v", resp.Status)
log.Printf("Retrying in 5 seconds...")
time.Sleep(5 * time.Second)
attempts++
continue
}
log.Printf("Identity service is healthy")
time.Sleep(1 * time.Second)
return
}
log.Fatal("Identity service is not healthy. Terminating...")
}

func Run() {
var server = config.Server{}
var route = router.Server{}
Expand All @@ -42,6 +76,8 @@ func Run() {

route.DB = server.Initialize("postgres", os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_PORT"), os.Getenv("DB_HOST"), os.Getenv("DB_NAME"))

identityServiceHealthCheck()

seed.LoadMongoData(server.Client, server.DB)

route.Router = mux.NewRouter()
Expand Down

0 comments on commit 3fd4695

Please sign in to comment.