/// <summary> /// Create a new MySql Server database /// </summary> private static async Task CreateMariaDBDatabaseAsync(string dbName, bool recreateDb = true) { var onRetry = new Func <Exception, int, TimeSpan, object, Task>((ex, cpt, ts, arg) => { Console.WriteLine($"Creating MariaDB database failed when connecting to information_schema ({ex.Message}). Wating {ts.Milliseconds}. Try number {cpt}"); return(Task.CompletedTask); }); SyncPolicy policy = SyncPolicy.WaitAndRetry(3, TimeSpan.FromMilliseconds(500), null, onRetry); await policy.ExecuteAsync(async() => { using var sysConnection = new MySqlConnection(Setup.GetMariaDBDatabaseConnectionString("information_schema")); sysConnection.Open(); if (recreateDb) { using var cmdDrop = new MySqlCommand($"Drop schema if exists {dbName};", sysConnection); await cmdDrop.ExecuteNonQueryAsync(); } using (var cmdDb = new MySqlCommand($"create schema {dbName};", sysConnection)) cmdDb.ExecuteNonQuery(); sysConnection.Close(); }); }
/// <summary> /// Ensure we have policy. Create a new one, if not provided /// </summary> private SyncPolicy EnsurePolicy(SyncPolicy policy) { if (policy != default) { return(policy); } // Defining my retry policy policy = SyncPolicy.WaitAndRetry(2, (retryNumber) => { return(TimeSpan.FromMilliseconds(500 * retryNumber)); }, (ex, arg) => { var webEx = ex as SyncException; // handle session lost return(webEx == null || webEx.TypeName != nameof(HttpSessionLostException)); }, async(ex, cpt, ts, arg) => { await this.InterceptAsync(new HttpSyncPolicyArgs(10, cpt, ts), default).ConfigureAwait(false); }); return(policy); }
/// <summary> /// Create a new Sql Server database /// </summary> public static async Task CreateSqlServerDatabaseAsync(string dbName, bool recreateDb = true) { var onRetry = new Func <Exception, int, TimeSpan, object, Task>((ex, cpt, ts, arg) => { Console.WriteLine($"Creating SQL Server database failed when connecting to master ({ex.Message}). Wating {ts.Milliseconds}. Try number {cpt}"); return(Task.CompletedTask); }); SyncPolicy policy = SyncPolicy.WaitAndRetry(3, TimeSpan.FromMilliseconds(500), null, onRetry); await policy.ExecuteAsync(async() => { using var masterConnection = new SqlConnection(Setup.GetSqlDatabaseConnectionString("master")); masterConnection.Open(); using (var cmdDb = new SqlCommand(GetSqlCreationScript(dbName, recreateDb), masterConnection)) await cmdDb.ExecuteNonQueryAsync(); masterConnection.Close(); }); }