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-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
|
|
|
|
2021-02-12 10:13:56 +01:00
|
|
|
namespace Umbraco.Cms.Core.Cache
|
2020-12-24 09:50:05 +11:00
|
|
|
{
|
|
|
|
|
/// <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;
|
2021-03-17 11:50:23 -07:00
|
|
|
private readonly IRuntimeState _runtimeState;
|
2020-12-24 09:50:05 +11:00
|
|
|
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,
|
2021-03-17 11:50:23 -07:00
|
|
|
IRuntimeState runtimeState,
|
2020-12-24 09:50:05 +11:00
|
|
|
IDistributedCacheBinder distributedCacheBinder,
|
|
|
|
|
ILogger<DatabaseServerMessengerNotificationHandler> logger)
|
|
|
|
|
{
|
|
|
|
|
_distributedCacheBinder = distributedCacheBinder;
|
|
|
|
|
_logger = logger;
|
2020-12-24 14:44:42 +11:00
|
|
|
_messenger = serverMessenger;
|
2021-03-17 11:50:23 -07:00
|
|
|
_runtimeState = runtimeState;
|
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
|
|
|
{
|
2021-03-17 11:50:23 -07:00
|
|
|
if (_runtimeState.Level == RuntimeLevel.Install)
|
2020-12-24 09:50:05 +11:00
|
|
|
{
|
2021-03-17 11:50:23 -07:00
|
|
|
_logger.LogWarning("Disabling distributed calls during install");
|
2020-12-24 09:50:05 +11:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|