Skip to content

Commit

Permalink
Make sokemotor wait for es instance to start up.
Browse files Browse the repository at this point in the history
Signed-off-by: Daksh Chauhan <dak-x@outlook.com>
  • Loading branch information
dak-x committed Aug 1, 2021
1 parent 6976234 commit c92c02c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
13 changes: 8 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:

depends_on:
- elastic

networks:
- soke-elastic
ports:
Expand All @@ -20,19 +20,22 @@ services:
volumes:
- ./:/app
- $GOPATH:/go/



elastic:
image: elasticsearch:7.13.4

environment:
- discovery.type=single-node

container_name: elastic
hostname: elastic
volumes:
- elastic-data:/usr/share/elasticsearch/data
networks:
- soke-elastic
- soke-elastic


networks:
soke-elastic:
driver: bridge
Expand Down
41 changes: 38 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,53 @@ package main

import (
"log"
"time"

"github.com/elastic/go-elasticsearch/v7"
"github.com/gin-gonic/gin"
)

func main() {
func getEsClint() *elasticsearch.Client {

esCfg := elasticsearch.Config{
Addresses: []string{
"http://elastic:9200/",
},
}

es, err := elasticsearch.NewClient(esCfg)

_, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatal("Elastic Instance Not Running Exiting...")
log.Fatalf("Cannot create Client: %v", err)
}

res, err := es.Info()

var numRetry int = 0

for err != nil {
time.Sleep(3 * time.Second)
numRetry++
if numRetry == 5 {
break
}
res, err = es.Info()
}

if err != nil {
log.Fatalf("Elastic is not running %s", err)
}

log.Printf("ELASTIC IS UP: %v", res)
log.Printf("STARTING SOKEMOTOR SERVICE")

return es
}

func main() {

_ = getEsClint()

app := gin.Default()
app.GET("/search/:query", searchHandler)
app.POST("/document", documentHandler)
Expand Down

0 comments on commit c92c02c

Please sign in to comment.