Browse Source

Better cancellation for migration. (#418)

* Better cancellation for migration.
pull/421/head
Sebastian Stehle 7 years ago
committed by GitHub
parent
commit
fce1c9558b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/Squidex.Infrastructure/Migrations/Migrator.cs
  2. 1
      src/Squidex.Infrastructure/Orleans/GrainBootstrap.cs
  3. 2
      src/Squidex/Config/Startup/MigratorHost.cs
  4. 4
      tests/Squidex.Infrastructure.Tests/Migrations/MigratorTests.cs

5
src/Squidex.Infrastructure/Migrations/Migrator.cs

@ -7,6 +7,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Squidex.Infrastructure.Log; using Squidex.Infrastructure.Log;
@ -32,7 +33,7 @@ namespace Squidex.Infrastructure.Migrations
this.log = log; this.log = log;
} }
public async Task MigrateAsync() public async Task MigrateAsync(CancellationToken ct = default)
{ {
var version = 0; var version = 0;
@ -49,7 +50,7 @@ namespace Squidex.Infrastructure.Migrations
version = await migrationStatus.GetVersionAsync(); version = await migrationStatus.GetVersionAsync();
while (true) while (!ct.IsCancellationRequested)
{ {
var (newVersion, migrations) = migrationPath.GetNext(version); var (newVersion, migrations) = migrationPath.GetNext(version);

1
src/Squidex.Infrastructure/Orleans/GrainBootstrap.cs

@ -28,6 +28,7 @@ namespace Squidex.Infrastructure.Orleans
{ {
for (var i = 1; i <= NumTries; i++) for (var i = 1; i <= NumTries; i++)
{ {
ct.ThrowIfCancellationRequested();
try try
{ {
var grain = grainFactory.GetGrain<T>(SingleGrain.Id); var grain = grainFactory.GetGrain<T>(SingleGrain.Id);

2
src/Squidex/Config/Startup/MigratorHost.cs

@ -25,7 +25,7 @@ namespace Squidex.Config.Startup
protected override Task StartAsync(ISemanticLog log, CancellationToken ct) protected override Task StartAsync(ISemanticLog log, CancellationToken ct)
{ {
return migrator.MigrateAsync(); return migrator.MigrateAsync(ct);
} }
} }
} }

4
tests/Squidex.Infrastructure.Tests/Migrations/MigratorTests.cs

@ -108,7 +108,7 @@ namespace Squidex.Infrastructure.Migrations
A.CallTo(() => migrator_1_2.UpdateAsync()).Throws(new ArgumentException()); A.CallTo(() => migrator_1_2.UpdateAsync()).Throws(new ArgumentException());
await Assert.ThrowsAsync<MigrationFailedException>(sut.MigrateAsync); await Assert.ThrowsAsync<MigrationFailedException>(() => sut.MigrateAsync());
A.CallTo(() => migrator_0_1.UpdateAsync()).MustHaveHappened(); A.CallTo(() => migrator_0_1.UpdateAsync()).MustHaveHappened();
A.CallTo(() => migrator_1_2.UpdateAsync()).MustHaveHappened(); A.CallTo(() => migrator_1_2.UpdateAsync()).MustHaveHappened();
@ -147,7 +147,7 @@ namespace Squidex.Infrastructure.Migrations
var sut = new Migrator(new InMemoryStatus(), path, log) { LockWaitMs = 2 }; var sut = new Migrator(new InMemoryStatus(), path, log) { LockWaitMs = 2 };
await Task.WhenAll(Enumerable.Repeat(0, 10).Select(x => Task.Run(sut.MigrateAsync))); 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())
.MustHaveHappened(1, Times.Exactly); .MustHaveHappened(1, Times.Exactly);

Loading…
Cancel
Save