# Avoiding ElasticSearch error 503 Server Unavailable: Use of WaitForStatus

**URL:** <https://discuss.elastic.co/t/avoiding-elasticsearch-error-503-server-unavailable-use-of-waitforstatus/216288>\
**Category:** Elasticsearch\
**Created:** [January 23, 2020, 3:55pm UTC](https://discuss.elastic.co/t/avoiding-elasticsearch-error-503-server-unavailable-use-of-waitforstatus/216288 "2020-01-23T15:55:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CesarPozo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cesarpozo/32/60608_2.png) [@CesarPozo](https://discuss.elastic.co/u/CesarPozo)\
**Post date:** [January 23, 2020, 3:55pm UTC](https://discuss.elastic.co/t/avoiding-elasticsearch-error-503-server-unavailable-use-of-waitforstatus/216288/1 "2020-01-23T15:55:10Z")

</div>

When I start my program, I run the ElasticSearch Service and **check if an Index exists and if there is any documents** , let's say I just run the ES service and I have these two functions:

```auto
public ElasticClient getElasticSearchClient()
{
    ConnectionSettings connectionSettings = new Nest.ConnectionSettings(new Uri("http://localhost:9200"))
                                                    .DefaultIndex("myindex")
                                                    .DisableDirectStreaming();
    ElasticClient client = new ElasticClient(connectionSettings);
    //var health = client.Cluster.Health("myindex", a => (a.WaitForStatus(WaitForStatus.Yellow)).Timeout(50));
    return client;
}

public void checkElasticsearchIndex()
{
    var client = getElasticSearchClient();

    var health = this.client.Cluster.Health("myindex", a => (a.WaitForStatus(WaitForStatus.Yellow)));

    CountResponse count = client.Count<myobject>();

    if (!client.Indices.Exists("myindex").IsValid || count.Count == 0)
    {
        BulkWriteAllToIndexES(client);
    }
}

```

Inside the checkElasticsearchIndex function,

1. The count operation fails with the following error message: _OriginalException: Elasticsearch.Net.ElasticsearchClientException: The remote server returned an error: (503) Server Unavailable.. Call: Status code 503 from: GET /myindex/\_count. ServerError: Type: search\_phase\_execution\_exception Reason: "all shards failed" ---\> System.Net.WebException: The remote server returned an error: (503) Server Unavailable._
2. The Health fails as well: _OriginalException: Elasticsearch.Net.ElasticsearchClientException: Unable to connect to the remote server. Call: Status code unknown from: GET /\_cluster/health/myindex?wait\_for\_status=yellow ---\> System.Net.WebException: Unable to connect to the remote server ---\> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:9200_

As you can see, I have tried the Cluster WaitForStatus, but it didn't work.

**My question: is there any way to wait until client/cluster/nodes are ready and not get any exception?**

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [January 24, 2020, 4:50am UTC](https://discuss.elastic.co/t/avoiding-elasticsearch-error-503-server-unavailable-use-of-waitforstatus/216288/2 "2020-01-24T04:50:42Z")

</div>

It sounds like you're starting the Elasticsearch process at the same time as starting your program, but Elasticsearch takes longer than your program to be ready.

If that's the case, you may be interested in using [the same abstractions](https://github.com/elastic/elasticsearch-net-abstractions) that the .NET client uses for integration tests against Elasticsearch. The abstractions read output from the Elasticsearch process to know when it is ready, and block until this happens. There [available on an AppVeyor CI package feed](https://ci.appveyor.com/nuget/elasticsearch-net-abstractions) (with plans to release them to Nuget in the future).

There are [some examples of how to spin up a cluster with the abstractions](https://github.com/elastic/elasticsearch-net-abstractions/blob/edfc2f4ada98a6b1d28f017ebc2ff59ae61aa142/src/Examples/Elastic.Managed.Example/Program.cs). For single node, it would be something like

```
using System;
using Elastic.Managed.Configuration;
using Elastic.Managed.ConsoleWriters;
using Elastic.Managed.FileSystem;

namespace Elastic.Managed.Example
{
	class Program
	{
		static void Main(string[] args)
		{
			var version = "7.5.1";
			var esHome = Environment.ExpandEnvironmentVariables($@"%LOCALAPPDATA%\ElasticManaged\{version}\elasticsearch-{version}");

			using (var node = new ElasticsearchNode(version, esHome))
			{
				node.SubscribeLines(new LineHighlightWriter());
				if (!node.WaitForStarted(TimeSpan.FromMinutes(2))) throw new Exception();

                // do your work here
			}
		}
	}
}

```

This assumes that Elasticsearch 7.5.1 zip has been downloaded already, and exists at `%LOCALAPPDATA%\ElasticManaged\7.5.1\elasticsearch-7.5.1`. There are more complex [examples of how to integrate this into tests with xUnit.](https://github.com/elastic/elasticsearch-net-abstractions/tree/edfc2f4ada98a6b1d28f017ebc2ff59ae61aa142/src/Examples)

You can [use the `EphemeralCluster` components](https://github.com/elastic/elasticsearch-net-abstractions/blob/master/src/Elastic.Managed.Ephemeral/README.md) to download, configure and run Elasticsearch

```
var plugins = new ElasticsearchPlugins(ElasticsearchPlugin.RepositoryAzure, ElasticsearchPlugin.IngestAttachment);
var config = new EphemeralClusterConfiguration("7.5.1", ClusterFeatures.XPack, plugins, numberOfNodes: 1);
using (var cluster = new EphemeralCluster(config))
{
	cluster.Start();

	var nodes = cluster.NodesUris();
	var connectionPool = new StaticConnectionPool(nodes);
	var settings = new ConnectionSettings(connectionPool).EnableDebugMode();
	var client = new ElasticClient(settings);
				
	Console.Write(client.CatPlugins().DebugInformation);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 21, 2020, 4:50am UTC](https://discuss.elastic.co/t/avoiding-elasticsearch-error-503-server-unavailable-use-of-waitforstatus/216288/3 "2020-02-21T04:50:56Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
