<commit_before>// Copyright (c) Umbraco.
// See LICENSE for more details.

using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Infrastructure.Persistence;

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<UmbracoApplicationStarting>, INotificationHandler<UmbracoRequestEnd>
    {
        private readonly IServerMessenger _messenger;
        private readonly IRuntimeState _runtimeState;
        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,
            IRuntimeState runtimeState,
            IDistributedCacheBinder distributedCacheBinder,
            ILogger<DatabaseServerMessengerNotificationHandler> logger)
        {
            _distributedCacheBinder = distributedCacheBinder;
            _logger = logger;
            _messenger = serverMessenger;
            _runtimeState = runtimeState;
        }

        /// <inheritdoc/>
        public void Handle(UmbracoApplicationStarting notification)
        {
            if (_runtimeState.Level == RuntimeLevel.Install)
            {
                _logger.LogWarning("Disabling distributed calls during install");
            }
            else
            {<commit_msg>Re-add runtime level check, only make calls in Run mode<commit_after>// Copyright (c) Umbraco.
// See LICENSE for more details.

using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Infrastructure.Persistence;

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<UmbracoApplicationStarting>, INotificationHandler<UmbracoRequestEnd>
    {
        private readonly IServerMessenger _messenger; 
        private readonly IUmbracoDatabaseFactory _databaseFactory;
        private readonly IDistributedCacheBinder _distributedCacheBinder;
        private readonly ILogger<DatabaseServerMessengerNotificationHandler> _logger;
        private readonly IRuntimeState _runtimeState;

        /// <summary>
        /// Initializes a new instance of the <see cref="DatabaseServerMessengerNotificationHandler"/> class.
        /// </summary>
        public DatabaseServerMessengerNotificationHandler(
            IServerMessenger serverMessenger, 
            IUmbracoDatabaseFactory databaseFactory,
            IDistributedCacheBinder distributedCacheBinder,
            ILogger<DatabaseServerMessengerNotificationHandler> logger,
            IRuntimeState runtimeState)
        { 
            _databaseFactory = databaseFactory;
            _distributedCacheBinder = distributedCacheBinder;
            _logger = logger;
            _messenger = serverMessenger;
            _runtimeState = runtimeState;
        }

        /// <inheritdoc/>
        public void Handle(UmbracoApplicationStarting 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
            {
{'fraction_non_alphanumeric': 0.06919642857142858, 'fraction_numerical': 0.0, 'mean_word_length': 2.8519579751671444, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4943169', 'n_tokens_mistral': 1013, 'n_tokens_neox': 962, 'n_words': 242}