Skip to content

Commit

Permalink
🐛 Connecting to RavenDB database can timeout with `DatabaseLoadTimeou…
Browse files Browse the repository at this point in the history
…tException`. Will retry until the cancellationtoken is set
  • Loading branch information
ramonsmits committed Jan 8, 2024
1 parent 9762cdd commit ef3e553
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus.Logging;
using Raven.Client.Documents;
using Raven.Client.Exceptions.Database;

class RavenEmbeddedPersistenceLifecycle : IRavenPersistenceLifecycle
{
Expand All @@ -26,7 +28,20 @@ public async Task Start(CancellationToken cancellationToken)
{
database = EmbeddedDatabase.Start(databaseConfiguration);

documentStore = await database.Connect(cancellationToken);
while (true)
{
cancellationToken.ThrowIfCancellationRequested();

try
{
documentStore = await database.Connect(cancellationToken);
}
catch (DatabaseLoadTimeoutException e)
{
Log.Warn("Could not connect to database. Retrying in 500ms...", e);
await Task.Delay(500, cancellationToken);
}
}
}

public Task Stop(CancellationToken cancellationToken)
Expand All @@ -39,7 +54,7 @@ public Task Stop(CancellationToken cancellationToken)

IDocumentStore documentStore;
EmbeddedDatabase database;

static readonly ILog Log = LogManager.GetLogger(typeof(RavenEmbeddedPersistenceLifecycle));
readonly DatabaseConfiguration databaseConfiguration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus.Logging;
using NUnit.Framework;
using ServiceControl.Audit.Persistence.RavenDB;
using TestHelper;
Expand Down Expand Up @@ -40,8 +41,9 @@ public static async Task<EmbeddedDatabase> GetInstance(CancellationToken cancell

return embeddedDatabase;
}
catch (Exception)
catch (Exception e)
{
Log.Warn("Could not connect to database. Retrying in 500ms...", e);
await Task.Delay(500, cancellationToken);
}
}
Expand All @@ -68,5 +70,6 @@ public static async Task Stop(CancellationToken cancellationToken = default)

static EmbeddedDatabase embeddedDatabase;
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
static readonly ILog Log = LogManager.GetLogger(typeof(SharedEmbeddedServer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus.Logging;
using Raven.Client.Documents;
using Raven.Client.Exceptions.Database;
using ServiceControl.Persistence;

class RavenEmbeddedPersistenceLifecycle : IPersistenceLifecycle, IDisposable
Expand All @@ -27,7 +29,21 @@ public IDocumentStore GetDocumentStore()
public async Task Initialize(CancellationToken cancellationToken)
{
database = EmbeddedDatabase.Start(databaseConfiguration);
documentStore = await database.Connect(cancellationToken);

while (true)
{
cancellationToken.ThrowIfCancellationRequested();

try
{
documentStore = await database.Connect(cancellationToken);
}
catch (DatabaseLoadTimeoutException e)
{
Log.Warn("Could not connect to database. Retrying in 500ms...", e);
await Task.Delay(500, cancellationToken);
}
}
}

public void Dispose()
Expand All @@ -41,6 +57,7 @@ public void Dispose()
EmbeddedDatabase database;

readonly RavenPersisterSettings databaseConfiguration;
static readonly ILog Log = LogManager.GetLogger(typeof(RavenEmbeddedPersistenceLifecycle));

~RavenEmbeddedPersistenceLifecycle()
{
Expand Down

0 comments on commit ef3e553

Please sign in to comment.