Skip to content

Commit

Permalink
Add retries to TrsDataSyncService and DqtReportingService (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Jan 29, 2024
1 parent 16ec56b commit ff53e06
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion TeachingRecordSystem/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<PackageVersion Include="NSwag.Examples" Version="1.0.11" />
<PackageVersion Include="Optional" Version="4.0.0" />
<PackageVersion Include="PdfSharpCore" Version="1.3.62" />
<PackageVersion Include="Polly.Core" Version="8.2.1" />
<PackageVersion Include="RedisRateLimiting.AspNetCore" Version="1.1.0" />
<PackageVersion Include="Respawn" Version="6.1.0" />
<PackageVersion Include="Scrutor" Version="4.2.2" />
Expand All @@ -86,4 +87,4 @@
<PackageVersion Include="Xunit.DependencyInjection" Version="8.9.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.5" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using Polly;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Dqt.Queries;
using TeachingRecordSystem.Core.Services.CrmEntityChanges;
Expand All @@ -27,6 +28,15 @@ public partial class DqtReportingService : BackgroundService
private const int MaxUpsertBatchSize = 100;
private const int MaxEntityTypesToProcessConcurrently = 10;

private static readonly ResiliencePipeline _resiliencePipeline = new ResiliencePipelineBuilder()
.AddRetry(new Polly.Retry.RetryStrategyOptions()
{
BackoffType = DelayBackoffType.Exponential,
Delay = TimeSpan.FromSeconds(30),
MaxRetryAttempts = 3
})
.Build();

private readonly DqtReportingOptions _options;
private readonly ICrmEntityChangesService _crmEntityChangesService;
private readonly ICrmQueryDispatcher _crmQueryDispatcher;
Expand Down Expand Up @@ -61,7 +71,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await ProcessChanges(stoppingToken);
await _resiliencePipeline.ExecuteAsync(async ct => await ProcessChanges(ct), stoppingToken);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Options;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Polly;
using TeachingRecordSystem.Core.Services.CrmEntityChanges;

namespace TeachingRecordSystem.Core.Services.TrsDataSync;
Expand All @@ -17,6 +18,15 @@ public class TrsDataSyncService(
public const string CrmClientName = "TrsDataSync";
private const string ChangesKeyPrefix = "TrsDataSync";

private static readonly ResiliencePipeline _resiliencePipeline = new ResiliencePipelineBuilder()
.AddRetry(new Polly.Retry.RetryStrategyOptions()
{
BackoffType = DelayBackoffType.Exponential,
Delay = TimeSpan.FromSeconds(30),
MaxRetryAttempts = 3
})
.Build();

private const int PageSize = 1000;

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand All @@ -27,7 +37,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await ProcessChanges(stoppingToken);
await _resiliencePipeline.ExecuteAsync(async ct => await ProcessChanges(ct), stoppingToken);
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Optional" />
<PackageReference Include="PdfSharpCore" />
<PackageReference Include="Polly.Core" />
<PackageReference Include="Scrutor" />
<PackageReference Include="System.Net.Http.Json" />
<PackageReference Include="System.Reactive" />
Expand Down

0 comments on commit ff53e06

Please sign in to comment.