Browse Source

Avoid problematic startups.

pull/342/head
Sebastian Stehle 7 years ago
parent
commit
e97d38148d
  1. 30
      src/Squidex.Domain.Apps.Entities/Apps/Templates/AlwaysCreateClientCommandMiddleware.cs
  2. 8
      src/Squidex.Domain.Apps.Entities/Apps/Templates/CreateBlogCommandMiddleware.cs
  3. 8
      src/Squidex.Domain.Apps.Entities/Apps/Templates/CreateIdentityCommandMiddleware.cs
  4. 8
      src/Squidex.Domain.Apps.Entities/Apps/Templates/CreateProfileCommandMiddleware.cs
  5. 3
      src/Squidex/Config/Domain/EntitiesServices.cs
  6. 35
      src/Squidex/Config/Domain/SystemExtensions.cs
  7. 7
      src/Squidex/Config/Orleans/SiloWrapper.cs
  8. 12
      src/Squidex/appsettings.json

30
src/Squidex.Domain.Apps.Entities/Apps/Templates/AlwaysCreateClientCommandMiddleware.cs

@ -0,0 +1,30 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Threading.Tasks;
using Squidex.Domain.Apps.Entities.Apps.Commands;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.Tasks;
namespace Squidex.Domain.Apps.Entities.Apps.Templates
{
public sealed class AlwaysCreateClientCommandMiddleware : ICommandMiddleware
{
public Task HandleAsync(CommandContext context, Func<Task> next)
{
if (context.IsCompleted && context.Command is CreateApp createApp)
{
var command = new AttachClient { Id = "default", AppId = createApp.AppId };
context.CommandBus.PublishAsync(command).Forget();
}
return next();
}
}
}

8
src/Squidex.Domain.Apps.Entities/Apps/Templates/CreateBlogCommandMiddleware.cs

@ -39,8 +39,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Templates
await Task.WhenAll( await Task.WhenAll(
CreatePagesAsync(publish), CreatePagesAsync(publish),
CreatePostsAsync(publish), CreatePostsAsync(publish));
CreateClientAsync(publish, appId.Id));
} }
await next(); await next();
@ -51,11 +50,6 @@ namespace Squidex.Domain.Apps.Entities.Apps.Templates
return string.Equals(createApp.Template, TemplateName, StringComparison.OrdinalIgnoreCase); return string.Equals(createApp.Template, TemplateName, StringComparison.OrdinalIgnoreCase);
} }
private static async Task CreateClientAsync(Func<ICommand, Task> publish, Guid appId)
{
await publish(new AttachClient { Id = "sample-client", AppId = appId });
}
private static async Task CreatePostsAsync(Func<ICommand, Task> publish) private static async Task CreatePostsAsync(Func<ICommand, Task> publish)
{ {
var postsId = await CreatePostsSchemaAsync(publish); var postsId = await CreatePostsSchemaAsync(publish);

8
src/Squidex.Domain.Apps.Entities/Apps/Templates/CreateIdentityCommandMiddleware.cs

@ -53,8 +53,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Templates
CreateClientsSchemaAsync(publish), CreateClientsSchemaAsync(publish),
CreateIdentityResourcesSchemaAsync(publish), CreateIdentityResourcesSchemaAsync(publish),
CreateSettingsSchemaAsync(publish), CreateSettingsSchemaAsync(publish),
CreateUsersSchemaAsync(publish), CreateUsersSchemaAsync(publish));
CreateClientAsync(publish, appId.Id));
} }
await next(); await next();
@ -65,11 +64,6 @@ namespace Squidex.Domain.Apps.Entities.Apps.Templates
return string.Equals(createApp.Template, TemplateName, StringComparison.OrdinalIgnoreCase); return string.Equals(createApp.Template, TemplateName, StringComparison.OrdinalIgnoreCase);
} }
private static async Task CreateClientAsync(Func<ICommand, Task> publish, Guid appId)
{
await publish(new AttachClient { Id = "default", AppId = appId });
}
private static async Task<NamedId<Guid>> CreateAuthenticationSchemeSchemaAsync(Func<ICommand, Task> publish) private static async Task<NamedId<Guid>> CreateAuthenticationSchemeSchemaAsync(Func<ICommand, Task> publish)
{ {
var schema = var schema =

8
src/Squidex.Domain.Apps.Entities/Apps/Templates/CreateProfileCommandMiddleware.cs

@ -42,8 +42,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Templates
CreateExperienceSchemaAsync(publish), CreateExperienceSchemaAsync(publish),
CreateProjectsSchemaAsync(publish), CreateProjectsSchemaAsync(publish),
CreatePublicationsSchemaAsync(publish), CreatePublicationsSchemaAsync(publish),
CreateSkillsSchemaAsync(publish), CreateSkillsSchemaAsync(publish));
CreateClientAsync(publish, appId.Id));
} }
await next(); await next();
@ -54,11 +53,6 @@ namespace Squidex.Domain.Apps.Entities.Apps.Templates
return string.Equals(createApp.Template, TemplateName, StringComparison.OrdinalIgnoreCase); return string.Equals(createApp.Template, TemplateName, StringComparison.OrdinalIgnoreCase);
} }
private static async Task CreateClientAsync(Func<ICommand, Task> publish, Guid appId)
{
await publish(new AttachClient { Id = "sample-client", AppId = appId });
}
private static async Task CreateBasicsAsync(Func<ICommand, Task> publish) private static async Task CreateBasicsAsync(Func<ICommand, Task> publish)
{ {
var postsId = await CreateBasicsSchemaAsync(publish); var postsId = await CreateBasicsSchemaAsync(publish);

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

@ -196,6 +196,9 @@ namespace Squidex.Config.Domain
services.AddSingletonAs<SingletonCommandMiddleware>() services.AddSingletonAs<SingletonCommandMiddleware>()
.As<ICommandMiddleware>(); .As<ICommandMiddleware>();
services.AddSingletonAs<AlwaysCreateClientCommandMiddleware>()
.As<ICommandMiddleware>();
services.AddSingletonAs<CreateBlogCommandMiddleware>() services.AddSingletonAs<CreateBlogCommandMiddleware>()
.As<ICommandMiddleware>(); .As<ICommandMiddleware>();

35
src/Squidex/Config/Domain/SystemExtensions.cs

@ -20,22 +20,31 @@ namespace Squidex.Config.Domain
public sealed class InitializeHostedService : IHostedService public sealed class InitializeHostedService : IHostedService
{ {
private readonly IEnumerable<IInitializable> targets; private readonly IEnumerable<IInitializable> targets;
private readonly IApplicationLifetime lifetime;
private readonly ISemanticLog log; private readonly ISemanticLog log;
public InitializeHostedService(IEnumerable<IInitializable> targets, ISemanticLog log) public InitializeHostedService(IEnumerable<IInitializable> targets, IApplicationLifetime lifetime, ISemanticLog log)
{ {
this.targets = targets; this.targets = targets;
this.lifetime = lifetime;
this.log = log; this.log = log;
} }
public async Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
{ {
foreach (var target in targets) try
{ {
await target.InitializeAsync(cancellationToken); foreach (var target in targets)
{
await target.InitializeAsync(cancellationToken);
log.LogInformation(w => w.WriteProperty("initializedSystem", target.GetType().Name)); log.LogInformation(w => w.WriteProperty("initializedSystem", target.GetType().Name));
}
}
catch
{
lifetime.StopApplication();
throw;
} }
} }
@ -47,16 +56,26 @@ namespace Squidex.Config.Domain
public sealed class MigratorHostedService : IHostedService public sealed class MigratorHostedService : IHostedService
{ {
private readonly IApplicationLifetime lifetime;
private readonly Migrator migrator; private readonly Migrator migrator;
public MigratorHostedService(Migrator migrator) public MigratorHostedService(IApplicationLifetime lifetime, Migrator migrator)
{ {
this.lifetime = lifetime;
this.migrator = migrator; this.migrator = migrator;
} }
public Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
{ {
return migrator.MigrateAsync(); try
{
await migrator.MigrateAsync();
}
catch
{
lifetime.StopApplication();
throw;
}
} }
public Task StopAsync(CancellationToken cancellationToken) public Task StopAsync(CancellationToken cancellationToken)

7
src/Squidex/Config/Orleans/SiloWrapper.cs

@ -30,6 +30,7 @@ namespace Squidex.Config.Orleans
{ {
private readonly Lazy<ISiloHost> lazySilo; private readonly Lazy<ISiloHost> lazySilo;
private readonly ISemanticLog log; private readonly ISemanticLog log;
private readonly IApplicationLifetime lifetime;
private bool isStopping; private bool isStopping;
internal sealed class Source : IConfigurationSource internal sealed class Source : IConfigurationSource
@ -54,6 +55,7 @@ namespace Squidex.Config.Orleans
public SiloWrapper(IConfiguration config, ISemanticLog log, IApplicationLifetime lifetime) public SiloWrapper(IConfiguration config, ISemanticLog log, IApplicationLifetime lifetime)
{ {
this.lifetime = lifetime;
this.log = log; this.log = log;
lazySilo = new Lazy<ISiloHost>(() => lazySilo = new Lazy<ISiloHost>(() =>
@ -158,6 +160,11 @@ namespace Squidex.Config.Orleans
{ {
await lazySilo.Value.StartAsync(cancellationToken); await lazySilo.Value.StartAsync(cancellationToken);
} }
catch
{
lifetime.StopApplication();
throw;
}
finally finally
{ {
var elapsedMs = watch.Stop(); var elapsedMs = watch.Stop();

12
src/Squidex/appsettings.json

@ -68,8 +68,8 @@
/* /*
* The maximum number of megabyte that the process can consume until it is marked as not healthy. * The maximum number of megabyte that the process can consume until it is marked as not healthy.
*/ */
"threshold": 4096 "threshold": 4096
} }
}, },
"contentsController": { "contentsController": {
@ -78,13 +78,13 @@
* *
* Nginx Has problems with long headers. It might make sense to disable this feature if you do not use a CDN. * Nginx Has problems with long headers. It might make sense to disable this feature if you do not use a CDN.
*/ */
"enableSurrogateKeys": true, "enableSurrogateKeys": true,
/* /*
* Restrict the surrogate keys to results that have less than 200 items. * Restrict the surrogate keys to results that have less than 200 items.
*/ */
"maxItemsForSurrogateKeys": 200 "maxItemsForSurrogateKeys": 200
}, },
"content": { "content": {
/* /*

Loading…
Cancel
Save