From 98270414a0433b2c3ef2bed926d7c2d57e2f6598 Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Wed, 11 Sep 2024 16:30:29 -0500 Subject: [PATCH] use common retry policy builder function --- .../BaseOrchestrator.ApplyChanges.cs | 9 +------ .../LocalOrchestrator.ApplyChanges.cs | 9 +------ .../RemoteOrchestrator.ApplyChanges.cs | 9 +------ .../Orchestrators/BaseOrchestrator.cs | 26 +++++++++++++++++++ 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/BaseOrchestrator.ApplyChanges.cs b/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/BaseOrchestrator.ApplyChanges.cs index 235cbe9a..d524f028 100644 --- a/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/BaseOrchestrator.ApplyChanges.cs +++ b/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/BaseOrchestrator.ApplyChanges.cs @@ -242,15 +242,8 @@ internal virtual async Task InternalApplyTableChangesAsync(ScopeInfo this.Logger.LogInformation("[InternalApplyTableChangesAsync]. Directory name {DirectoryName}. BatchParts count {BatchPartsInfoCount}", message.Changes.DirectoryName, message.Changes.BatchPartsInfo.Count); - // If we have a transient error happening, and we are rerunning the tranaction, - // raising an interceptor - var onRetry = new Func((ex, cpt, ts, arg) => - this.InterceptAsync(new TransientErrorOccuredArgs(context, connection, ex, cpt, ts), progress, cancellationToken).AsTask()); - // Defining my retry policy - var retryPolicy = this.Options.TransactionMode != TransactionMode.AllOrNothing - ? SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(500 * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry) - : SyncPolicy.WaitAndRetry(0, TimeSpan.Zero); + var retryPolicy = this.BuildSyncPolicy(context, connection, progress, cancellationToken); var applyChangesPolicyResult = await retryPolicy.ExecuteAsync( async () => diff --git a/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/LocalOrchestrator.ApplyChanges.cs b/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/LocalOrchestrator.ApplyChanges.cs index df76e38e..652e6d4b 100644 --- a/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/LocalOrchestrator.ApplyChanges.cs +++ b/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/LocalOrchestrator.ApplyChanges.cs @@ -25,15 +25,8 @@ public partial class LocalOrchestrator : BaseOrchestrator DbConnection connection = default, DbTransaction transaction = default, IProgress progress = null, CancellationToken cancellationToken = default) { - // If we have a transient error happening, and we are rerunning the tranaction, - // raising an interceptor - var onRetry = new Func((ex, cpt, ts, arg) => - this.InterceptAsync(new TransientErrorOccuredArgs(context, connection, ex, cpt, ts), progress, cancellationToken).AsTask()); - // Defining my retry policy - SyncPolicy retryPolicy = this.Options.TransactionMode == TransactionMode.AllOrNothing - ? retryPolicy = SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(500 * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry) - : retryPolicy = SyncPolicy.WaitAndRetry(0, TimeSpan.Zero); + var retryPolicy = this.BuildSyncPolicy(context, connection, progress, cancellationToken); // Execute my OpenAsync in my policy context var applyChangesResult = await retryPolicy.ExecuteAsync<(SyncContext Context, ClientSyncChanges ClientSyncChange, ScopeInfoClient CScopeInfoClient)>( diff --git a/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/RemoteOrchestrator.ApplyChanges.cs b/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/RemoteOrchestrator.ApplyChanges.cs index e2e5dd9c..1842847e 100644 --- a/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/RemoteOrchestrator.ApplyChanges.cs +++ b/Projects/Dotmim.Sync.Core/Orchestrators/ApplyChanges/RemoteOrchestrator.ApplyChanges.cs @@ -52,15 +52,8 @@ public partial class RemoteOrchestrator : BaseOrchestrator // Previous sync errors BatchInfo lastSyncErrorsBatchInfo = null; - // If we have a transient error happening, and we are rerunning the tranaction, - // raising an interceptor - var onRetry = new Func((ex, cpt, ts, arg) => - this.InterceptAsync(new TransientErrorOccuredArgs(context, connection, ex, cpt, ts), progress, cancellationToken).AsTask()); - // Defining my retry policy - SyncPolicy retryPolicy = this.Options.TransactionMode == TransactionMode.AllOrNothing - ? retryPolicy = SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(500 * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry) - : retryPolicy = SyncPolicy.WaitAndRetry(0, TimeSpan.Zero); + var retryPolicy = this.BuildSyncPolicy(context, connection, progress, cancellationToken); await retryPolicy.ExecuteAsync( async () => diff --git a/Projects/Dotmim.Sync.Core/Orchestrators/BaseOrchestrator.cs b/Projects/Dotmim.Sync.Core/Orchestrators/BaseOrchestrator.cs index a0248122..f018196c 100644 --- a/Projects/Dotmim.Sync.Core/Orchestrators/BaseOrchestrator.cs +++ b/Projects/Dotmim.Sync.Core/Orchestrators/BaseOrchestrator.cs @@ -445,5 +445,31 @@ internal SyncException GetSyncError(SyncContext context, Exception exception, st throw this.GetSyncError(context, ex); } } + + /// + /// Builds a synchronization policy based on the TransactionMode of this orchestrator. + /// + /// The synchronization context. + /// The database connection. + /// The progress reporter. + /// The cancellation token. + /// The interval in milliseconds between retry attempts. Default is 500 milliseconds. + /// A configured according to the transaction mode. + /// Thrown when an unsupported transaction mode is encountered. + protected SyncPolicy BuildSyncPolicy(SyncContext context, DbConnection connection, IProgress progress, CancellationToken cancellationToken, int retryIntervalMilliseconds = 500) + { + // If we have a transient error happening, and we are rerunning the transaction, + // raising an interceptor + var onRetry = new Func((ex, cpt, ts, arg) => + this.InterceptAsync(new TransientErrorOccuredArgs(context, connection, ex, cpt, ts), progress, cancellationToken).AsTask()); + + return this.Options.TransactionMode switch + { + TransactionMode.AllOrNothing => SyncPolicy.WaitAndRetry(0, TimeSpan.Zero), + TransactionMode.PerBatch => SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(retryIntervalMilliseconds * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry), + TransactionMode.None => SyncPolicy.WaitAndRetryForever(retryAttempt => TimeSpan.FromMilliseconds(retryIntervalMilliseconds * retryAttempt), (ex, arg) => this.Provider.ShouldRetryOn(ex), onRetry), + _ => throw new NotImplementedException(), + }; + } } } \ No newline at end of file