Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the DefaultShellReleaseManager #15960

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OrchardCore.Environment.Shell.Models;

public class ShellReleaseRequestContext
{
public const string ShellScopeKey = nameof(ShellReleaseRequestContext);

public bool Release { get; set; }
}
42 changes: 42 additions & 0 deletions src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Logging;
using OrchardCore.Environment.Cache;
using OrchardCore.Environment.Shell.Builders;
using OrchardCore.Environment.Shell.Models;
using OrchardCore.Modules;

namespace OrchardCore.Environment.Shell.Scope
Expand Down Expand Up @@ -128,6 +129,42 @@ public static T Get<T>(object key)
return current._items.TryGetValue(key, out var value) ? value is T item ? item : default : default;
}

/// <summary>
/// Gets a shared item of a given type from the current shell scope.
/// </summary>
public static bool TryGetValue(object key, out object value)
{
var current = Current;

if (current == null)
{
value = default;

return false;
}

current._items ??= [];

return current._items.TryGetValue(key, out value);
}

/// <summary>
/// Gets a shared item of a given type from the current shell scope.
/// </summary>
public static bool TryGetValue<T>(object key, out T value)
{
if (TryGetValue(key, out var existingValue) && existingValue is T item)
{
value = item;

return true;
}

value = default;

return false;
}

/// <summary>
/// Gets (or creates) a shared item of a given type from the current shell scope.
/// </summary>
Expand Down Expand Up @@ -498,6 +535,11 @@ await scope.UsingAsync(async scope =>
activateShell: false);
}
}

if (TryGetValue<ShellReleaseRequestContext>(ShellReleaseRequestContext.ShellScopeKey, out var context) && context.Release)
{
await ShellContext.ReleaseAsync();
}
}

/// <summary>
Expand Down
38 changes: 11 additions & 27 deletions src/OrchardCore/OrchardCore/Shell/DefaultShellReleaseManager.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Environment.Shell.Models;
using OrchardCore.Environment.Shell.Scope;

namespace OrchardCore.Environment.Shell;

public class DefaultShellReleaseManager : IShellReleaseManager
{
private bool _release;
private bool _deferredTaskAdded;

public void SuspendReleaseRequest()
{
_release = false;
if (ShellScope.TryGetValue<ShellReleaseRequestContext>(ShellReleaseRequestContext.ShellScopeKey, out var context))
{
context.Release = false;

ShellScope.Set(ShellReleaseRequestContext.ShellScopeKey, context);
}
}

public void RequestRelease()
{
_release = true;

if (_deferredTaskAdded)
{
return;
}

_deferredTaskAdded = true;

ShellScope.AddDeferredTask(async scope =>
{
if (!_release)
ShellScope.Set(ShellReleaseRequestContext.ShellScopeKey,
new ShellReleaseRequestContext()
{
return;
}

_release = false;

var shellHost = scope.ServiceProvider.GetRequiredService<IShellHost>();
var shellSettings = scope.ServiceProvider.GetRequiredService<ShellSettings>();

await shellHost.ReleaseShellContextAsync(shellSettings);
});
Release = true
});
}
}