Skip to content

Commit

Permalink
feat: Auto-recover projections when using new projection store
Browse files Browse the repository at this point in the history
  • Loading branch information
leksyCode committed Jul 12, 2022
1 parent aeb9f7c commit 37725d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Replay(string hash, IProjectionVersioningPolicy policy)
}
}

public void Rebuild(string hash, IProjectionVersioningPolicy policy)
public void Rebuild(string hash)
{
if (CanRebuild(hash))
{
Expand All @@ -74,14 +74,18 @@ public void VersionRequestTimedout(ProjectionVersion version, VersionRequestTime
}
}

public void NotifyHash(string hash, IProjectionVersioningPolicy policy)
public void NotifyHash(ProjectionVersionManagerId id, string hash, IProjectionVersioningPolicy policy, InMemoryProjectionVersionStore projectionStore)
{
EnsureThereIsNoOutdatedBuildingVersions();

if (ShouldReplay(hash))
{
Replay(hash, policy);
}
else if (ShouldRebuild(id.Id, projectionStore))
{
Rebuild(hash);
}
}

public void FinalizeVersionRequest(ProjectionVersion version)
Expand All @@ -99,8 +103,16 @@ public void FinalizeVersionRequest(ProjectionVersion version)
private bool ShouldReplay(string hash)
{
bool isNewHashTheLiveOne = state.Versions.IsHashTheLiveOne(hash);
bool isInProgress = state.Versions.HasBuildingVersion();

return isInProgress == false && (state.Versions.HasLiveVersion == false || isNewHashTheLiveOne == false);
}

private bool ShouldRebuild(string projectionName, InMemoryProjectionVersionStore projectionStore)
{
ProjectionVersions versions = projectionStore.Get(projectionName);

return state.Versions.HasLiveVersion == false || isNewHashTheLiveOne == false;
return versions.HasLiveVersion == false && versions.HasRebuildingVersion() == false;
}

private bool CanReplay(string hash, IProjectionVersioningPolicy policy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public class ProjectionVersionManagerAppService : ApplicationService<ProjectionV
ICommandHandler<RebuildProjectionCommand>
{
private readonly IProjectionVersioningPolicy projectionVersioningPolicy;
private readonly InMemoryProjectionVersionStore projectionStore;

public ProjectionVersionManagerAppService(IAggregateRepository repository, IProjectionVersioningPolicy projectionVersioningPolicy) : base(repository)
public ProjectionVersionManagerAppService(IAggregateRepository repository, IProjectionVersioningPolicy projectionVersioningPolicy, InMemoryProjectionVersionStore projectionStore) : base(repository)
{
this.projectionVersioningPolicy = projectionVersioningPolicy;
this.projectionStore = projectionStore;
}

public async Task HandleAsync(RegisterProjection command)
Expand All @@ -26,7 +28,7 @@ public async Task HandleAsync(RegisterProjection command)
if (result.IsSuccess)
{
ar = result.Data;
ar.NotifyHash(command.Hash, projectionVersioningPolicy);
ar.NotifyHash(command.Id, command.Hash, projectionVersioningPolicy, projectionStore);
}

if (result.NotFound)
Expand All @@ -42,7 +44,7 @@ public Task HandleAsync(ReplayProjection command)

public Task HandleAsync(RebuildProjectionCommand command)
{
return UpdateAsync(command.Id, ar => ar.Rebuild(command.Hash, projectionVersioningPolicy));
return UpdateAsync(command.Id, ar => ar.Rebuild(command.Hash));
}

public Task HandleAsync(FinalizeProjectionVersionRequest command)
Expand Down

0 comments on commit 37725d2

Please sign in to comment.