Remove KeepAliveJob (#15891)

This commit is contained in:
Mole
2024-03-15 12:48:31 +01:00
committed by GitHub
parent c2fe847ba5
commit b619399edb
8 changed files with 0 additions and 221 deletions

View File

@@ -1,28 +0,0 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.ComponentModel;
namespace Umbraco.Cms.Core.Configuration.Models;
/// <summary>
/// Typed configuration options for keep alive settings.
/// </summary>
[UmbracoOptions(Constants.Configuration.ConfigKeepAlive)]
public class KeepAliveSettings
{
internal const bool StaticDisableKeepAliveTask = false;
internal const string StaticKeepAlivePingUrl = "~/api/keepalive/ping";
/// <summary>
/// Gets or sets a value indicating whether the keep alive task is disabled.
/// </summary>
[DefaultValue(StaticDisableKeepAliveTask)]
public bool DisableKeepAliveTask { get; set; } = StaticDisableKeepAliveTask;
/// <summary>
/// Gets or sets a value for the keep alive ping URL.
/// </summary>
[DefaultValue(StaticKeepAlivePingUrl)]
public string KeepAlivePingUrl { get; set; } = StaticKeepAlivePingUrl;
}

View File

@@ -39,7 +39,6 @@ public static partial class Constants
public const string ConfigImaging = ConfigPrefix + "Imaging"; public const string ConfigImaging = ConfigPrefix + "Imaging";
public const string ConfigExamine = ConfigPrefix + "Examine"; public const string ConfigExamine = ConfigPrefix + "Examine";
public const string ConfigIndexing = ConfigPrefix + "Indexing"; public const string ConfigIndexing = ConfigPrefix + "Indexing";
public const string ConfigKeepAlive = ConfigPrefix + "KeepAlive";
public const string ConfigLogging = ConfigPrefix + "Logging"; public const string ConfigLogging = ConfigPrefix + "Logging";
public const string ConfigMemberPassword = ConfigPrefix + "Security:MemberPassword"; public const string ConfigMemberPassword = ConfigPrefix + "Security:MemberPassword";
public const string ConfigModelsBuilder = ConfigPrefix + "ModelsBuilder"; public const string ConfigModelsBuilder = ConfigPrefix + "ModelsBuilder";

View File

@@ -68,7 +68,6 @@ public static partial class UmbracoBuilderExtensions
.AddUmbracoOptions<HostingSettings>() .AddUmbracoOptions<HostingSettings>()
.AddUmbracoOptions<ImagingSettings>() .AddUmbracoOptions<ImagingSettings>()
.AddUmbracoOptions<IndexingSettings>() .AddUmbracoOptions<IndexingSettings>()
.AddUmbracoOptions<KeepAliveSettings>()
.AddUmbracoOptions<LoggingSettings>() .AddUmbracoOptions<LoggingSettings>()
.AddUmbracoOptions<MemberPasswordConfigurationSettings>() .AddUmbracoOptions<MemberPasswordConfigurationSettings>()
.AddUmbracoOptions<NuCacheSettings>() .AddUmbracoOptions<NuCacheSettings>()

View File

@@ -1,90 +0,0 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Core.Sync;
using Umbraco.Extensions;
namespace Umbraco.Cms.Infrastructure.BackgroundJobs.Jobs;
/// <summary>
/// Hosted service implementation for keep alive feature.
/// </summary>
public class KeepAliveJob : IRecurringBackgroundJob
{
public TimeSpan Period { get => TimeSpan.FromMinutes(5); }
// No-op event as the period never changes on this job
public event EventHandler PeriodChanged { add { } remove { } }
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<KeepAliveJob> _logger;
private readonly IProfilingLogger _profilingLogger;
private KeepAliveSettings _keepAliveSettings;
/// <summary>
/// Initializes a new instance of the <see cref="KeepAliveJob" /> class.
/// </summary>
/// <param name="hostingEnvironment">The current hosting environment</param>
/// <param name="keepAliveSettings">The configuration for keep alive settings.</param>
/// <param name="logger">The typed logger.</param>
/// <param name="profilingLogger">The profiling logger.</param>
/// <param name="httpClientFactory">Factory for <see cref="HttpClient" /> instances.</param>
public KeepAliveJob(
IHostingEnvironment hostingEnvironment,
IOptionsMonitor<KeepAliveSettings> keepAliveSettings,
ILogger<KeepAliveJob> logger,
IProfilingLogger profilingLogger,
IHttpClientFactory httpClientFactory)
{
_hostingEnvironment = hostingEnvironment;
_keepAliveSettings = keepAliveSettings.CurrentValue;
_logger = logger;
_profilingLogger = profilingLogger;
_httpClientFactory = httpClientFactory;
keepAliveSettings.OnChange(x => _keepAliveSettings = x);
}
public async Task RunJobAsync()
{
if (_keepAliveSettings.DisableKeepAliveTask)
{
return;
}
using (_profilingLogger.DebugDuration<KeepAliveJob>("Keep alive executing", "Keep alive complete"))
{
var umbracoAppUrl = _hostingEnvironment.ApplicationMainUrl?.ToString();
if (umbracoAppUrl.IsNullOrWhiteSpace())
{
_logger.LogWarning("No umbracoApplicationUrl for service (yet), skip.");
return;
}
// If the config is an absolute path, just use it
var keepAlivePingUrl = WebPath.Combine(
umbracoAppUrl!,
_hostingEnvironment.ToAbsolute(_keepAliveSettings.KeepAlivePingUrl));
try
{
var request = new HttpRequestMessage(HttpMethod.Get, keepAlivePingUrl);
HttpClient httpClient = _httpClientFactory.CreateClient(Constants.HttpClients.IgnoreCertificateErrors);
_ = await httpClient.SendAsync(request);
}
catch (Exception ex)
{
_logger.LogError(ex, "Keep alive failed (at '{keepAlivePingUrl}').", keepAlivePingUrl);
}
}
}
}

View File

@@ -176,7 +176,6 @@ public static partial class UmbracoBuilderExtensions
{ {
// Add background jobs // Add background jobs
builder.Services.AddRecurringBackgroundJob<HealthCheckNotifierJob>(); builder.Services.AddRecurringBackgroundJob<HealthCheckNotifierJob>();
builder.Services.AddRecurringBackgroundJob<KeepAliveJob>();
builder.Services.AddRecurringBackgroundJob<LogScrubberJob>(); builder.Services.AddRecurringBackgroundJob<LogScrubberJob>();
builder.Services.AddRecurringBackgroundJob<ContentVersionCleanupJob>(); builder.Services.AddRecurringBackgroundJob<ContentVersionCleanupJob>();
builder.Services.AddRecurringBackgroundJob<ScheduledPublishingJob>(); builder.Services.AddRecurringBackgroundJob<ScheduledPublishingJob>();

View File

@@ -30,10 +30,6 @@
"Hosting": { "Hosting": {
"Debug": false "Debug": false
}, },
"KeepAlive": {
"DisableKeepAliveTask": false,
"KeepAlivePingUrl": "~/api/keepalive/ping"
},
"RequestHandler": { "RequestHandler": {
"ConvertUrlsToAscii": "try" "ConvertUrlsToAscii": "try"
}, },

View File

@@ -1,94 +0,0 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Moq;
using Moq.Protected;
using NUnit.Framework;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Infrastructure.BackgroundJobs.Jobs;
using Umbraco.Cms.Infrastructure.HostedServices;
using Umbraco.Cms.Tests.Common;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.BackgroundJobs.Jobs;
[TestFixture]
public class KeepAliveJobTests
{
private Mock<HttpMessageHandler> _mockHttpMessageHandler;
private const string ApplicationUrl = "https://mysite.com";
[Test]
public async Task Does_Not_Execute_When_Not_Enabled()
{
var sut = CreateKeepAlive(false);
await sut.RunJobAsync();
VerifyKeepAliveRequestNotSent();
}
[Test]
public async Task Executes_And_Calls_Ping_Url()
{
var sut = CreateKeepAlive();
await sut.RunJobAsync();
VerifyKeepAliveRequestSent();
}
private KeepAliveJob CreateKeepAlive(
bool enabled = true)
{
var settings = new KeepAliveSettings { DisableKeepAliveTask = !enabled };
var mockHostingEnvironment = new Mock<IHostingEnvironment>();
mockHostingEnvironment.SetupGet(x => x.ApplicationMainUrl).Returns(new Uri(ApplicationUrl));
mockHostingEnvironment.Setup(x => x.ToAbsolute(It.IsAny<string>()))
.Returns((string s) => s.TrimStart('~'));
var mockScopeProvider = new Mock<IScopeProvider>();
var mockLogger = new Mock<ILogger<KeepAliveJob>>();
var mockProfilingLogger = new Mock<IProfilingLogger>();
_mockHttpMessageHandler = new Mock<HttpMessageHandler>();
_mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK))
.Verifiable();
_mockHttpMessageHandler.As<IDisposable>().Setup(s => s.Dispose());
var httpClient = new HttpClient(_mockHttpMessageHandler.Object);
var mockHttpClientFactory = new Mock<IHttpClientFactory>(MockBehavior.Strict);
mockHttpClientFactory.Setup(x => x.CreateClient(It.IsAny<string>())).Returns(httpClient);
return new KeepAliveJob(
mockHostingEnvironment.Object,
new TestOptionsMonitor<KeepAliveSettings>(settings),
mockLogger.Object,
mockProfilingLogger.Object,
mockHttpClientFactory.Object);
}
private void VerifyKeepAliveRequestNotSent() => VerifyKeepAliveRequestSentTimes(Times.Never());
private void VerifyKeepAliveRequestSent() => VerifyKeepAliveRequestSentTimes(Times.Once());
private void VerifyKeepAliveRequestSentTimes(Times times) => _mockHttpMessageHandler.Protected()
.Verify(
"SendAsync",
times,
ItExpr.Is<HttpRequestMessage>(x => x.RequestUri.ToString() == $"{ApplicationUrl}/api/keepalive/ping"),
ItExpr.IsAny<CancellationToken>());
}

View File

@@ -39,8 +39,6 @@ internal class UmbracoCmsSchema
public IndexCreatorSettings Examine { get; set; } = null!; public IndexCreatorSettings Examine { get; set; } = null!;
public IndexingSettings Indexing { get; set; } = null!; public IndexingSettings Indexing { get; set; } = null!;
public KeepAliveSettings KeepAlive { get; set; } = null!;
public LoggingSettings Logging { get; set; } = null!; public LoggingSettings Logging { get; set; } = null!;
public NuCacheSettings NuCache { get; set; } = null!; public NuCacheSettings NuCache { get; set; } = null!;