Skip to content

Commit

Permalink
Merge pull request #37 from Lombiq/issue/NEST-359
Browse files Browse the repository at this point in the history
NEST-359: Changing DisableIdleTenants feature name and some clean-up
  • Loading branch information
wAsnk authored Oct 4, 2022
2 parents fb84f20 + 5e8b7cb commit ade31a8
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void SetMaxIdleMinutesAndLoggingForUITest(
(_, argumentsBuilder) =>
{
argumentsBuilder
.Add("--OrchardCore:Lombiq_Hosting_Tenants_IdleTenantManagement:IdleMinutesOptions:MaxIdleMinutes")
.Add("--OrchardCore:Lombiq_Hosting_Tenants_IdleTenantManagement:IdleShutdownOptions:MaxIdleMinutes")
.Add("1");

argumentsBuilder
Expand All @@ -26,5 +26,5 @@ public static void SetMaxIdleMinutesAndLoggingForUITest(
public static readonly Func<IWebApplicationInstance, Task> AssertAppLogsWithIdleCheckAsync =
async webApplicationInstance =>
(await webApplicationInstance.GetLogOutputAsync())
.ShouldContain("Shutting down tenant \"Default\" because of idle timeout");
.ShouldContain("Shutting down tenant \"Default\" because of idle timeout.");
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Lombiq.Hosting.Tenants.IdleTenantManagement.Constants;
namespace Lombiq.Hosting.Tenants.IdleTenantManagement.Constants;

public static class FeatureNames
{
private const string Module = "Lombiq.Hosting.Tenants.IdleTenantManagement";

public const string DisableIdleTenants = Module + "." + nameof(DisableIdleTenants);
public const string ShutDownIdleTenants = Module + "." + nameof(ShutDownIdleTenants);
}
6 changes: 3 additions & 3 deletions Lombiq.Hosting.Tenants.IdleTenantManagement/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
)]

[assembly: Feature(
Id = DisableIdleTenants,
Name = "Lombiq Hosting - Idle Tenant Management - Disable Idle Tenants",
Description = "Disable tenants not receiving requests after a configured amount of time.",
Id = ShutDownIdleTenants,
Name = "Lombiq Hosting - Idle Tenant Management - Shut Down Idle Tenants",
Description = "Shut down tenants not receiving requests after a configured amount of time to conserve computing resources.",
Category = "Hosting",
Priority = "9999",
IsAlwaysEnabled = true
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Lombiq.Hosting.Tenants.IdleTenantManagement.Models;

public class IdleShutdownOptions
{
/// <summary>
/// Gets or sets the maximum amount of idle time before the tenant shuts down.
/// </summary>
public int MaxIdleMinutes { get; set; } = 30;
}
18 changes: 16 additions & 2 deletions Lombiq.Hosting.Tenants.IdleTenantManagement/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ This feature is available on any tenant. The maximum idle time can be set in the
```csharp
public void ConfigureServices(IServiceCollection services) =>
services.AddOrchardCms(
builder => builder.AddTenantFeatures(Lombiq.Hosting.Tenants.IdleTenantManagement.Constants.FeatureNames.DisableIdleTenants));
builder => builder.AddTenantFeatures(Lombiq.Hosting.Tenants.IdleTenantManagement.Constants.FeatureNames.ShutDownIdleTenants));
```

**NOTE:** This way the feature will also be enabled on the Default tenant. You may not want to use this feature on the default tenant.
**NOTE:** This way the feature will also be enabled on the Default tenant. You may not want to use this feature on the default tenant, so configure the corresponding `MaxIdleMinutes` as `0`. You can do this in an _appsettings.json_ file like this (see [the docs](https://docs.orchardcore.net/en/latest/docs/reference/core/Configuration/) for more details):

```json
{
"OrchardCore": {
"Default": {
"Lombiq_Hosting_Tenants_IdleTenantManagement": {
"IdleShutdownOptions": {
"MaxIdleMinutes": 0
}
}
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@

namespace Lombiq.Hosting.Tenants.IdleTenantManagement.Services;

[BackgroundTask(Schedule = "* * * * *", Description = "Disable Idle Tenants.")]
[BackgroundTask(Schedule = "* * * * *", Description = "Shut down idle tenants.")]
public class IdleShutdownTask : IBackgroundTask
{
public async Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
{
var clock = serviceProvider.GetService<IClock>();
var shellSettings = serviceProvider.GetService<ShellSettings>();
var logger = serviceProvider.GetService<ILogger<IdleShutdownTask>>();
var lastActiveTimeAccessor = serviceProvider.GetService<ILastActiveTimeAccessor>();
var shellSettingsManager = serviceProvider.GetService<IShellSettingsManager>();
var shellHost = serviceProvider.GetService<IShellHost>();
var options = serviceProvider.GetService<IOptions<IdleMinutesOptions>>();

var options = serviceProvider.GetService<IOptions<IdleShutdownOptions>>();
var maxIdleMinutes = options.Value.MaxIdleMinutes;

if (maxIdleMinutes <= 0) return;

if (lastActiveTimeAccessor.LastActiveDateTimeUtc.AddMinutes(maxIdleMinutes) <= clock?.UtcNow)
{
var shellSettings = serviceProvider.GetService<ShellSettings>();
var logger = serviceProvider.GetService<ILogger<IdleShutdownTask>>();

logger?.LogInformation("Shutting down tenant \"{ShellName}\" because of idle timeout.", shellSettings?.Name);

try
Expand All @@ -44,9 +44,10 @@ public async Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToke
shellSettings?.Name);

// If the ReleaseShellContextAsync() fails (which can happen with a DB error: then the transaction
// commits triggered by the dispose will fail) then while the tenant is unavailable the shell is
// still active in a failed state. So first we need to correctly start the tenant, then shut it
// down for good.
// commits triggered by the dispose will fail) then while the tenant is unavailable the shell is still
// active in a failed state. So first we need to correctly start the tenant, then shut it down for good.

var shellSettingsManager = serviceProvider.GetService<IShellSettingsManager>();

await InvokeRestartAsync(shellSettingsManager, shellHost, shellSettings);
await InvokeShutdownAsync(shellSettings, shellHost);
Expand Down
14 changes: 7 additions & 7 deletions Lombiq.Hosting.Tenants.IdleTenantManagement/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lombiq.Hosting.Tenants.IdleTenantManagement.Constants;
using Lombiq.Hosting.Tenants.IdleTenantManagement.Constants;
using Lombiq.Hosting.Tenants.IdleTenantManagement.Middlewares;
using Lombiq.Hosting.Tenants.IdleTenantManagement.Models;
using Lombiq.Hosting.Tenants.IdleTenantManagement.Services;
Expand All @@ -13,29 +13,29 @@

namespace Lombiq.Hosting.Tenants.IdleTenantManagement;

[Feature(FeatureNames.DisableIdleTenants)]
public class DisableIdleTenantsStartup : StartupBase
[Feature(FeatureNames.ShutDownIdleTenants)]
public class ShutDownIdleTenantsStartup : StartupBase
{
private readonly IShellConfiguration _shellConfiguration;

public DisableIdleTenantsStartup(IShellConfiguration shellConfiguration) =>
public ShutDownIdleTenantsStartup(IShellConfiguration shellConfiguration) =>
_shellConfiguration = shellConfiguration;

public override void Configure(
IApplicationBuilder app,
IEndpointRouteBuilder routes,
IServiceProvider serviceProvider) =>
app.UseMiddleware<IdleTimeProviderMiddleware>();
app.UseMiddleware<IdleTimeProviderMiddleware>();

public override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ILastActiveTimeAccessor, LastActiveTimeAccessor>();
services.AddSingleton<IBackgroundTask, IdleShutdownTask>();

// Idle Minutes Settings
services.Configure<IdleMinutesOptions>(options =>
services.Configure<IdleShutdownOptions>(options =>
_shellConfiguration
.GetSection("Lombiq_Hosting_Tenants_IdleTenantManagement:IdleMinutesOptions")
.GetSection("Lombiq_Hosting_Tenants_IdleTenantManagement:IdleShutdownOptions")
.Bind(options));
}
}

0 comments on commit ade31a8

Please sign in to comment.