Skip to content

Commit 6f105d5

Browse files
authored
🐛 Connecting to RavenDB database can timeout with DatabaseLoadTimeoutException. Will retry until the cancellationtoken is set (#3918)
1 parent 3e3ecab commit 6f105d5

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/ServiceControl.Audit.Persistence.RavenDb5/RavenDbEmbeddedPersistenceLifecycle.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using NServiceBus.Logging;
67
using Raven.Client.Documents;
8+
using Raven.Client.Exceptions.Database;
79

810
class RavenDbEmbeddedPersistenceLifecycle : IRavenDbPersistenceLifecycle
911
{
@@ -26,7 +28,21 @@ public async Task Start(CancellationToken cancellationToken)
2628
{
2729
database = EmbeddedDatabase.Start(databaseConfiguration);
2830

29-
documentStore = await database.Connect(cancellationToken).ConfigureAwait(false);
31+
while (true)
32+
{
33+
cancellationToken.ThrowIfCancellationRequested();
34+
35+
try
36+
{
37+
documentStore = await database.Connect(cancellationToken).ConfigureAwait(false);
38+
return;
39+
}
40+
catch (DatabaseLoadTimeoutException e)
41+
{
42+
Log.Warn("Could not connect to database. Retrying in 500ms...", e);
43+
await Task.Delay(500, cancellationToken).ConfigureAwait(false);
44+
}
45+
}
3046
}
3147

3248
public Task Stop(CancellationToken cancellationToken)
@@ -39,7 +55,7 @@ public Task Stop(CancellationToken cancellationToken)
3955

4056
IDocumentStore documentStore;
4157
EmbeddedDatabase database;
42-
58+
static readonly ILog Log = LogManager.GetLogger(typeof(RavenDbEmbeddedPersistenceLifecycle));
4359
readonly DatabaseConfiguration databaseConfiguration;
4460
}
45-
}
61+
}

src/ServiceControl.Audit.Persistence.Tests.RavenDb5/SharedEmbeddedServer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Net.NetworkInformation;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using NServiceBus.Logging;
910
using NUnit.Framework;
1011
using ServiceControl.Audit.Persistence.RavenDb;
1112

@@ -41,8 +42,9 @@ public static async Task<EmbeddedDatabase> GetInstance(CancellationToken cancell
4142

4243
return embeddedDatabase;
4344
}
44-
catch (Exception)
45+
catch (Exception e)
4546
{
47+
Log.Warn("Could not connect to database. Retrying in 500ms...", e);
4648
await Task.Delay(500, cancellationToken).ConfigureAwait(false);
4749
}
4850
}
@@ -87,5 +89,6 @@ static int FindAvailablePort(int startPort)
8789

8890
static EmbeddedDatabase embeddedDatabase;
8991
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
92+
static readonly ILog Log = LogManager.GetLogger(typeof(SharedEmbeddedServer));
9093
}
9194
}

0 commit comments

Comments
 (0)