-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shell Pipeline in the Background (#14105)
- Loading branch information
Showing
13 changed files
with
219 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/OrchardCore/OrchardCore/Modules/Extensions/ShellPipelineExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Environment.Shell; | ||
using OrchardCore.Environment.Shell.Builders; | ||
using OrchardCore.Environment.Shell.Scope; | ||
|
||
namespace OrchardCore.Modules; | ||
|
||
public static class ShellPipelineExtensions | ||
{ | ||
private static readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphores = new(); | ||
|
||
/// <summary> | ||
/// Builds the tenant pipeline atomically. | ||
/// </summary> | ||
public static async Task BuildPipelineAsync(this ShellContext context) | ||
{ | ||
var semaphore = _semaphores.GetOrAdd(context.Settings.Name, _ => new SemaphoreSlim(1)); | ||
|
||
await semaphore.WaitAsync(); | ||
try | ||
{ | ||
if (!context.HasPipeline()) | ||
{ | ||
context.Pipeline = context.BuildPipeline(); | ||
} | ||
} | ||
finally | ||
{ | ||
semaphore.Release(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Builds the tenant pipeline. | ||
/// </summary> | ||
private static IShellPipeline BuildPipeline(this ShellContext context) | ||
{ | ||
var features = context.ServiceProvider.GetService<IServer>()?.Features; | ||
var builder = new ApplicationBuilder(context.ServiceProvider, features ?? new FeatureCollection()); | ||
var startupFilters = builder.ApplicationServices.GetService<IEnumerable<IStartupFilter>>(); | ||
|
||
Action<IApplicationBuilder> configure = builder => | ||
{ | ||
ConfigurePipeline(builder); | ||
}; | ||
|
||
foreach (var filter in startupFilters.Reverse()) | ||
{ | ||
configure = filter.Configure(configure); | ||
} | ||
|
||
configure(builder); | ||
|
||
var shellPipeline = new ShellRequestPipeline | ||
{ | ||
Next = builder.Build() | ||
}; | ||
|
||
return shellPipeline; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the tenant pipeline. | ||
/// </summary> | ||
private static void ConfigurePipeline(IApplicationBuilder builder) | ||
{ | ||
// 'IStartup' instances are ordered by module dependencies with a 'ConfigureOrder' of 0 by default. | ||
// 'OrderBy' performs a stable sort, so the order is preserved among equal 'ConfigureOrder' values. | ||
var startups = builder.ApplicationServices.GetServices<IStartup>().OrderBy(s => s.ConfigureOrder); | ||
|
||
builder.UseRouting().UseEndpoints(routes => | ||
{ | ||
foreach (var startup in startups) | ||
{ | ||
startup.Configure(builder, routes, ShellScope.Services); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.