// Copyright (c) Umbraco. // See LICENSE for more details. using Microsoft.Extensions.Logging; using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Sync; using Umbraco.Cms.Infrastructure.Persistence; namespace Umbraco.Cms.Core.Cache { /// /// Ensures that distributed cache events are setup and the is initialized /// public sealed class DatabaseServerMessengerNotificationHandler : INotificationHandler, INotificationHandler { private readonly IServerMessenger _messenger; private readonly IUmbracoDatabaseFactory _databaseFactory; private readonly ILogger _logger; private readonly IRuntimeState _runtimeState; /// /// Initializes a new instance of the class. /// public DatabaseServerMessengerNotificationHandler( IServerMessenger serverMessenger, IUmbracoDatabaseFactory databaseFactory, ILogger logger, IRuntimeState runtimeState) { _databaseFactory = databaseFactory; _logger = logger; _messenger = serverMessenger; _runtimeState = runtimeState; } /// public void Handle(UmbracoApplicationStartingNotification notification) { if (_databaseFactory.CanConnect == false) { _logger.LogWarning("Cannot connect to the database, distributed calls will not be enabled for this server."); } else if (_runtimeState.Level != RuntimeLevel.Run) { _logger.LogWarning("Distributed calls are not available outside the Run runtime level"); } else { // Sync on startup, this will run through the messenger's initialization sequence _messenger?.Sync(); } } /// /// Clear the batch on end request /// public void Handle(UmbracoRequestEndNotification notification) => _messenger?.SendMessages(); } }