2020-12-24 09:50:05 +11:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Umbraco.Core.Events;
|
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
using Umbraco.Core.Sync;
|
|
|
|
|
using Umbraco.Web;
|
|
|
|
|
using Umbraco.Web.Cache;
|
|
|
|
|
using Umbraco.Web.Routing;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Infrastructure.Cache
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures that distributed cache events are setup and the <see cref="IServerMessenger"/> is initialized
|
|
|
|
|
/// </summary>
|
2021-02-02 18:42:39 +11:00
|
|
|
public sealed class DatabaseServerMessengerNotificationHandler : INotificationHandler<UmbracoApplicationStarting>, INotificationHandler<UmbracoRequestEnd>
|
2020-12-24 09:50:05 +11:00
|
|
|
{
|
2020-12-24 14:44:42 +11:00
|
|
|
private readonly IServerMessenger _messenger;
|
2020-12-24 09:50:05 +11:00
|
|
|
private readonly IUmbracoDatabaseFactory _databaseFactory;
|
|
|
|
|
private readonly IDistributedCacheBinder _distributedCacheBinder;
|
|
|
|
|
private readonly ILogger<DatabaseServerMessengerNotificationHandler> _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="DatabaseServerMessengerNotificationHandler"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DatabaseServerMessengerNotificationHandler(
|
|
|
|
|
IServerMessenger serverMessenger,
|
|
|
|
|
IUmbracoDatabaseFactory databaseFactory,
|
|
|
|
|
IDistributedCacheBinder distributedCacheBinder,
|
|
|
|
|
ILogger<DatabaseServerMessengerNotificationHandler> logger)
|
|
|
|
|
{
|
|
|
|
|
_databaseFactory = databaseFactory;
|
|
|
|
|
_distributedCacheBinder = distributedCacheBinder;
|
|
|
|
|
_logger = logger;
|
2020-12-24 14:44:42 +11:00
|
|
|
_messenger = serverMessenger;
|
2020-12-24 09:50:05 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2021-01-19 09:57:55 +01:00
|
|
|
public void Handle(UmbracoApplicationStarting notification)
|
2020-12-24 09:50:05 +11:00
|
|
|
{
|
|
|
|
|
if (_databaseFactory.CanConnect == false)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Cannot connect to the database, distributed calls will not be enabled for this server.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_distributedCacheBinder.BindEvents();
|
|
|
|
|
|
|
|
|
|
// Sync on startup, this will run through the messenger's initialization sequence
|
|
|
|
|
_messenger?.Sync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clear the batch on end request
|
|
|
|
|
/// </summary>
|
2021-02-02 18:42:39 +11:00
|
|
|
public void Handle(UmbracoRequestEnd notification) => _messenger?.SendMessages();
|
2020-12-24 09:50:05 +11:00
|
|
|
}
|
|
|
|
|
}
|