diff --git a/backend/src/Migrations/Migrations/ClearRules.cs b/backend/src/Migrations/Migrations/ClearRules.cs index 7c8ba563a..bf8a06801 100644 --- a/backend/src/Migrations/Migrations/ClearRules.cs +++ b/backend/src/Migrations/Migrations/ClearRules.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Squidex.Domain.Apps.Entities.Rules.DomainObject; using Squidex.Infrastructure.Migrations; @@ -21,7 +22,7 @@ namespace Migrations.Migrations this.store = store; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { return store.ClearSnapshotsAsync(); } diff --git a/backend/src/Migrations/Migrations/ClearSchemas.cs b/backend/src/Migrations/Migrations/ClearSchemas.cs index 821ab8566..e1100cdc2 100644 --- a/backend/src/Migrations/Migrations/ClearSchemas.cs +++ b/backend/src/Migrations/Migrations/ClearSchemas.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Squidex.Domain.Apps.Entities.Schemas.DomainObject; using Squidex.Infrastructure.Migrations; @@ -21,7 +22,7 @@ namespace Migrations.Migrations this.store = store; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { return store.ClearSnapshotsAsync(); } diff --git a/backend/src/Migrations/Migrations/ConvertEventStore.cs b/backend/src/Migrations/Migrations/ConvertEventStore.cs index 54db4b50d..82e074e90 100644 --- a/backend/src/Migrations/Migrations/ConvertEventStore.cs +++ b/backend/src/Migrations/Migrations/ConvertEventStore.cs @@ -6,6 +6,7 @@ // ========================================================================== using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; @@ -23,7 +24,7 @@ namespace Migrations.Migrations this.eventStore = eventStore; } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { if (eventStore is MongoEventStore mongoEventStore) { @@ -42,7 +43,7 @@ namespace Migrations.Migrations if (writes.Count == 1000 || (force && writes.Count > 0)) { - await collection.BulkWriteAsync(writes); + await collection.BulkWriteAsync(writes, cancellationToken: ct); writes.Clear(); } @@ -59,7 +60,7 @@ namespace Migrations.Migrations } await WriteAsync(new ReplaceOneModel(filter.Eq("_id", commit["_id"].AsString), commit), false); - }); + }, ct); await WriteAsync(null, true); } diff --git a/backend/src/Migrations/Migrations/ConvertEventStoreAppId.cs b/backend/src/Migrations/Migrations/ConvertEventStoreAppId.cs index 5139dd8fe..8c73ff9c1 100644 --- a/backend/src/Migrations/Migrations/ConvertEventStoreAppId.cs +++ b/backend/src/Migrations/Migrations/ConvertEventStoreAppId.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; @@ -25,7 +26,7 @@ namespace Migrations.Migrations this.eventStore = eventStore; } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { if (eventStore is MongoEventStore mongoEventStore) { @@ -45,7 +46,7 @@ namespace Migrations.Migrations if (writesBatches.Count == 1000 || (force && writesBatches.Count > 0)) { - await collection.BulkWriteAsync(writesBatches); + await collection.BulkWriteAsync(writesBatches, cancellationToken: ct); writesBatches.Clear(); } @@ -86,7 +87,7 @@ namespace Migrations.Migrations await WriteAsync(write, false); } - }); + }, ct); await WriteAsync(null, true); } diff --git a/backend/src/Migrations/Migrations/CreateAssetSlugs.cs b/backend/src/Migrations/Migrations/CreateAssetSlugs.cs index ba81eefc7..8ecd2f467 100644 --- a/backend/src/Migrations/Migrations/CreateAssetSlugs.cs +++ b/backend/src/Migrations/Migrations/CreateAssetSlugs.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Squidex.Domain.Apps.Entities.Assets; using Squidex.Domain.Apps.Entities.Assets.DomainObject; @@ -23,7 +24,7 @@ namespace Migrations.Migrations this.stateForAssets = stateForAssets; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { return stateForAssets.ReadAllAsync(async (state, version) => { @@ -32,7 +33,7 @@ namespace Migrations.Migrations var key = DomainId.Combine(state.AppId.Id, state.Id); await stateForAssets.WriteAsync(key, state, version, version); - }); + }, ct); } } } diff --git a/backend/src/Migrations/Migrations/MongoDb/AddAppIdToEventStream.cs b/backend/src/Migrations/Migrations/MongoDb/AddAppIdToEventStream.cs index 6a4c4962a..bded9e846 100644 --- a/backend/src/Migrations/Migrations/MongoDb/AddAppIdToEventStream.cs +++ b/backend/src/Migrations/Migrations/MongoDb/AddAppIdToEventStream.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; using MongoDB.Bson; @@ -26,7 +27,7 @@ namespace Migrations.Migrations.MongoDb this.database = database; } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { const int SizeOfBatch = 1000; const int SizeOfQueue = 20; @@ -122,7 +123,7 @@ namespace Migrations.Migrations.MongoDb PropagateCompletion = true }); - await collectionOld.Find(new BsonDocument()).ForEachAsync(batchBlock.SendAsync); + await collectionOld.Find(new BsonDocument()).ForEachAsync(batchBlock.SendAsync, ct); batchBlock.Complete(); diff --git a/backend/src/Migrations/Migrations/MongoDb/ConvertDocumentIds.cs b/backend/src/Migrations/Migrations/MongoDb/ConvertDocumentIds.cs index 247382d43..da158ce1f 100644 --- a/backend/src/Migrations/Migrations/MongoDb/ConvertDocumentIds.cs +++ b/backend/src/Migrations/Migrations/MongoDb/ConvertDocumentIds.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; using MongoDB.Bson; @@ -55,22 +56,22 @@ namespace Migrations.Migrations.MongoDb return this; } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { switch (scope) { case Scope.Assets: - await RebuildAsync(database, ConvertParentId, "States_Assets"); - await RebuildAsync(database, ConvertParentId, "States_AssetFolders"); + await RebuildAsync(database, ConvertParentId, "States_Assets", ct); + await RebuildAsync(database, ConvertParentId, "States_AssetFolders", ct); break; case Scope.Contents: - await RebuildAsync(databaseContent, null, "State_Contents_All"); - await RebuildAsync(databaseContent, null, "State_Contents_Published"); + await RebuildAsync(databaseContent, null, "State_Contents_All", ct); + await RebuildAsync(databaseContent, null, "State_Contents_Published", ct); break; } } - private static async Task RebuildAsync(IMongoDatabase database, Action? extraAction, string collectionNameOld) + private static async Task RebuildAsync(IMongoDatabase database, Action? extraAction, string collectionNameOld, CancellationToken ct) { const int SizeOfBatch = 1000; const int SizeOfQueue = 10; @@ -88,7 +89,7 @@ namespace Migrations.Migrations.MongoDb return; } - await collectionNew.DeleteManyAsync(new BsonDocument()); + await collectionNew.DeleteManyAsync(new BsonDocument(), ct); var batchBlock = new BatchBlock(SizeOfBatch, new GroupingDataflowBlockOptions { @@ -156,7 +157,7 @@ namespace Migrations.Migrations.MongoDb PropagateCompletion = true }); - await collectionOld.Find(new BsonDocument()).ForEachAsync(batchBlock.SendAsync); + await collectionOld.Find(new BsonDocument()).ForEachAsync(batchBlock.SendAsync, ct); batchBlock.Complete(); diff --git a/backend/src/Migrations/Migrations/MongoDb/ConvertOldSnapshotStores.cs b/backend/src/Migrations/Migrations/MongoDb/ConvertOldSnapshotStores.cs index 6bfd8cc61..a22ffaefd 100644 --- a/backend/src/Migrations/Migrations/MongoDb/ConvertOldSnapshotStores.cs +++ b/backend/src/Migrations/Migrations/MongoDb/ConvertOldSnapshotStores.cs @@ -6,6 +6,7 @@ // ========================================================================== using System.Linq; +using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; @@ -22,7 +23,7 @@ namespace Migrations.Migrations.MongoDb this.database = database; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { var collections = new[] { diff --git a/backend/src/Migrations/Migrations/MongoDb/ConvertRuleEventsJson.cs b/backend/src/Migrations/Migrations/MongoDb/ConvertRuleEventsJson.cs index fe69573e9..d0f4905ca 100644 --- a/backend/src/Migrations/Migrations/MongoDb/ConvertRuleEventsJson.cs +++ b/backend/src/Migrations/Migrations/MongoDb/ConvertRuleEventsJson.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; @@ -21,9 +22,9 @@ namespace Migrations.Migrations.MongoDb collection = database.GetCollection("RuleEvents"); } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { - foreach (var document in collection.Find(new BsonDocument()).ToEnumerable()) + foreach (var document in collection.Find(new BsonDocument()).ToEnumerable(ct)) { try { @@ -31,7 +32,7 @@ namespace Migrations.Migrations.MongoDb var filter = Builders.Filter.Eq("_id", document["_id"].ToString()); - await collection.ReplaceOneAsync(filter, document); + await collection.ReplaceOneAsync(filter, document, cancellationToken: ct); } catch { diff --git a/backend/src/Migrations/Migrations/MongoDb/DeleteContentCollections.cs b/backend/src/Migrations/Migrations/MongoDb/DeleteContentCollections.cs index 73b30dbfe..f2c33f57f 100644 --- a/backend/src/Migrations/Migrations/MongoDb/DeleteContentCollections.cs +++ b/backend/src/Migrations/Migrations/MongoDb/DeleteContentCollections.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using Squidex.Infrastructure.Migrations; @@ -20,12 +21,12 @@ namespace Migrations.Migrations.MongoDb this.database = database; } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { - await database.DropCollectionAsync("States_Contents"); - await database.DropCollectionAsync("States_Contents_Archive"); - await database.DropCollectionAsync("State_Content_Draft"); - await database.DropCollectionAsync("State_Content_Published"); + await database.DropCollectionAsync("States_Contents", ct); + await database.DropCollectionAsync("States_Contents_Archive", ct); + await database.DropCollectionAsync("State_Content_Draft", ct); + await database.DropCollectionAsync("State_Content_Published", ct); } } } diff --git a/backend/src/Migrations/Migrations/MongoDb/RenameAssetMetadata.cs b/backend/src/Migrations/Migrations/MongoDb/RenameAssetMetadata.cs index ab8bdb1b2..1050e82fb 100644 --- a/backend/src/Migrations/Migrations/MongoDb/RenameAssetMetadata.cs +++ b/backend/src/Migrations/Migrations/MongoDb/RenameAssetMetadata.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; @@ -21,7 +22,7 @@ namespace Migrations.Migrations.MongoDb this.database = database; } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { var collection = database.GetCollection("States_Assets"); @@ -29,39 +30,39 @@ namespace Migrations.Migrations.MongoDb Builders.Update .Set("md", new BsonDocument()); - await collection.UpdateManyAsync(new BsonDocument(), createMetadata); + await collection.UpdateManyAsync(new BsonDocument(), createMetadata, cancellationToken: ct); var removeNullPixelInfos = Builders.Update .Unset("ph") .Unset("pw"); - await collection.UpdateManyAsync(new BsonDocument("ph", BsonValue.Create(null)), removeNullPixelInfos); + await collection.UpdateManyAsync(new BsonDocument("ph", BsonValue.Create(null)), removeNullPixelInfos, cancellationToken: ct); var setPixelDimensions = Builders.Update .Rename("ph", "md.pixelHeight") .Rename("pw", "md.pixelWidth"); - await collection.UpdateManyAsync(new BsonDocument(), setPixelDimensions); + await collection.UpdateManyAsync(new BsonDocument(), setPixelDimensions, cancellationToken: ct); var setTypeToImage = Builders.Update .Set("at", "Image"); - await collection.UpdateManyAsync(new BsonDocument("im", true), setTypeToImage); + await collection.UpdateManyAsync(new BsonDocument("im", true), setTypeToImage, cancellationToken: ct); var setTypeToUnknown = Builders.Update .Set("at", "Unknown"); - await collection.UpdateManyAsync(new BsonDocument("im", false), setTypeToUnknown); + await collection.UpdateManyAsync(new BsonDocument("im", false), setTypeToUnknown, cancellationToken: ct); var removeIsImage = Builders.Update .Unset("im"); - await collection.UpdateManyAsync(new BsonDocument(), removeIsImage); + await collection.UpdateManyAsync(new BsonDocument(), removeIsImage, cancellationToken: ct); } } } diff --git a/backend/src/Migrations/Migrations/MongoDb/RenameAssetSlugField.cs b/backend/src/Migrations/Migrations/MongoDb/RenameAssetSlugField.cs index 1fb1d8916..78e52b518 100644 --- a/backend/src/Migrations/Migrations/MongoDb/RenameAssetSlugField.cs +++ b/backend/src/Migrations/Migrations/MongoDb/RenameAssetSlugField.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; @@ -21,13 +22,13 @@ namespace Migrations.Migrations.MongoDb this.database = database; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { var collection = database.GetCollection("States_Assets"); var update = Builders.Update.Rename("FileNameSlug", "Slug"); - return collection.UpdateManyAsync(new BsonDocument(), update); + return collection.UpdateManyAsync(new BsonDocument(), update, cancellationToken: ct); } } } diff --git a/backend/src/Migrations/Migrations/MongoDb/RestructureContentCollection.cs b/backend/src/Migrations/Migrations/MongoDb/RestructureContentCollection.cs index 8bef57f48..30e86e3d4 100644 --- a/backend/src/Migrations/Migrations/MongoDb/RestructureContentCollection.cs +++ b/backend/src/Migrations/Migrations/MongoDb/RestructureContentCollection.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Driver; @@ -22,20 +23,20 @@ namespace Migrations.Migrations.MongoDb this.contentDatabase = contentDatabase; } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { if (await contentDatabase.CollectionExistsAsync("State_Content_Draft")) { - await contentDatabase.DropCollectionAsync("State_Contents"); - await contentDatabase.DropCollectionAsync("State_Content_Published"); - await contentDatabase.RenameCollectionAsync("State_Content_Draft", "State_Contents"); + await contentDatabase.DropCollectionAsync("State_Contents", ct); + await contentDatabase.DropCollectionAsync("State_Content_Published", ct); + await contentDatabase.RenameCollectionAsync("State_Content_Draft", "State_Contents", cancellationToken: ct); } if (await contentDatabase.CollectionExistsAsync("State_Contents")) { var collection = contentDatabase.GetCollection("State_Contents"); - await collection.UpdateManyAsync(new BsonDocument(), Builders.Update.Unset("dt")); + await collection.UpdateManyAsync(new BsonDocument(), Builders.Update.Unset("dt"), cancellationToken: ct); } } } diff --git a/backend/src/Migrations/Migrations/PopulateGrainIndexes.cs b/backend/src/Migrations/Migrations/PopulateGrainIndexes.cs index be2bb4981..2f6fa1cd7 100644 --- a/backend/src/Migrations/Migrations/PopulateGrainIndexes.cs +++ b/backend/src/Migrations/Migrations/PopulateGrainIndexes.cs @@ -6,6 +6,7 @@ // ========================================================================== using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Squidex.Domain.Apps.Entities.Apps.Indexes; using Squidex.Domain.Apps.Entities.Rules.Indexes; @@ -39,15 +40,15 @@ namespace Migrations.Migrations this.eventStore = eventStore; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { return Task.WhenAll( - RebuildAppIndexes(), - RebuildRuleIndexes(), - RebuildSchemaIndexes()); + RebuildAppIndexes(ct), + RebuildRuleIndexes(ct), + RebuildSchemaIndexes(ct)); } - private async Task RebuildAppIndexes() + private async Task RebuildAppIndexes(CancellationToken ct) { var appsByName = new Dictionary(); var appsByUser = new Dictionary>(); @@ -75,7 +76,7 @@ namespace Migrations.Migrations } } - await foreach (var storedEvent in eventStore.QueryAllAsync("^app\\-")) + await foreach (var storedEvent in eventStore.QueryAllAsync("^app\\-", ct: ct)) { var @event = eventDataFormatter.ParseIfKnown(storedEvent); @@ -119,7 +120,7 @@ namespace Migrations.Migrations } } - private async Task RebuildRuleIndexes() + private async Task RebuildRuleIndexes(CancellationToken ct) { var rulesByApp = new Dictionary>(); @@ -128,7 +129,7 @@ namespace Migrations.Migrations return rulesByApp!.GetOrAddNew(@event.AppId.Id); } - await foreach (var storedEvent in eventStore.QueryAllAsync("^rule\\-")) + await foreach (var storedEvent in eventStore.QueryAllAsync("^rule\\-", ct: ct)) { var @event = eventDataFormatter.ParseIfKnown(storedEvent); @@ -152,7 +153,7 @@ namespace Migrations.Migrations } } - private async Task RebuildSchemaIndexes() + private async Task RebuildSchemaIndexes(CancellationToken ct) { var schemasByApp = new Dictionary>(); @@ -161,7 +162,7 @@ namespace Migrations.Migrations return schemasByApp!.GetOrAddNew(@event.AppId.Id); } - await foreach (var storedEvent in eventStore.QueryAllAsync("^schema\\-")) + await foreach (var storedEvent in eventStore.QueryAllAsync("^schema\\-", ct: ct)) { var @event = eventDataFormatter.ParseIfKnown(storedEvent); @@ -185,4 +186,4 @@ namespace Migrations.Migrations } } } -} \ No newline at end of file +} diff --git a/backend/src/Migrations/Migrations/RebuildApps.cs b/backend/src/Migrations/Migrations/RebuildApps.cs index 6402273c2..56e812d98 100644 --- a/backend/src/Migrations/Migrations/RebuildApps.cs +++ b/backend/src/Migrations/Migrations/RebuildApps.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Squidex.Infrastructure.Commands; @@ -24,9 +25,9 @@ namespace Migrations.Migrations this.rebuildOptions = rebuildOptions.Value; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { - return rebuilder.RebuildAppsAsync(rebuildOptions.BatchSize); + return rebuilder.RebuildAppsAsync(rebuildOptions.BatchSize, ct); } } } diff --git a/backend/src/Migrations/Migrations/RebuildAssetFolders.cs b/backend/src/Migrations/Migrations/RebuildAssetFolders.cs index ebef613cc..74a0f71c4 100644 --- a/backend/src/Migrations/Migrations/RebuildAssetFolders.cs +++ b/backend/src/Migrations/Migrations/RebuildAssetFolders.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Squidex.Infrastructure.Commands; @@ -24,9 +25,9 @@ namespace Migrations.Migrations this.rebuildOptions = rebuildOptions.Value; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { - return rebuilder.RebuildAssetFoldersAsync(rebuildOptions.BatchSize); + return rebuilder.RebuildAssetFoldersAsync(rebuildOptions.BatchSize, ct); } } } diff --git a/backend/src/Migrations/Migrations/RebuildAssets.cs b/backend/src/Migrations/Migrations/RebuildAssets.cs index 1893f681e..9a639e7ac 100644 --- a/backend/src/Migrations/Migrations/RebuildAssets.cs +++ b/backend/src/Migrations/Migrations/RebuildAssets.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Squidex.Infrastructure.Commands; @@ -24,9 +25,9 @@ namespace Migrations.Migrations this.rebuildOptions = rebuildOptions.Value; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { - return rebuilder.RebuildAssetsAsync(rebuildOptions.BatchSize); + return rebuilder.RebuildAssetsAsync(rebuildOptions.BatchSize, ct); } } } diff --git a/backend/src/Migrations/Migrations/RebuildContents.cs b/backend/src/Migrations/Migrations/RebuildContents.cs index bba3e1e2d..771252a6d 100644 --- a/backend/src/Migrations/Migrations/RebuildContents.cs +++ b/backend/src/Migrations/Migrations/RebuildContents.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Squidex.Infrastructure.Commands; @@ -24,9 +25,9 @@ namespace Migrations.Migrations this.rebuildOptions = rebuildOptions.Value; } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { - return rebuilder.RebuildContentAsync(rebuildOptions.BatchSize); + return rebuilder.RebuildContentAsync(rebuildOptions.BatchSize, ct); } } } diff --git a/backend/src/Migrations/Migrations/RebuildSnapshots.cs b/backend/src/Migrations/Migrations/RebuildSnapshots.cs index c484d4eb0..cf6b74965 100644 --- a/backend/src/Migrations/Migrations/RebuildSnapshots.cs +++ b/backend/src/Migrations/Migrations/RebuildSnapshots.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Squidex.Infrastructure.Commands; @@ -24,14 +25,14 @@ namespace Migrations.Migrations this.rebuildOptions = rebuildOptions.Value; } - public async Task UpdateAsync() + public async Task UpdateAsync(CancellationToken ct) { - await rebuilder.RebuildAppsAsync(rebuildOptions.BatchSize); - await rebuilder.RebuildSchemasAsync(rebuildOptions.BatchSize); - await rebuilder.RebuildRulesAsync(rebuildOptions.BatchSize); - await rebuilder.RebuildContentAsync(rebuildOptions.BatchSize); - await rebuilder.RebuildAssetsAsync(rebuildOptions.BatchSize); - await rebuilder.RebuildAssetFoldersAsync(rebuildOptions.BatchSize); + await rebuilder.RebuildAppsAsync(rebuildOptions.BatchSize, ct); + await rebuilder.RebuildSchemasAsync(rebuildOptions.BatchSize, ct); + await rebuilder.RebuildRulesAsync(rebuildOptions.BatchSize, ct); + await rebuilder.RebuildContentAsync(rebuildOptions.BatchSize, ct); + await rebuilder.RebuildAssetsAsync(rebuildOptions.BatchSize, ct); + await rebuilder.RebuildAssetFoldersAsync(rebuildOptions.BatchSize, ct); } } } diff --git a/backend/src/Migrations/Migrations/StartEventConsumers.cs b/backend/src/Migrations/Migrations/StartEventConsumers.cs index 96c6b3a1b..8e635c215 100644 --- a/backend/src/Migrations/Migrations/StartEventConsumers.cs +++ b/backend/src/Migrations/Migrations/StartEventConsumers.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Orleans; using Squidex.Infrastructure.EventSourcing.Grains; @@ -22,7 +23,7 @@ namespace Migrations.Migrations eventConsumerManager = grainFactory.GetGrain(SingleGrain.Id); } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { return eventConsumerManager.StartAllAsync(); } diff --git a/backend/src/Migrations/Migrations/StopEventConsumers.cs b/backend/src/Migrations/Migrations/StopEventConsumers.cs index 9de6378e1..152793f49 100644 --- a/backend/src/Migrations/Migrations/StopEventConsumers.cs +++ b/backend/src/Migrations/Migrations/StopEventConsumers.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; using Orleans; using Squidex.Infrastructure.EventSourcing.Grains; @@ -22,7 +23,7 @@ namespace Migrations.Migrations eventConsumerManager = grainFactory.GetGrain(SingleGrain.Id); } - public Task UpdateAsync() + public Task UpdateAsync(CancellationToken ct) { return eventConsumerManager.StopAllAsync(); } diff --git a/backend/src/Migrations/RebuildRunner.cs b/backend/src/Migrations/RebuildRunner.cs index e56944ba2..175dc724a 100644 --- a/backend/src/Migrations/RebuildRunner.cs +++ b/backend/src/Migrations/RebuildRunner.cs @@ -17,23 +17,23 @@ namespace Migrations { public sealed class RebuildRunner { - private readonly RepairFiles repairFiles; + private readonly RebuildFiles rebuildFiles; private readonly Rebuilder rebuilder; private readonly PopulateGrainIndexes populateGrainIndexes; private readonly RebuildOptions rebuildOptions; public RebuildRunner( - RepairFiles repairFiles, - Rebuilder rebuilder, IOptions rebuildOptions, + Rebuilder rebuilder, + RebuildFiles rebuildFiles, PopulateGrainIndexes populateGrainIndexes) { - Guard.NotNull(repairFiles, nameof(repairFiles)); + Guard.NotNull(rebuildFiles, nameof(rebuildFiles)); Guard.NotNull(rebuilder, nameof(rebuilder)); Guard.NotNull(rebuildOptions, nameof(rebuildOptions)); Guard.NotNull(populateGrainIndexes, nameof(populateGrainIndexes)); - this.repairFiles = repairFiles; + this.rebuildFiles = rebuildFiles; this.rebuilder = rebuilder; this.rebuildOptions = rebuildOptions.Value; this.populateGrainIndexes = populateGrainIndexes; @@ -66,7 +66,7 @@ namespace Migrations if (rebuildOptions.AssetFiles) { - await repairFiles.RepairAsync(ct); + await rebuildFiles.RepairAsync(ct); } if (rebuildOptions.Contents) @@ -76,7 +76,7 @@ namespace Migrations if (rebuildOptions.Indexes) { - await populateGrainIndexes.UpdateAsync(); + await populateGrainIndexes.UpdateAsync(ct); } } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/RepairFiles.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/RepairFiles.cs deleted file mode 100644 index 55831f930..000000000 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/RepairFiles.cs +++ /dev/null @@ -1,75 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschraenkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System.IO; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using Squidex.Assets; -using Squidex.Domain.Apps.Events.Assets; -using Squidex.Infrastructure; -using Squidex.Infrastructure.EventSourcing; - -namespace Squidex.Domain.Apps.Entities.Assets -{ - public sealed class RepairFiles - { - private static readonly MemoryStream DummyStream = new MemoryStream(Encoding.UTF8.GetBytes("dummy")); - private readonly IAssetFileStore assetFileStore; - private readonly IEventStore eventStore; - private readonly IEventDataFormatter eventDataFormatter; - - public RepairFiles( - IAssetFileStore assetFileStore, - IEventStore eventStore, - IEventDataFormatter eventDataFormatter) - { - Guard.NotNull(assetFileStore, nameof(assetFileStore)); - Guard.NotNull(eventStore, nameof(eventStore)); - Guard.NotNull(eventDataFormatter, nameof(eventDataFormatter)); - - this.assetFileStore = assetFileStore; - this.eventStore = eventStore; - this.eventDataFormatter = eventDataFormatter; - } - - public async Task RepairAsync(CancellationToken ct = default) - { - await foreach (var storedEvent in eventStore.QueryAllAsync("^asset\\-", ct: ct)) - { - var @event = eventDataFormatter.ParseIfKnown(storedEvent); - - if (@event != null) - { - switch (@event.Payload) - { - case AssetCreated assetCreated: - await TryRepairAsync(assetCreated.AppId, assetCreated.AssetId, assetCreated.FileVersion, ct); - break; - case AssetUpdated assetUpdated: - await TryRepairAsync(assetUpdated.AppId, assetUpdated.AssetId, assetUpdated.FileVersion, ct); - break; - } - } - } - } - - private async Task TryRepairAsync(NamedId appId, DomainId id, long fileVersion, CancellationToken ct) - { - try - { - await assetFileStore.GetFileSizeAsync(appId.Id, id, fileVersion, ct); - } - catch (AssetNotFoundException) - { - DummyStream.Position = 0; - - await assetFileStore.UploadAsync(appId.Id, id, fileVersion, DummyStream, ct); - } - } - } -} diff --git a/backend/src/Squidex.Infrastructure/Migrations/IMigration.cs b/backend/src/Squidex.Infrastructure/Migrations/IMigration.cs index 143ab6ad5..760a14a2e 100644 --- a/backend/src/Squidex.Infrastructure/Migrations/IMigration.cs +++ b/backend/src/Squidex.Infrastructure/Migrations/IMigration.cs @@ -5,12 +5,13 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Threading; using System.Threading.Tasks; namespace Squidex.Infrastructure.Migrations { public interface IMigration { - Task UpdateAsync(); + Task UpdateAsync(CancellationToken ct); } } diff --git a/backend/src/Squidex.Infrastructure/Migrations/Migrator.cs b/backend/src/Squidex.Infrastructure/Migrations/Migrator.cs index abf53da1e..133353ba5 100644 --- a/backend/src/Squidex.Infrastructure/Migrations/Migrator.cs +++ b/backend/src/Squidex.Infrastructure/Migrations/Migrator.cs @@ -73,7 +73,7 @@ namespace Squidex.Infrastructure.Migrations .WriteProperty("status", "Completed") .WriteProperty("migrator", name))) { - await migration.UpdateAsync(); + await migration.UpdateAsync(ct); } } catch (Exception ex) diff --git a/backend/src/Squidex/Config/Domain/AssetServices.cs b/backend/src/Squidex/Config/Domain/AssetServices.cs index 1409b2342..2ce86e2d5 100644 --- a/backend/src/Squidex/Config/Domain/AssetServices.cs +++ b/backend/src/Squidex/Config/Domain/AssetServices.cs @@ -53,7 +53,7 @@ namespace Squidex.Config.Domain services.AddSingletonAs() .AsSelf(); - services.AddSingletonAs() + services.AddSingletonAs() .AsSelf(); services.AddTransientAs() diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/RepairFilesTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/RepairFilesTests.cs index 89c636fe6..a2e0c30ad 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/RepairFilesTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/RepairFilesTests.cs @@ -24,11 +24,11 @@ namespace Squidex.Domain.Apps.Entities.Assets private readonly IEventDataFormatter eventDataFormatter = A.Fake(); private readonly IAssetFileStore assetFileStore = A.Fake(); private readonly NamedId appId = NamedId.Of(DomainId.NewGuid(), "my-app"); - private readonly RepairFiles sut; + private readonly RebuildFiles sut; public RepairFilesTests() { - sut = new RepairFiles(assetFileStore, eventStore, eventDataFormatter); + sut = new RebuildFiles(assetFileStore, eventStore, eventDataFormatter); } [Fact] diff --git a/backend/tests/Squidex.Infrastructure.Tests/Migrations/MigratorTests.cs b/backend/tests/Squidex.Infrastructure.Tests/Migrations/MigratorTests.cs index 5b200cbd0..1afca3044 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/Migrations/MigratorTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/Migrations/MigratorTests.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using FakeItEasy; using Squidex.Log; @@ -103,13 +104,13 @@ namespace Squidex.Infrastructure.Migrations await sut.MigrateAsync(); - A.CallTo(() => migrator_0_1.UpdateAsync()) + A.CallTo(() => migrator_0_1.UpdateAsync(A._)) .MustHaveHappened(); - A.CallTo(() => migrator_1_2.UpdateAsync()) + A.CallTo(() => migrator_1_2.UpdateAsync(A._)) .MustHaveHappened(); - A.CallTo(() => migrator_2_3.UpdateAsync()) + A.CallTo(() => migrator_2_3.UpdateAsync(A._)) .MustHaveHappened(); A.CallTo(() => status.CompleteAsync(1)) @@ -136,13 +137,13 @@ namespace Squidex.Infrastructure.Migrations await sut.MigrateAsync(); - A.CallTo(() => migrator_0_1.UpdateAsync()) + A.CallTo(() => migrator_0_1.UpdateAsync(A._)) .MustHaveHappened(); - A.CallTo(() => migrator_1_2.UpdateAsync()) + A.CallTo(() => migrator_1_2.UpdateAsync(A._)) .MustHaveHappened(); - A.CallTo(() => migrator_2_3.UpdateAsync()) + A.CallTo(() => migrator_2_3.UpdateAsync(A._)) .MustHaveHappened(); A.CallTo(() => status.CompleteAsync(1)) @@ -167,17 +168,17 @@ namespace Squidex.Infrastructure.Migrations var sut = new Migrator(status, path, log); - A.CallTo(() => migrator_1_2.UpdateAsync()).Throws(new ArgumentException()); + A.CallTo(() => migrator_1_2.UpdateAsync(A._)).Throws(new ArgumentException()); await Assert.ThrowsAsync(() => sut.MigrateAsync()); - A.CallTo(() => migrator_0_1.UpdateAsync()) + A.CallTo(() => migrator_0_1.UpdateAsync(A._)) .MustHaveHappened(); - A.CallTo(() => migrator_1_2.UpdateAsync()) + A.CallTo(() => migrator_1_2.UpdateAsync(A._)) .MustHaveHappened(); - A.CallTo(() => migrator_2_3.UpdateAsync()) + A.CallTo(() => migrator_2_3.UpdateAsync(A._)) .MustNotHaveHappened(); A.CallTo(() => status.CompleteAsync(1)) @@ -201,7 +202,7 @@ namespace Squidex.Infrastructure.Migrations var ex = new InvalidOperationException(); - A.CallTo(() => migrator_0_1.UpdateAsync()) + A.CallTo(() => migrator_0_1.UpdateAsync(A._)) .Throws(ex); var sut = new Migrator(status, path, log); @@ -211,7 +212,7 @@ namespace Squidex.Infrastructure.Migrations A.CallTo(() => log.Log(SemanticLogLevel.Fatal, ex, A._!)) .MustHaveHappened(); - A.CallTo(() => migrator_1_2.UpdateAsync()) + A.CallTo(() => migrator_1_2.UpdateAsync(A._)) .MustNotHaveHappened(); } @@ -225,10 +226,10 @@ namespace Squidex.Infrastructure.Migrations await Task.WhenAll(Enumerable.Repeat(0, 10).Select(x => Task.Run(() => sut.MigrateAsync()))); - A.CallTo(() => migrator_0_1.UpdateAsync()) + A.CallTo(() => migrator_0_1.UpdateAsync(A._)) .MustHaveHappenedOnceExactly(); - A.CallTo(() => migrator_1_2.UpdateAsync()) + A.CallTo(() => migrator_1_2.UpdateAsync(A._)) .MustHaveHappenedOnceExactly(); }