Skip to content

Commit

Permalink
Merge pull request #99 from Lombiq/issue/NEST-490
Browse files Browse the repository at this point in the history
NEST-490: Setting loglevel to information
  • Loading branch information
Piedone authored Nov 12, 2023
2 parents bce3d99 + 0971523 commit dc88ba0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;

namespace Lombiq.Hosting.Tenants.IdleTenantManagement.Services;

/// <summary>
/// Service to shut down idle tenants.
/// </summary>
public interface IIdleShutdown
{
/// <summary>
/// Shuts down idle tenants if they are idle for more than the configured time.
/// </summary>
Task ShutDownIdleTenantsAsync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Lombiq.Hosting.Tenants.IdleTenantManagement.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
using OrchardCore.Modules;
using System.Threading.Tasks;

namespace Lombiq.Hosting.Tenants.IdleTenantManagement.Services;

public class IdleShutdown : IIdleShutdown
{
private readonly IOptions<IdleShutdownOptions> _options;
private readonly ShellSettings _shellSettings;
private readonly IClock _clock;
private readonly ILastActiveTimeAccessor _lastActiveTimeAccessor;
private readonly ILogger<IdleShutdown> _logger;
private readonly IShellHost _shellHost;

public IdleShutdown(
IOptions<IdleShutdownOptions> options,
ShellSettings shellSettings,
IClock clock,
ILastActiveTimeAccessor lastActiveTimeAccessor,
ILogger<IdleShutdown> logger,
IShellHost shellHost)
{
_options = options;
_shellSettings = shellSettings;
_clock = clock;
_lastActiveTimeAccessor = lastActiveTimeAccessor;
_logger = logger;
_shellHost = shellHost;
}

public async Task ShutDownIdleTenantsAsync()
{
var maxIdleMinutes = _options.Value.MaxIdleMinutes;

if (maxIdleMinutes <= 0 || _shellSettings.IsDefaultShell()) return;

var lastActiveDateTimeUtc = _lastActiveTimeAccessor.LastActiveDateTimeUtc;

if (lastActiveDateTimeUtc.AddMinutes(maxIdleMinutes) <= _clock?.UtcNow)
{
_logger?.LogInformation("Shutting down tenant \"{ShellName}\" because of idle timeout.", _shellSettings.Name);

await _shellHost.ReleaseShellContextAsync(_shellSettings, eventSource: false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using Lombiq.Hosting.Tenants.IdleTenantManagement.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.BackgroundTasks;
using OrchardCore.Environment.Shell;
using OrchardCore.Modules;
using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -14,27 +9,10 @@ namespace Lombiq.Hosting.Tenants.IdleTenantManagement.Services;
[BackgroundTask(Schedule = "* * * * *", Description = "Shut down idle tenants.")]
public class IdleShutdownTask : IBackgroundTask
{
public async Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
public Task DoWorkAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
{
var maxIdleMinutes = serviceProvider.GetRequiredService<IOptions<IdleShutdownOptions>>().Value.MaxIdleMinutes;
var idleShutdown = serviceProvider.GetRequiredService<IIdleShutdown>();

var shellSettings = serviceProvider.GetRequiredService<ShellSettings>();

if (maxIdleMinutes <= 0 || shellSettings.IsDefaultShell()) return;

var clock = serviceProvider.GetRequiredService<IClock>();

var lastActiveDateTimeUtc = serviceProvider.GetRequiredService<ILastActiveTimeAccessor>().LastActiveDateTimeUtc;

if (lastActiveDateTimeUtc.AddMinutes(maxIdleMinutes) <= clock?.UtcNow)
{
var logger = serviceProvider.GetRequiredService<ILogger<IdleShutdownTask>>();

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

var shellHost = serviceProvider.GetRequiredService<IShellHost>();

await shellHost.ReleaseShellContextAsync(shellSettings, eventSource: false);
}
return idleShutdown.ShutDownIdleTenantsAsync();
}
}
1 change: 1 addition & 0 deletions Lombiq.Hosting.Tenants.IdleTenantManagement/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public override void Configure(
public override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ILastActiveTimeAccessor, LastActiveTimeAccessor>();
services.AddScoped<IIdleShutdown, IdleShutdown>();
services.AddSingleton<IBackgroundTask, IdleShutdownTask>();

// Idle Minutes Settings
Expand Down

0 comments on commit dc88ba0

Please sign in to comment.