Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
freever committed Oct 19, 2021
1 parent 0a9eca6 commit 5d1ef42
Show file tree
Hide file tree
Showing 28 changed files with 450 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/blauhaus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
dotnet pack ./src/Blauhaus.Sync.TestHelpers.Orleans/Blauhaus.Sync.TestHelpers.Orleans.csproj -p:PackageVersion=${VERSION_NAME} --no-build --output ./ --configuration Release
dotnet pack ./src/Blauhaus.Sync.TestHelpers.EfCore/Blauhaus.Sync.TestHelpers.EfCore.csproj -p:PackageVersion=${VERSION_NAME} --no-build --output ./ --configuration Release
dotnet pack ./src/Blauhaus.Sync.TestHelpers.Sqlite/Blauhaus.Sync.TestHelpers.Sqlite.csproj -p:PackageVersion=${VERSION_NAME} --no-build --output ./ --configuration Release
dotnet pack ./src/Blauhaus.Sync.Server.Orleans/Blauhaus.Sync.Server.Orleans.csproj -p:PackageVersion=${VERSION_NAME} --no-build --output ./ --configuration Release
- name: Deploy NuGet Package
run: |
dotnet nuget push /home/runner/work/Blauhaus.Sync/Blauhaus.Sync/Blauhaus.Sync.Abstractions.${VERSION_NAME}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
Expand All @@ -38,6 +39,7 @@ jobs:
dotnet nuget push /home/runner/work/Blauhaus.Sync/Blauhaus.Sync/Blauhaus.Sync.TestHelpers.${VERSION_NAME}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
dotnet nuget push /home/runner/work/Blauhaus.Sync/Blauhaus.Sync/Blauhaus.Sync.Client.SignalR.${VERSION_NAME}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
dotnet nuget push /home/runner/work/Blauhaus.Sync/Blauhaus.Sync/Blauhaus.Sync.Server.EfCore.${VERSION_NAME}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
dotnet nuget push /home/runner/work/Blauhaus.Sync/Blauhaus.Sync/Blauhaus.Sync.Server.Orleans.${VERSION_NAME}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
dotnet nuget push /home/runner/work/Blauhaus.Sync/Blauhaus.Sync/Blauhaus.Sync.TestHelpers.Orleans.${VERSION_NAME}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
dotnet nuget push /home/runner/work/Blauhaus.Sync/Blauhaus.Sync/Blauhaus.Sync.TestHelpers.EfCore.${VERSION_NAME}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
dotnet nuget push /home/runner/work/Blauhaus.Sync/Blauhaus.Sync/Blauhaus.Sync.TestHelpers.Sqlite.${VERSION_NAME}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blauhaus.Common.Abstractions" Version="1.12.3" />
<PackageReference Include="Blauhaus.Domain.Abstractions" Version="1.5.10" />
<PackageReference Include="Blauhaus.Errors" Version="0.4.3" />
<PackageReference Include="Blauhaus.Common.Abstractions" Version="1.12.5" />
<PackageReference Include="Blauhaus.Domain.Abstractions" Version="1.5.12" />
<PackageReference Include="Blauhaus.Errors" Version="0.4.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Blauhaus.SignalR.Client" Version="0.12.0" />
<PackageReference Include="Blauhaus.SignalR.Client" Version="0.12.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -16,9 +16,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Blauhaus.ClientActors" Version="0.9.4" />
<PackageReference Include="Blauhaus.ClientDatabase.Sqlite" Version="0.7.9" />
<PackageReference Include="Blauhaus.Domain.Client.Sqlite" Version="1.5.10" />
<PackageReference Include="Blauhaus.ClientActors" Version="0.9.6" />
<PackageReference Include="Blauhaus.ClientDatabase.Sqlite" Version="0.7.11" />
<PackageReference Include="Blauhaus.Domain.Client.Sqlite" Version="1.5.12" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/Blauhaus.Sync.Client/Blauhaus.Sync.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand All @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Blauhaus.ClientActors" Version="0.9.4" />
<PackageReference Include="Blauhaus.ClientActors" Version="0.9.6" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Blauhaus.Domain.Server.EFCore" Version="1.5.10" />
<PackageReference Include="Blauhaus.Domain.Server.EFCore" Version="1.5.12" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions src/Blauhaus.Sync.Server.Orleans/Abstractions/IDtoSyncGrain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Blauhaus.Domain.Abstractions.CommandHandlers;
using Blauhaus.Orleans.Abstractions.Grains;
using Blauhaus.Orleans.Abstractions.Handlers;
using Blauhaus.SignalR.Abstractions.Auth;
using System;
using System.Threading.Tasks;
using Blauhaus.Domain.Abstractions.Entities;
using Blauhaus.Sync.Abstractions.Common;
using Orleans.Concurrency;

namespace Blauhaus.Sync.Server.Orleans.Abstractions
{
public interface IDtoSyncGrain<TDto> : IGrainSingleton, IConnectedUserHandler,
IAuthenticatedCommandHandler<DtoBatch<TDto, Guid>, DtoSyncCommand, IConnectedUser>
where TDto : IClientEntity<Guid>
{
Task SetBatchSizeAsync(int batchSize);

[OneWay]
Task UpdateDtoAsync(TDto dto);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blauhaus.Orleans.EfCore" Version="0.6.13" />
<PackageReference Include="Blauhaus.Orleans" Version="0.6.16" />
<PackageReference Include="Blauhaus.Orleans.EfCore" Version="0.6.16" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Blauhaus.Sync.Server.EfCore\Blauhaus.Sync.Server.EfCore.csproj" />
<ProjectReference Include="..\Blauhaus.Sync.Abstractions\Blauhaus.Sync.Abstractions.csproj" />
</ItemGroup>

</Project>
154 changes: 154 additions & 0 deletions src/Blauhaus.Sync.Server.Orleans/Grains/BaseDtoSyncGrain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Blauhaus.Analytics.Abstractions.Service;
using Blauhaus.Domain.Abstractions.DtoHandlers;
using Blauhaus.Domain.Abstractions.Entities;
using Blauhaus.Domain.Server.EFCore.Extensions;
using Blauhaus.Domain.Server.Entities;
using Blauhaus.Orleans.Abstractions.Resolver;
using Blauhaus.Orleans.EfCore.Grains;
using Blauhaus.Responses;
using Blauhaus.SignalR.Abstractions.Auth;
using Blauhaus.Sync.Abstractions.Common;
using Blauhaus.Sync.Server.Orleans.Abstractions;
using Blauhaus.Time.Abstractions;
using Microsoft.EntityFrameworkCore;
using EntityState = Blauhaus.Domain.Abstractions.Entities.EntityState;

namespace Blauhaus.Sync.Server.Orleans.Grains
{
public abstract class BaseDtoSyncGrain<TDbContext, TDto, TEntity, TGrainResolver> : BaseDbGrain<TDbContext, TGrainResolver>, IDtoSyncGrain<TDto>
where TDbContext : DbContext
where TDto : IClientEntity<Guid>
where TGrainResolver : IGrainResolver
where TEntity : BaseServerEntity, IDtoOwner<TDto>
{

protected readonly Dictionary<string, IConnectedUser> UserConnections = new();
protected int BatchSize = 50;
protected Dictionary<Guid, TDto> AllDtos = new();

protected BaseDtoSyncGrain(
Func<TDbContext> dbContextFactory,
IAnalyticsService analyticsService,
ITimeService timeService,
TGrainResolver grainResolver)
: base(dbContextFactory, analyticsService, timeService, grainResolver)
{
}

public Task SetBatchSizeAsync(int batchSize)
{
BatchSize = batchSize;
return Task.CompletedTask;
}

public override async Task OnActivateAsync()
{
using var db = GetDbContext();

var query = Include(db.Set<TEntity>().AsQueryable());
query = await ModifySyncQueryAsync(query);
var allEntities = await query.ToArrayAsync();

foreach (var entity in allEntities)
{
AllDtos[entity.Id] =await entity.GetDtoAsync();
}
}

public Task<Response<DtoBatch<TDto, Guid>>> HandleAsync(DtoSyncCommand command, IConnectedUser user)
{
var modifiedAfter = command.ModifiedAfterTicks;

Func<TDto, bool> filter;

if (command.IsFirstSync)
{
if (command.ModifiedAfterTicks > 0)
{
filter = dto =>
dto.ModifiedAtTicks > modifiedAfter &&
dto.EntityState == EntityState.Active;
}
else
{
filter = dto =>
dto.EntityState == EntityState.Active;
}
}
else
{
filter = dto => dto.ModifiedAtTicks > modifiedAfter;
}

//there is a problem when 2 entities have exactly the same modified and the first is the last one in a batch
//the next batch will ask for modified after the previous one so entity 2 is exclude. Unlikely to happen but could be nasty
//possible solutions - add any entities with exactly the same modified as the last one in the set?

var totalCount = AllDtos.Values
.Count(filter);

var dtoBatch = AllDtos.Values
.OrderBy(x => x.ModifiedAtTicks)
.Where(filter)
.Take(BatchSize).ToArray();

return Response.SuccessTask(totalCount == 0
? DtoBatch<TDto, Guid>.Empty()
: DtoBatch<TDto, Guid>.Create(dtoBatch, Math.Max(0, totalCount - dtoBatch.Length)));
}

protected virtual IQueryable<TEntity> Include(IQueryable<TEntity> query)
{
return query;
}

protected virtual Task<IQueryable<TEntity>> ModifySyncQueryAsync(IQueryable<TEntity> query)
{
return Task.FromResult(query);
}


public Task UpdateDtoAsync(TDto dto)
{
//todo update connected users
AllDtos[dto.Id] = dto;
return Task.CompletedTask;
}



public async Task ConnectUserAsync(IConnectedUser user)
{
if (!UserConnections.TryGetValue(user.UniqueId, out _))
{
UserConnections[user.UniqueId] = user;
await HandleConnectedUserAsync(user);
}
}

public async Task DisconnectUserAsync(IConnectedUser user)
{
if (UserConnections.TryGetValue(user.UniqueId, out _))
{
UserConnections.Remove(user.UniqueId);
await HandleDisconnectedUserAsync(user);
}
}


protected virtual Task HandleConnectedUserAsync(IConnectedUser user)
{
return Task.CompletedTask;
}

protected virtual Task HandleDisconnectedUserAsync(IConnectedUser user)
{
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blauhaus.Domain.TestHelpers.EFCore" Version="1.5.10" />
<PackageReference Include="Blauhaus.Domain.TestHelpers.EFCore" Version="1.5.12" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 5d1ef42

Please sign in to comment.