Browse Source

Migration improved.

pull/349/head
Sebastian Stehle 7 years ago
parent
commit
e482bf93fe
  1. 9
      src/Squidex.Infrastructure/DependencyInjection/DependencyInjectionExtensions.cs
  2. 17
      src/Squidex.Infrastructure/IBackgroundProcess.cs
  3. 6
      src/Squidex.Infrastructure/Orleans/GrainBootstrap.cs
  4. 14
      src/Squidex/Config/Domain/EntitiesServices.cs
  5. 9
      src/Squidex/Config/Domain/InfrastructureServices.cs
  6. 8
      src/Squidex/Config/Orleans/OrleansServices.cs
  7. 38
      src/Squidex/Config/Startup/BackgroundHost.cs
  8. 1
      src/Squidex/WebStartup.cs
  9. 4
      tests/Squidex.Infrastructure.Tests/Orleans/BootstrapTests.cs
  10. 5
      tools/Migrate_01/Migrations/MongoDb/RestructureContentCollection.cs

9
src/Squidex.Infrastructure/DependencyInjection/DependencyInjectionExtensions.cs

@ -83,10 +83,17 @@ namespace Microsoft.Extensions.DependencyInjection
private static void RegisterDefaults<T>(IServiceCollection services) where T : class
{
if (typeof(T).GetInterfaces().Contains(typeof(IInitializable)))
var interfaces = typeof(T).GetInterfaces();
if (interfaces.Contains(typeof(IInitializable)))
{
services.AddSingleton(typeof(IInitializable), c => c.GetRequiredService<T>());
}
if (interfaces.Contains(typeof(IBackgroundProcess)))
{
services.AddSingleton(typeof(IBackgroundProcess), c => c.GetRequiredService<T>());
}
}
}
}

17
src/Squidex.Infrastructure/IBackgroundProcess.cs

@ -0,0 +1,17 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Threading;
using System.Threading.Tasks;
namespace Squidex.Infrastructure
{
public interface IBackgroundProcess
{
Task StartAsync(CancellationToken ct);
}
}

6
src/Squidex.Infrastructure/Orleans/Bootstrap.cs → src/Squidex.Infrastructure/Orleans/GrainBootstrap.cs

@ -12,19 +12,19 @@ using Orleans.Runtime;
namespace Squidex.Infrastructure.Orleans
{
public sealed class Bootstrap<T> : IStartupTask where T : IBackgroundGrain
public sealed class GrainBootstrap<T> : IBackgroundProcess where T : IBackgroundGrain
{
private const int NumTries = 10;
private readonly IGrainFactory grainFactory;
public Bootstrap(IGrainFactory grainFactory)
public GrainBootstrap(IGrainFactory grainFactory)
{
Guard.NotNull(grainFactory, nameof(grainFactory));
this.grainFactory = grainFactory;
}
public async Task Execute(CancellationToken cancellationToken)
public async Task StartAsync(CancellationToken ct)
{
for (var i = 1; i <= NumTries; i++)
{

14
src/Squidex/Config/Domain/EntitiesServices.cs

@ -44,7 +44,9 @@ using Squidex.Domain.Apps.Entities.Tags;
using Squidex.Infrastructure.Assets;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.EventSourcing.Grains;
using Squidex.Infrastructure.Migrations;
using Squidex.Infrastructure.Orleans;
using Squidex.Pipeline;
using Squidex.Pipeline.CommandMiddlewares;
@ -63,12 +65,10 @@ namespace Squidex.Config.Domain
.As<IGraphQLUrlGenerator>().As<IRuleUrlGenerator>().As<IAssetUrlGenerator>();
services.AddSingletonAs<HistoryService>()
.As<IEventConsumer>()
.As<IHistoryService>();
.As<IEventConsumer>().As<IHistoryService>();
services.AddSingletonAs<AssetUsageTracker>()
.As<IEventConsumer>()
.As<IAssetUsageTracker>();
.As<IEventConsumer>().As<IAssetUsageTracker>();
services.AddSingletonAs<CachingGraphQLService>()
.As<IGraphQLService>();
@ -118,6 +118,12 @@ namespace Squidex.Config.Domain
services.AddSingletonAs<JintScriptEngine>()
.AsOptional<IScriptEngine>();
services.AddSingletonAs<GrainBootstrap<IContentSchedulerGrain>>()
.AsSelf();
services.AddSingletonAs<GrainBootstrap<IRuleDequeuerGrain>>()
.AsSelf();
services.AddCommandPipeline();
services.AddBackupHandlers();

9
src/Squidex/Config/Domain/InfrastructureServices.cs

@ -14,9 +14,12 @@ using Microsoft.Extensions.DependencyInjection;
using NodaTime;
using Squidex.Areas.Api.Controllers.News.Service;
using Squidex.Domain.Apps.Entities.Apps.Diagnostics;
using Squidex.Domain.Apps.Entities.Rules.UsageTracking;
using Squidex.Domain.Users;
using Squidex.Infrastructure.Caching;
using Squidex.Infrastructure.Diagnostics;
using Squidex.Infrastructure.EventSourcing.Grains;
using Squidex.Infrastructure.Orleans;
using Squidex.Infrastructure.Translations;
using Squidex.Infrastructure.UsageTracking;
using Squidex.Shared.Users;
@ -48,6 +51,12 @@ namespace Squidex.Config.Domain
services.AddSingletonAs<BackgroundUsageTracker>()
.AsSelf();
services.AddSingletonAs<GrainBootstrap<IEventConsumerManagerGrain>>()
.AsSelf();
services.AddSingletonAs<GrainBootstrap<IUsageTrackerGrain>>()
.AsSelf();
services.AddSingletonAs<DeepLTranslator>()
.As<ITranslator>();

8
src/Squidex/Config/Orleans/OrleansServices.cs

@ -12,10 +12,6 @@ using Microsoft.Extensions.DependencyInjection;
using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;
using Squidex.Domain.Apps.Entities.Contents;
using Squidex.Domain.Apps.Entities.Rules;
using Squidex.Domain.Apps.Entities.Rules.UsageTracking;
using Squidex.Infrastructure.EventSourcing.Grains;
using Squidex.Infrastructure.Orleans;
namespace Squidex.Config.Orleans
@ -45,10 +41,6 @@ namespace Squidex.Config.Orleans
.UseDashboardEx()
.EnableDirectClient()
.AddIncomingGrainCallFilter<LocalCacheFilter>()
.AddStartupTask<Bootstrap<IContentSchedulerGrain>>()
.AddStartupTask<Bootstrap<IEventConsumerManagerGrain>>()
.AddStartupTask<Bootstrap<IRuleDequeuerGrain>>()
.AddStartupTask<Bootstrap<IUsageTrackerGrain>>()
.ConfigureApplicationParts(builder =>
{
builder.AddMyParts();

38
src/Squidex/Config/Startup/BackgroundHost.cs

@ -0,0 +1,38 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Log;
namespace Squidex.Config.Startup
{
public sealed class BackgroundHost : SafeHostedService
{
private readonly IEnumerable<IBackgroundProcess> targets;
public BackgroundHost(IEnumerable<IBackgroundProcess> targets, IApplicationLifetime lifetime, ISemanticLog log)
: base(lifetime, log)
{
this.targets = targets;
}
protected override async Task StartAsync(ISemanticLog log, CancellationToken ct)
{
foreach (var target in targets.Distinct())
{
await target.StartAsync(ct);
log.LogInformation(w => w.WriteProperty("backgroundSystem", target.GetType().Name));
}
}
}
}

1
src/Squidex/WebStartup.cs

@ -103,6 +103,7 @@ namespace Squidex
var provider = services.AddAndBuildOrleans(configuration, afterServices =>
{
afterServices.AddHostedService<MigratorHost>();
afterServices.AddHostedService<BackgroundHost>();
});
return provider;

4
tests/Squidex.Infrastructure.Tests/Orleans/BootstrapTests.cs

@ -19,13 +19,13 @@ namespace Squidex.Infrastructure.Orleans
public class BootstrapTests
{
private readonly IBackgroundGrain grain = A.Fake<IBackgroundGrain>();
private readonly Bootstrap<IBackgroundGrain> sut;
private readonly GrainBootstrap<IBackgroundGrain> sut;
public BootstrapTests()
{
var factory = A.Fake<IGrainFactory>();
sut = new Bootstrap<IBackgroundGrain>(factory);
sut = new GrainBootstrap<IBackgroundGrain>(factory);
A.CallTo(() => factory.GetGrain<IBackgroundGrain>("Default", null))
.Returns(grain);

5
tools/Migrate_01/Migrations/MongoDb/RestructureContentCollection.cs

@ -29,8 +29,11 @@ namespace Migrate_01.Migrations.MongoDb
await contentDatabase.DropCollectionAsync("State_Contents");
await contentDatabase.DropCollectionAsync("State_Content_Published");
await contentDatabase.RenameCollectionAsync("State_Content_Draft", "State_Contents");
}
var collection = contentDatabase.GetCollection<BsonDocument>("State_Content");
if (await contentDatabase.CollectionExistsAsync("State_Contents"))
{
var collection = contentDatabase.GetCollection<BsonDocument>("State_Contents");
await collection.UpdateManyAsync(new BsonDocument(), Builders<BsonDocument>.Update.Unset("dt"));
}

Loading…
Cancel
Save