Skip to content

Commit af91ded

Browse files
committed
EnsureDatabaseConnections: accept CLI arg to check only the relevant database
1 parent fb2b437 commit af91ded

2 files changed

Lines changed: 32 additions & 47 deletions

File tree

.github/workflows/dotnet.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ jobs:
2121
compose-service: ''
2222
- test-project: Stores/MariaDB/Cleipnir.ResilientFunctions.MariaDB.Tests
2323
compose-service: maria-db
24+
db-name: mariadb
2425
- test-project: Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL.Tests
2526
compose-service: postgresql-db
27+
db-name: postgres
2628
- test-project: Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer.Tests
2729
compose-service: sql-server-db
30+
db-name: sqlserver
2831
steps:
2932
- uses: actions/checkout@v4
3033
- uses: actions/setup-dotnet@v4
@@ -34,7 +37,7 @@ jobs:
3437
- if: matrix.compose-service != ''
3538
run: docker compose up -d ${{ matrix.compose-service }}
3639
- if: matrix.compose-service != ''
37-
run: dotnet run --project ./Stores/EnsureDatabaseConnections/EnsureDatabaseConnections.csproj --no-build
40+
run: dotnet run --project ./Stores/EnsureDatabaseConnections/EnsureDatabaseConnections.csproj --no-build -- ${{ matrix.db-name }}
3841
- run: dotnet test ./${{ matrix.test-project }} --no-build --logger "console;verbosity=detailed"
3942
- if: always() && matrix.compose-service != ''
4043
run: docker compose down

Stores/EnsureDatabaseConnections/Program.cs

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,42 @@ internal static class Program
88
{
99
public static async Task<int> Main(string[] args)
1010
{
11-
var retry = 1;
12-
while (true)
11+
var databases = args.Length > 0
12+
? args.Select(a => a.ToLowerInvariant()).ToHashSet()
13+
: new HashSet<string> { "mariadb", "postgres", "sqlserver" };
14+
15+
var checks = new List<(string Name, Func<Task> Connect)>();
16+
if (databases.Contains("mariadb"))
17+
checks.Add(("MariaDB", CreateAndOpenMariaDbConnection));
18+
if (databases.Contains("postgres"))
19+
checks.Add(("Postgres", CreateAndOpenPostgresConnection));
20+
if (databases.Contains("sqlserver"))
21+
checks.Add(("SQL Server", CreateAndOpenSqlServerConnection));
22+
23+
foreach (var (name, connect) in checks)
1324
{
14-
await Task.Delay(1_000);
15-
Console.WriteLine($"Trying {retry}/20");
16-
17-
try
18-
{
19-
await CreateAndOpenMariaDbConnection();
20-
}
21-
catch (Exception e)
25+
var retry = 1;
26+
while (true)
2227
{
23-
if (retry == 20)
28+
await Task.Delay(1_000);
29+
Console.WriteLine($"[{name}] Trying {retry}/20");
30+
try
2431
{
25-
Console.WriteLine($"Unable to connect to MariaDB. Exception: {e}");
26-
return -1;
32+
await connect();
33+
break;
2734
}
28-
retry++;
29-
continue;
30-
}
31-
32-
try
33-
{
34-
await CreateAndOpenPostgresConnection();
35-
}
36-
catch (Exception e)
37-
{
38-
if (retry == 20)
35+
catch (Exception e)
3936
{
40-
Console.WriteLine($"Unable to connect to Postgres. Exception: {e}");
41-
return -1;
37+
if (retry == 20)
38+
{
39+
Console.WriteLine($"Unable to connect to {name}. Exception: {e}");
40+
return -1;
41+
}
42+
retry++;
4243
}
43-
retry++;
44-
continue;
4544
}
46-
47-
try
48-
{
49-
await CreateAndOpenSqlServerConnection();
50-
}
51-
catch (Exception e)
52-
{
53-
if (retry == 20)
54-
{
55-
Console.WriteLine($"Unable to connect to SQL Server. Exception: {e}");
56-
return -1;
57-
}
58-
retry++;
59-
continue;
60-
}
61-
62-
break;
6345
}
64-
46+
6547
Console.WriteLine("All connections were established successfully");
6648
return 0;
6749
}

0 commit comments

Comments
 (0)