Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDB, programmatically check whether Server instance is running

I configure my document store in the following way:

store = new DocumentStore { Url = serverUrl };
store.Initialize();

I like to know how I can make sure prior or post initialization but before opening a session whether the client is connected to the server. I did not startup the server and I could still initialize the store, not sure why or whether it creates by default an embedded db if it cannot find the server under specified url. Any idea how to check that the connection is established between client and server?

like image 341
Matt Avatar asked Feb 15 '13 08:02

Matt


1 Answers

Initialization does not actually open a connection. The RavenDB client opens and closes connections as it needs to.

It will not revert to an embedded database. You have to explicitly use an EmbeddableDocumentStore if you want an embedded database instance.

If you want to check yourself if the server is up, you can just do something and see if it fails. Probably the easiest thing you could do is to try to get the build number of the RavenDB server. This can be done using documentStore.AsyncDatabaseCommands.GetBuildNumberAsync().

Here are some extension methods that will help make it even easier. Put these in a static class:

public static bool TryGetServerVersion(this IDocumentStore documentStore, out BuildNumber buildNumber, int timeoutMilliseconds = 5000)
{
    try
    {
        var task = documentStore.AsyncDatabaseCommands.GetBuildNumberAsync();
        var success = task.Wait(timeoutMilliseconds);
        buildNumber = task.Result;
        return success;
    }
    catch
    {
        buildNumber = null;
        return false;
    }
}

public static bool IsServerOnline(this IDocumentStore documentStore, int timeoutMilliseconds = 5000)
{
    BuildNumber buildNumber;
    return documentStore.TryGetServerVersion(out buildNumber, timeoutMilliseconds);
}

Then you can use them like this:

var online = documentStore.IsServerOnline();

Or like this:

BuildNumber buildNumber;
var online = documentStore.TryGetServerVersion(out buildNumber);
like image 123
Matt Johnson-Pint Avatar answered Sep 19 '22 12:09

Matt Johnson-Pint