@@ -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