Skip to content

Commit

Permalink
feat: add cachevalidator wether to update position
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD committed Feb 2, 2024
1 parent 0f20961 commit 7aa7022
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Be.Vlaanderen.Basisregisters.ProjectionHandling.LastChangedList
{
using System.Threading;
using System.Threading.Tasks;

public interface ICacheValidator
{
Task<bool> CanCache(long position, CancellationToken ct);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace Be.Vlaanderen.Basisregisters.ProjectionHandling.LastChangedList
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Connector;
using Microsoft.EntityFrameworkCore;
using Model;
using Polly;

public abstract class LastChangedListConnectedProjection : ConnectedProjection<LastChangedListContext>
{
Expand All @@ -14,15 +16,29 @@ public abstract class LastChangedListConnectedProjection : ConnectedProjection<L

private readonly AcceptType[] _supportedAcceptTypes;
private readonly int _commandTimeoutInSeconds;
private readonly ICacheValidator? _cacheValidator = null;
private readonly int _cacheCheckIntervalInSeconds;

protected LastChangedListConnectedProjection(AcceptType[] supportedAcceptTypes) : this(supportedAcceptTypes, 300) {}
protected LastChangedListConnectedProjection(AcceptType[] supportedAcceptTypes)
: this(supportedAcceptTypes, 300) {}

protected LastChangedListConnectedProjection(AcceptType[] supportedAcceptTypes, int commandTimeoutInSeconds)
{
_supportedAcceptTypes = supportedAcceptTypes;
_commandTimeoutInSeconds = commandTimeoutInSeconds;
}

protected LastChangedListConnectedProjection(AcceptType[] supportedAcceptTypes, ICacheValidator cacheValidator)
: this(supportedAcceptTypes, 300, cacheValidator, 5) {}

protected LastChangedListConnectedProjection(AcceptType[] supportedAcceptTypes, int commandTimeoutInSeconds, ICacheValidator cacheValidator, int cacheCheckIntervalInSeconds)
{
_supportedAcceptTypes = supportedAcceptTypes;
_commandTimeoutInSeconds = commandTimeoutInSeconds;
_cacheValidator = cacheValidator;
_cacheCheckIntervalInSeconds = cacheCheckIntervalInSeconds;
}

protected async Task<IEnumerable<LastChangedRecord>> GetLastChangedRecords(
string identifier,
LastChangedListContext context,
Expand Down Expand Up @@ -54,6 +70,8 @@ protected async Task<IEnumerable<LastChangedRecord>> GetLastChangedRecordsAndUpd
LastChangedListContext context,
CancellationToken cancellationToken)
{
await WaitTillCanCache(position, cancellationToken);

context.Database.SetCommandTimeout(_commandTimeoutInSeconds);
var attachedRecords = new List<LastChangedRecord>();

Expand Down Expand Up @@ -87,6 +105,19 @@ protected async Task<IEnumerable<LastChangedRecord>> GetLastChangedRecordsAndUpd
return attachedRecords;
}

private async Task WaitTillCanCache(long position, CancellationToken ct)
{
if (_cacheValidator is null)
{
return;
}

await Policy
.HandleResult<bool>(isValid => !isValid)
.WaitAndRetryForeverAsync(i => TimeSpan.FromSeconds(_cacheCheckIntervalInSeconds))
.ExecuteAsync(async () => await _cacheValidator.CanCache(position, ct));
}

private static string GetApplicationType(AcceptType acceptType)
{
return acceptType switch
Expand Down

0 comments on commit 7aa7022

Please sign in to comment.