Skip to content

Commit

Permalink
Adds retry in the apply tool command (#25)
Browse files Browse the repository at this point in the history
* Adds retry

* Fixes message

* feedback
  • Loading branch information
rbans96 authored May 21, 2020
1 parent b21ac31 commit 7e566f6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
41 changes: 27 additions & 14 deletions tools/SchemaManager/Commands/ApplyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Common;
using Polly;
using SchemaManager.Exceptions;
using SchemaManager.Model;
using SchemaManager.Utils;
Expand All @@ -19,7 +20,8 @@ namespace SchemaManager.Commands
{
public static class ApplyCommand
{
private static readonly TimeSpan MaxWaitTime = TimeSpan.FromMinutes(1);
private static readonly TimeSpan RetrySleepDuration = TimeSpan.FromSeconds(20);
private const int RetryAttempts = 3;

public static async Task HandlerAsync(string connectionString, Uri server, MutuallyExclusiveType exclusiveType, bool force)
{
Expand Down Expand Up @@ -61,13 +63,19 @@ public static async Task HandlerAsync(string connectionString, Uri server, Mutua
throw new SchemaManagerException(string.Format(Resources.SpecifiedVersionNotAvailable, targetVersion));
}

// to ensure server side polling is completed
Console.WriteLine(Resources.WaitMessage);
await Task.Delay(MaxWaitTime);

if (!force)
{
await ValidateCompatibleVersion(schemaClient, availableVersions.First().Id, availableVersions.Last().Id);
int attemptCount = 1;

await Policy.Handle<SchemaManagerException>()
.WaitAndRetryAsync(
retryCount: RetryAttempts,
sleepDurationProvider: (retryCount) => RetrySleepDuration,
onRetry: (exception, retryCount) =>
{
Console.WriteLine(string.Format(Resources.RetryVersionCompatibility), attemptCount++, RetryAttempts);
})
.ExecuteAsync(() => ValidateCompatibleVersion(schemaClient, availableVersions.First().Id, availableVersions.Last().Id));
}
else if (availableVersions.First().Id == 1)
{
Expand All @@ -81,19 +89,22 @@ public static async Task HandlerAsync(string connectionString, Uri server, Mutua
int executingVersion = availableVersion.Id;
if (!force)
{
await ValidateInstancesVersion(schemaClient, executingVersion);
int attemptCount = 1;

await Policy.Handle<SchemaManagerException>()
.WaitAndRetryAsync(
retryCount: RetryAttempts,
sleepDurationProvider: (retryCount) => RetrySleepDuration,
onRetry: (exception, retryCount) =>
{
Console.WriteLine(string.Format(Resources.RetryCurrentVersions, attemptCount++, RetryAttempts));
})
.ExecuteAsync(() => ValidateInstancesVersion(schemaClient, executingVersion));
}

string script = await GetScript(schemaClient, executingVersion, availableVersion.Script, availableVersion.Diff);

UpgradeSchema(connectionString, executingVersion, script);

// to ensure server side polling is completed after each version migration
if (executingVersion != availableVersions.Last().Id)
{
Console.WriteLine(Resources.WaitMessage);
await Task.Delay(MaxWaitTime);
}
}
}
catch (SchemaManagerException ex)
Expand All @@ -118,6 +129,8 @@ private static void UpgradeSchema(string connectionString, int version, string s
// check if the record for given version exists in failed status
SchemaDataStore.DeleteSchemaVersion(connectionString, version, SchemaDataStore.Failed);

Console.WriteLine(string.Format(Resources.SchemaMigrationStartedMessage, version));

SchemaDataStore.ExecuteScriptAndCompleteSchemaVersion(connectionString, script, version);

Console.WriteLine(string.Format(Resources.SchemaMigrationSuccessMessage, version));
Expand Down
29 changes: 28 additions & 1 deletion tools/SchemaManager/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion tools/SchemaManager/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,20 @@
<value>Unable to connect to host "{0}".</value>
<comment>{0} is the server url.</comment>
</data>
<data name="RetryCurrentVersions" xml:space="preserve">
<value>Attempt {0} of {1} to verify if all the instances are running the previous version.</value>
<comment>(0) is retry attempt, {1} is total retries</comment>
</data>
<data name="RetryVersionCompatibility" xml:space="preserve">
<value>Attempt {0} of {1} to verify if the schema version is compatible.</value>
<comment>(0) is retry attempt, {1} is total retries</comment>
</data>
<data name="SchemaMigrationStartedMessage" xml:space="preserve">
<value>Schema migration is started for the version : {0}.</value>
<comment>{0} is a version.</comment>
</data>
<data name="SchemaMigrationSuccessMessage" xml:space="preserve">
<value>Schema Migration is completed successfully for the version {0}.</value>
<value>Schema migration is completed successfully for the version : {0}.</value>
<comment>{0} is a version.</comment>
</data>
<data name="ScriptNotFound" xml:space="preserve">
Expand Down
1 change: 1 addition & 0 deletions tools/SchemaManager/SchemaManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="Ensure.That" Version="8.1.2" />
<PackageReference Include="Microsoft.SqlServer.SqlManagementObjects" Version="150.18208.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Polly" Version="7.2.1" />
<PackageReference Include="System.CommandLine.Experimental" Version="0.2.0-alpha.19174.3" />
<PackageReference Include="System.CommandLine.Rendering" Version="0.2.0-alpha.19174.3" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
Expand Down

0 comments on commit 7e566f6

Please sign in to comment.