Browse Source

Tests fixed.

pull/309/head
Sebastian 8 years ago
parent
commit
ffbe583bab
  1. 5
      src/Squidex.Domain.Apps.Entities/AppGrainCleaner.cs
  2. 88
      src/Squidex.Domain.Apps.Entities/Backup/AppCleanerGrain.cs
  3. 2
      src/Squidex.Domain.Apps.Entities/ICleanableAppStorage.cs
  4. 5
      src/Squidex.Domain.Apps.Entities/Tags/GrainTagService.cs
  5. 6
      tests/Squidex.Domain.Apps.Entities.Tests/AppGrainCleanerTests.cs
  6. 6
      tests/Squidex.Domain.Apps.Entities.Tests/Tags/GrainTagServiceTests.cs

5
src/Squidex.Domain.Apps.Entities/AppGrainCleaner.cs

@ -16,6 +16,11 @@ namespace Squidex.Domain.Apps.Entities
{ {
private readonly IGrainFactory grainFactory; private readonly IGrainFactory grainFactory;
public string Name
{
get { return typeof(T).Name; }
}
public AppGrainCleaner(IGrainFactory grainFactory) public AppGrainCleaner(IGrainFactory grainFactory)
{ {
Guard.NotNull(grainFactory, nameof(grainFactory)); Guard.NotNull(grainFactory, nameof(grainFactory));

88
src/Squidex.Domain.Apps.Entities/Backup/AppCleanerGrain.cs

@ -19,6 +19,7 @@ using Squidex.Domain.Apps.Entities.Schemas;
using Squidex.Domain.Apps.Entities.Schemas.State; using Squidex.Domain.Apps.Entities.Schemas.State;
using Squidex.Infrastructure; using Squidex.Infrastructure;
using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.Log;
using Squidex.Infrastructure.Orleans; using Squidex.Infrastructure.Orleans;
using Squidex.Infrastructure.States; using Squidex.Infrastructure.States;
using Squidex.Infrastructure.Tasks; using Squidex.Infrastructure.Tasks;
@ -32,6 +33,7 @@ namespace Squidex.Domain.Apps.Entities.Backup
private readonly IStore<Guid> store; private readonly IStore<Guid> store;
private readonly IEventStore eventStore; private readonly IEventStore eventStore;
private readonly IEnumerable<ICleanableAppStorage> storages; private readonly IEnumerable<ICleanableAppStorage> storages;
private readonly ISemanticLog log;
private IPersistence<State> persistence; private IPersistence<State> persistence;
private bool isCleaning; private bool isCleaning;
private State state = new State(); private State state = new State();
@ -44,18 +46,21 @@ namespace Squidex.Domain.Apps.Entities.Backup
public HashSet<Guid> PendingApps { get; set; } = new HashSet<Guid>(); public HashSet<Guid> PendingApps { get; set; } = new HashSet<Guid>();
} }
public AppCleanerGrain(IGrainFactory grainFactory, IEventStore eventStore, IStore<Guid> store, IEnumerable<ICleanableAppStorage> storages) public AppCleanerGrain(IGrainFactory grainFactory, IEventStore eventStore, IStore<Guid> store, IEnumerable<ICleanableAppStorage> storages, ISemanticLog log)
{ {
Guard.NotNull(grainFactory, nameof(grainFactory)); Guard.NotNull(grainFactory, nameof(grainFactory));
Guard.NotNull(store, nameof(store)); Guard.NotNull(store, nameof(store));
Guard.NotNull(storages, nameof(storages)); Guard.NotNull(storages, nameof(storages));
Guard.NotNull(eventStore, nameof(eventStore)); Guard.NotNull(eventStore, nameof(eventStore));
Guard.NotNull(log, nameof(log));
this.grainFactory = grainFactory; this.grainFactory = grainFactory;
this.store = store; this.store = store;
this.storages = storages; this.storages = storages;
this.log = log;
this.eventStore = eventStore; this.eventStore = eventStore;
} }
@ -106,21 +111,42 @@ namespace Squidex.Domain.Apps.Entities.Backup
{ {
foreach (var appId in state.Apps.ToList()) foreach (var appId in state.Apps.ToList())
{ {
try using (Profiler.StartSession())
{
await CleanAsync(appId);
state.Apps.Remove(appId);
}
catch (NotSupportedException)
{
state.Apps.Remove(appId);
state.PendingApps.Add(appId);
}
finally
{ {
await persistence.WriteSnapshotAsync(state); try
{
log.LogInformation(w => w
.WriteProperty("action", "cleanApp")
.WriteProperty("status", "started")
.WriteProperty("appId", appId.ToString()));
await CleanAsync(appId);
state.Apps.Remove(appId);
log.LogInformation(w =>
{
w.WriteProperty("action", "cleanApp");
w.WriteProperty("status", "completed");
w.WriteProperty("appId", appId.ToString());
Profiler.Session?.Write(w);
});
}
catch (Exception ex)
{
state.PendingApps.Add(appId);
log.LogError(ex, w => w
.WriteProperty("action", "cleanApp")
.WriteProperty("appId", appId.ToString()));
}
finally
{
state.Apps.Remove(appId);
await persistence.WriteSnapshotAsync(state);
}
} }
} }
} }
@ -132,25 +158,37 @@ namespace Squidex.Domain.Apps.Entities.Backup
private async Task CleanAsync(Guid appId) private async Task CleanAsync(Guid appId)
{ {
await eventStore.DeleteManyAsync("AppId", appId); using (Profiler.Trace("DeleteEvents"))
var ruleIds = await grainFactory.GetGrain<IRulesByAppIndex>(appId).GetRuleIdsAsync();
foreach (var ruleId in ruleIds)
{ {
await store.RemoveSnapshotAsync<RuleState>(ruleId); await eventStore.DeleteManyAsync("AppId", appId);
} }
var schemaIds = await grainFactory.GetGrain<ISchemasByAppIndex>(appId).GetSchemaIdsAsync(); using (Profiler.Trace("DeleteRules"))
{
var ruleIds = await grainFactory.GetGrain<IRulesByAppIndex>(appId).GetRuleIdsAsync();
foreach (var schemaId in schemaIds) foreach (var ruleId in ruleIds)
{
await store.RemoveSnapshotAsync<RuleState>(ruleId);
}
}
using (Profiler.Trace("DeleteSchemas"))
{ {
await store.RemoveSnapshotAsync<SchemaState>(schemaId); var schemaIds = await grainFactory.GetGrain<ISchemasByAppIndex>(appId).GetSchemaIdsAsync();
foreach (var schemaId in schemaIds)
{
await store.RemoveSnapshotAsync<SchemaState>(schemaId);
}
} }
foreach (var storage in storages) foreach (var storage in storages)
{ {
await storage.ClearAsync(appId); using (Profiler.Trace($"{storage.Name}.ClearAsync"))
{
await storage.ClearAsync(appId);
}
} }
await store.RemoveSnapshotAsync<AppState>(appId); await store.RemoveSnapshotAsync<AppState>(appId);

2
src/Squidex.Domain.Apps.Entities/ICleanableAppStorage.cs

@ -12,6 +12,8 @@ namespace Squidex.Domain.Apps.Entities
{ {
public interface ICleanableAppStorage public interface ICleanableAppStorage
{ {
string Name { get; }
Task ClearAsync(Guid appId); Task ClearAsync(Guid appId);
} }
} }

5
src/Squidex.Domain.Apps.Entities/Tags/GrainTagService.cs

@ -17,6 +17,11 @@ namespace Squidex.Domain.Apps.Entities.Tags
{ {
private readonly IGrainFactory grainFactory; private readonly IGrainFactory grainFactory;
public string Name
{
get { return "Tags"; }
}
public GrainTagService(IGrainFactory grainFactory) public GrainTagService(IGrainFactory grainFactory)
{ {
Guard.NotNull(grainFactory, nameof(grainFactory)); Guard.NotNull(grainFactory, nameof(grainFactory));

6
tests/Squidex.Domain.Apps.Entities.Tests/AppGrainCleanerTests.cs

@ -28,6 +28,12 @@ namespace Squidex.Domain.Apps.Entities
sut = new AppGrainCleaner<ICleanableAppGrain>(grainFactory); sut = new AppGrainCleaner<ICleanableAppGrain>(grainFactory);
} }
[Fact]
public void Should_provide_name()
{
Assert.Equal(typeof(ICleanableAppGrain).Name, sut.Name);
}
[Fact] [Fact]
public async Task Should_forward_to_index() public async Task Should_forward_to_index()
{ {

6
tests/Squidex.Domain.Apps.Entities.Tests/Tags/GrainTagServiceTests.cs

@ -29,6 +29,12 @@ namespace Squidex.Domain.Apps.Entities.Tags
sut = new GrainTagService(grainFactory); sut = new GrainTagService(grainFactory);
} }
[Fact]
public void Should_provide_name()
{
Assert.Equal("Tags", sut.Name);
}
[Fact] [Fact]
public async Task Should_call_grain_when_clearing() public async Task Should_call_grain_when_clearing()
{ {

Loading…
Cancel
Save