2021-02-12 10:13:56 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-12-24 09:50:05 +11:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Events;
|
2021-05-11 14:33:49 +02:00
|
|
|
using Umbraco.Cms.Core.Notifications;
|
2021-03-17 11:50:23 -07:00
|
|
|
using Umbraco.Cms.Core.Services;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Sync;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
2020-12-24 09:50:05 +11:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
namespace Umbraco.Cms.Core.Cache;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures that distributed cache events are setup and the <see cref="IServerMessenger" /> is initialized
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class DatabaseServerMessengerNotificationHandler :
|
|
|
|
|
INotificationHandler<UmbracoApplicationStartingNotification>, INotificationHandler<UmbracoRequestEndNotification>
|
2020-12-24 09:50:05 +11:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
private readonly IUmbracoDatabaseFactory _databaseFactory;
|
|
|
|
|
private readonly ILogger<DatabaseServerMessengerNotificationHandler> _logger;
|
|
|
|
|
private readonly IServerMessenger _messenger;
|
|
|
|
|
private readonly IRuntimeState _runtimeState;
|
|
|
|
|
|
2020-12-24 09:50:05 +11:00
|
|
|
/// <summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
/// Initializes a new instance of the <see cref="DatabaseServerMessengerNotificationHandler" /> class.
|
2020-12-24 09:50:05 +11:00
|
|
|
/// </summary>
|
2022-06-02 08:18:31 +02:00
|
|
|
public DatabaseServerMessengerNotificationHandler(
|
|
|
|
|
IServerMessenger serverMessenger,
|
|
|
|
|
IUmbracoDatabaseFactory databaseFactory,
|
|
|
|
|
ILogger<DatabaseServerMessengerNotificationHandler> logger,
|
|
|
|
|
IRuntimeState runtimeState)
|
|
|
|
|
{
|
|
|
|
|
_databaseFactory = databaseFactory;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_messenger = serverMessenger;
|
|
|
|
|
_runtimeState = runtimeState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Handle(UmbracoApplicationStartingNotification notification)
|
2020-12-24 09:50:05 +11:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
if (_runtimeState.Level != RuntimeLevel.Run)
|
2021-04-20 12:17:11 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
return;
|
2020-12-24 09:50:05 +11:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
if (_databaseFactory.CanConnect == false)
|
2020-12-24 09:50:05 +11:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
_logger.LogWarning(
|
|
|
|
|
"Cannot connect to the database, distributed calls will not be enabled for this server.");
|
|
|
|
|
return;
|
2020-12-24 09:50:05 +11:00
|
|
|
}
|
|
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// Sync on startup, this will run through the messenger's initialization sequence
|
|
|
|
|
_messenger?.Sync();
|
2020-12-24 09:50:05 +11:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clear the batch on end request
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Handle(UmbracoRequestEndNotification notification) => _messenger?.SendMessages();
|
2020-12-24 09:50:05 +11:00
|
|
|
}
|