RavenDB, programmatically check whether Server instance is running
Asked Answered
C

1

6

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?

Crenel answered 15/2, 2013 at 8:4 Comment(0)
B
8

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);
Burl answered 15/2, 2013 at 18:46 Comment(6)
This helps, will try and report back. Thank you.Crenel
It appears that my AsyncDatabaseCOmmands object is NULL? Are you doing any other initialisation on the document store before you get to this point??Demob
We tried this and found that it began failing after the most recent RavenDB client upgrade. This discussion on the Google group came to the conclusion that you should use a synchronous call.Bus
The problem is that GetBuildNumber only exists in the async commands. I will raise an issue to request it for sync also. But then you'd have to handle a timeout exception.Burl
@MattJohnson, seems the GetBuildNumber is missing in the AsyncDatabaseCommands in the new version (I just updated to Client.Lightweight 3.0.0.0). Is there a better way to check whether the store is connected?Crenel
@MattWolf AsyncDatabaseCommands.GlobalAdmin.GetBuildNumberAsync()Praenomen

© 2022 - 2024 — McMap. All rights reserved.