Wait until Elasticsearch become available.
When testing Elasticsearch in an automated way you have to wait for it to be up and ready to accept connections.
go get github.com/blacktop/wait-for-es
import (
"context"
"log"
"time"
w4e "github.com/blacktop/wait-for-es"
"github.com/pkg/errors"
)
func main() {
connCtx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
w := w4e.WaitForEs{
URL: address,
Timeout: timeout,
}
err := w.WaitForConnection(connCtx, timeout)
if err != nil {
log.Fatal(errors.Wrap(err, "failed to connect to elasticsearch"))
}
}
Download wait-for-es from releases
Via homebrew
$ brew install blacktop/tap/wait-for-es
$ wait-for-es --help
Wait until Elasticsearch become available
Usage:
wait-for-es [flags]
Flags:
--address string elasticsearch address (default "http://localhost:9200")
--config string config file (default is $HOME/.wait-for-es.yaml)
-H, --healthy wait until cluster health is green
-h, --help help for wait-for-es
--timeout int timeout in seconds (default 60)
-V, --verbose verbose output
$ wait-for-es --address http://localhost:9200 --timeout 60 --healthy --verbose
INFO[0000] ===> trying to connect to elasticsearch
DEBU[0000] attempting to PING to: http://localhost:9200
DEBU[0000] * could not connect to elasticsearch (sleeping for 1 second)
DEBU[0001] attempting to PING to: http://localhost:9200
DEBU[0001] * could not connect to elasticsearch (sleeping for 1 second)
..SNIP...
DEBU[0021] cluster status is "green"
DEBU[0021] elasticSearch connection successful cluster=elasticsearch code=200 url="http://localhost:9200" version=7.0.0
INFO[0021] elasticsearch came online after 21 seconds
MIT Copyright (c) 2019 blacktop