Skip to content

Commit

Permalink
Check DistributedShellMarkerService
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkech committed Oct 9, 2023
1 parent 62f9ab2 commit 9d406dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public DistributedContext(ShellContext context)

public IDistributedCache DistributedCache { get; }

public bool HasIsolatedConfiguration { get; internal set; }

public DistributedContext Acquire()
{
// Don't acquire a released context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OrchardCore.Environment.Shell.Builders;
Expand Down Expand Up @@ -43,7 +44,7 @@ internal class DistributedShellHostedService : BackgroundService
private DistributedContext _context;

private DateTime _busyStartTime;
private bool _terminated;
private bool _initialized;

public DistributedShellHostedService(
IShellHost shellHost,
Expand All @@ -58,7 +59,6 @@ public DistributedShellHostedService(
_shellRemovingManager = shellRemovingManager;
_logger = logger;

shellHost.LoadingAsync += LoadingAsync;
shellHost.ReleasingAsync += ReleasingAsync;
shellHost.ReloadingAsync += ReloadingAsync;
shellHost.RemovingAsync += RemovingAsync;
Expand Down Expand Up @@ -139,6 +139,17 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
continue;
}

// Initialize the service if not yet done by using the distributed cache.
try
{
await EnsureInitializedAsync(distributedCache);
}
catch (Exception ex) when (!ex.IsFatal())
{
// Get the next idle time before retrying to use the distributed cache.
idleTime = NextIdleTimeBeforeRetry(idleTime, ex);
}

// Try to retrieve the tenant changed global identifier from the distributed cache.
string shellChangedId;
try
Expand Down Expand Up @@ -307,7 +318,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
}

_terminated = true;
_initialized = false;

_shellHost.ReleasingAsync -= ReleasingAsync;
_shellHost.ReloadingAsync -= ReloadingAsync;
_shellHost.RemovingAsync -= RemovingAsync;

if (_context is not null)
{
Expand All @@ -318,41 +333,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}

/// <summary>
/// Called before loading all tenants to initialize the local shell identifiers from the distributed cache.
/// Initialize the local shell identifiers from the distributed cache.
/// </summary>
public async Task LoadingAsync()
public async Task EnsureInitializedAsync(IDistributedCache distributedCache)
{
if (_terminated)
{
return;
}

// Load a first isolated configuration as the default context it is not yet initialized.
var defaultSettings = (await _shellSettingsManager
.LoadSettingsAsync(ShellSettings.DefaultShellName))
.AsDisposable();

// If there is no default tenant or it is not running, nothing to do.
if (!defaultSettings.IsRunning())
{
defaultSettings.Dispose();
return;
}

// Create a distributed context based on the first isolated configuration.
var context = _context = await CreateDistributedContextAsync(defaultSettings);
if (context is null)
{
defaultSettings.Dispose();
return;
}

// Mark the context as using the first isolated configuration.
context.HasIsolatedConfiguration = true;

// If the required distributed features are not enabled, nothing to do.
var distributedCache = context.DistributedCache;
if (distributedCache is null)
if (_initialized)
{
return;
}
Expand Down Expand Up @@ -389,10 +374,12 @@ public async Task LoadingAsync()
// Keep in sync the tenant global identifiers.
_shellChangedId = shellChangedId;
_shellCountChangedId = shellCountChangedId;

_initialized = true;
}
catch (Exception ex) when (!ex.IsFatal())
{
_logger.LogError(ex, "Unable to read the distributed cache before loading all tenants.");
_logger.LogError(ex, "Unable to read the distributed cache while initializing the tenant identifiers.");
}
}

Expand All @@ -401,7 +388,7 @@ public async Task LoadingAsync()
/// </summary>
public async Task ReleasingAsync(string name)
{
if (_terminated)
if (!_initialized)
{
return;
}
Expand Down Expand Up @@ -452,7 +439,7 @@ public async Task ReleasingAsync(string name)
/// </summary>
public async Task ReloadingAsync(string name)
{
if (_terminated)
if (!_initialized)
{
return;
}
Expand All @@ -467,13 +454,6 @@ public async Task ReloadingAsync(string name)
// Acquire the distributed context or create a new one if not yet built.
await using var context = await AcquireOrCreateDistributedContextAsync(defaultContext);

// If the context still use the first isolated configuration.
if (context is not null && context.HasIsolatedConfiguration)
{
// Reset the serial number so that a new context will be built.
context.Context.Blueprint.Descriptor.SerialNumber = 0;
}

// If the required distributed features are not enabled, nothing to do.
var distributedCache = context?.DistributedCache;
if (distributedCache is null)
Expand Down Expand Up @@ -518,7 +498,7 @@ public async Task ReloadingAsync(string name)
public async Task RemovingAsync(string name)
{
// The 'Default' tenant can't be removed.
if (_terminated || name.IsDefaultShellName())
if (!_initialized || name.IsDefaultShellName())
{
return;
}
Expand Down Expand Up @@ -562,14 +542,21 @@ public async Task RemovingAsync(string name)
}
}

private static string ReleaseIdKey(string name) => name + ReleaseIdKeySuffix;
private static string ReloadIdKey(string name) => name + ReloadIdKeySuffix;
private static string ReleaseIdKey(string name) => $"{name}{ReleaseIdKeySuffix}";
private static string ReloadIdKey(string name) => $"{name}{ReloadIdKeySuffix}";

/// <summary>
/// Creates a distributed context based on the default tenant context.
/// </summary>
private async Task<DistributedContext> CreateDistributedContextAsync(ShellContext defaultContext)
{
// Check if the distributed shell feature is enabled.
if (!HasDistributedShellFeature(defaultContext))
{
// Nothing to create.
return null;
}

// Get the default tenant descriptor.
var descriptor = await GetDefaultShellDescriptorAsync(defaultContext);

Expand Down Expand Up @@ -601,21 +588,6 @@ private async Task<DistributedContext> CreateDistributedContextAsync(ShellSettin
}
}

/// <summary>
/// Creates a distributed context based on the default tenant settings.
/// </summary>
private async Task<DistributedContext> CreateDistributedContextAsync(ShellSettings defaultSettings)
{
try
{
return new DistributedContext(await _shellContextFactory.CreateShellContextAsync(defaultSettings));
}
catch
{
return null;
}
}

/// <summary>
/// Gets the default tenant descriptor.
/// </summary>
Expand Down Expand Up @@ -679,6 +651,13 @@ private async Task<DistributedContext> GetOrCreateDistributedContextAsync(ShellC
/// </summary>
private async Task<DistributedContext> ReuseOrCreateDistributedContextAsync(ShellContext defaultContext)
{
// Check if the distributed shell feature is enabled.
if (!HasDistributedShellFeature(defaultContext))
{
// Nothing to create.
return null;
}

// If no context.
if (_context is null)
{
Expand All @@ -698,7 +677,7 @@ private async Task<DistributedContext> ReuseOrCreateDistributedContextAsync(Shel

// If no descriptor.
if (descriptor is null)
{
{
// Nothing to create.
return null;
}
Expand Down Expand Up @@ -731,6 +710,25 @@ private Task<DistributedContext> AcquireOrCreateDistributedContextAsync(ShellCon
return Task.FromResult(distributedContext);
}

/// <summary>
/// Whether the default context has the distributed shell feature enabled or not.
/// </summary>
private static bool HasDistributedShellFeature(ShellContext defaultContext)
{
try
{
if (defaultContext.ServiceProvider?.GetService<DistributedShellMarkerService>() is not null)
{
return true;
}
}
catch
{
}

return false;
}

/// <summary>
/// Gets the next idle time before retrying to read the distributed cache.
/// </summary>
Expand Down

0 comments on commit 9d406dd

Please sign in to comment.