Skip to content

Commit 35926d7

Browse files
committed
Wait for SQL
1 parent 4b2d89a commit 35926d7

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

tests/Foundatio.Parsers.SqlQueries.Tests/SqlQueryParserTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel.DataAnnotations;
33
using System.Linq;
44
using System.Linq.Dynamic.Core;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using Foundatio.Parsers.LuceneQueries.Nodes;
78
using Foundatio.Parsers.LuceneQueries.Visitors;
@@ -448,6 +449,9 @@ public static string TryGetNationalNumber(string phoneNumber, string regionCode
448449

449450
public IServiceProvider GetServiceProvider()
450451
{
452+
string sqlConnectionString = "Server=localhost;User Id=sa;Password=P@ssword1;Timeout=5;Initial Catalog=foundatio;Encrypt=False";
453+
SqlWaiter.Wait(sqlConnectionString);
454+
451455
var services = new ServiceCollection();
452456
services.AddDbContext<SampleContext>((_, x) =>
453457
{
@@ -536,6 +540,37 @@ ON ftCatalog
536540
return db;
537541
}
538542

543+
private static bool _checked;
544+
private static readonly object _lock = new();
545+
546+
private static void WaitForSql(
547+
string connectionString,
548+
int maxRetries = 90,
549+
int delayMs = 1000)
550+
{
551+
for (int i = 0; i < maxRetries; i++)
552+
{
553+
try
554+
{
555+
using var conn = new SqlConnection(connectionString);
556+
conn.Open();
557+
558+
using var cmd = conn.CreateCommand();
559+
cmd.CommandText = "SELECT 1";
560+
cmd.ExecuteScalar();
561+
562+
return;
563+
}
564+
catch
565+
{
566+
if (i == maxRetries - 1)
567+
throw;
568+
569+
Thread.Sleep(delayMs);
570+
}
571+
}
572+
}
573+
539574
private async Task WaitForFullTextIndexAsync(DbContext db, string catalogName, int timeoutSeconds = 30)
540575
{
541576
var end = DateTime.UtcNow.AddSeconds(timeoutSeconds);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Threading;
3+
using Microsoft.Data.SqlClient;
4+
5+
namespace Foundatio.Parsers.SqlQueries.Tests;
6+
7+
public static class SqlWaiter
8+
{
9+
private static bool _checked;
10+
private static readonly object _lock = new();
11+
12+
public static void Wait(
13+
string connectionString,
14+
TimeSpan? timeout = null,
15+
int delayMs = 1000)
16+
{
17+
if (_checked)
18+
return;
19+
20+
lock (_lock)
21+
{
22+
if (_checked)
23+
return;
24+
25+
timeout ??= TimeSpan.FromSeconds(30);
26+
var end = DateTime.UtcNow + timeout.Value;
27+
28+
string masterCs = BuildMasterConnectionString(connectionString);
29+
30+
while (DateTime.UtcNow < end)
31+
{
32+
try
33+
{
34+
using var conn = new SqlConnection(masterCs);
35+
conn.Open();
36+
37+
using var cmd = conn.CreateCommand();
38+
cmd.CommandText = "SELECT 1";
39+
cmd.ExecuteScalar();
40+
41+
_checked = true;
42+
return;
43+
}
44+
catch
45+
{
46+
Thread.Sleep(delayMs);
47+
}
48+
}
49+
50+
throw new Exception("Failed to connect to SQL Server within timeout.");
51+
}
52+
}
53+
54+
private static string BuildMasterConnectionString(string cs)
55+
{
56+
var builder = new SqlConnectionStringBuilder(cs)
57+
{
58+
InitialCatalog = "master"
59+
};
60+
61+
return builder.ToString();
62+
}
63+
}

0 commit comments

Comments
 (0)