From 7d0572f2d156cab83eeae1dc6fa80295cade1460 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 8 Jun 2021 16:32:26 -0600 Subject: [PATCH] streamline runtime level checks --- .../Extensions/RuntimeStateExtensions.cs | 17 +++++++++++++++++ ...atabaseServerMessengerNotificationHandler.cs | 2 +- .../Examine/ExamineIndexRebuilder.cs | 2 +- .../Examine/RebuildOnStartupHandler.cs | 2 +- .../Persistence/UmbracoPocoDataBuilder.cs | 4 +++- .../Runtime/CoreRuntime.cs | 8 +++----- src/Umbraco.Infrastructure/RuntimeState.cs | 12 +++++++++++- .../Authorization/BackOfficeHandler.cs | 4 ++-- .../Install/InstallAreaRoutes.cs | 7 +++---- .../Install/InstallAuthorizeAttribute.cs | 6 +++--- .../Routing/BackOfficeAreaRoutes.cs | 1 + .../Routing/PreviewRoutes.cs | 1 + src/Umbraco.Web.Common/Profiler/WebProfiler.cs | 4 ++-- 13 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/Umbraco.Core/Extensions/RuntimeStateExtensions.cs b/src/Umbraco.Core/Extensions/RuntimeStateExtensions.cs index 4e45ea63d8..c3ef7af561 100644 --- a/src/Umbraco.Core/Extensions/RuntimeStateExtensions.cs +++ b/src/Umbraco.Core/Extensions/RuntimeStateExtensions.cs @@ -5,9 +5,26 @@ namespace Umbraco.Extensions { public static class RuntimeStateExtensions { + /// + /// Returns true if the installer is enabled based on the current runtime state + /// + /// + /// + public static bool EnableInstaller(this IRuntimeState state) + => state.Level == RuntimeLevel.Install || state.Level == RuntimeLevel.Upgrade || state.Level == RuntimeLevel.PackageMigrations; + /// /// Returns true if Umbraco is greater than /// public static bool UmbracoCanBoot(this IRuntimeState state) => state.Level > RuntimeLevel.BootFailed; + + /// + /// Returns true if the runtime state indicates that unattended boot logic should execute + /// + /// + /// + public static bool RunUnattendedBootLogic(this IRuntimeState state) + => (state.Reason == RuntimeLevelReason.UpgradeMigrations || state.Reason == RuntimeLevelReason.UpgradePackageMigrations) + && state.Level == RuntimeLevel.Run; } } diff --git a/src/Umbraco.Infrastructure/Cache/DatabaseServerMessengerNotificationHandler.cs b/src/Umbraco.Infrastructure/Cache/DatabaseServerMessengerNotificationHandler.cs index 092b4c6b99..fc59d06016 100644 --- a/src/Umbraco.Infrastructure/Cache/DatabaseServerMessengerNotificationHandler.cs +++ b/src/Umbraco.Infrastructure/Cache/DatabaseServerMessengerNotificationHandler.cs @@ -38,7 +38,7 @@ namespace Umbraco.Cms.Core.Cache /// public void Handle(UmbracoApplicationStartingNotification notification) { - if (_runtimeState.Level < RuntimeLevel.Run) + if (_runtimeState.Level != RuntimeLevel.Run) { return; } diff --git a/src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs b/src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs index 7d0a933027..472321ac9e 100644 --- a/src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs +++ b/src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs @@ -112,7 +112,7 @@ namespace Umbraco.Cms.Infrastructure.Examine } } - private bool CanRun() => _mainDom.IsMainDom && _runtimeState.Level >= RuntimeLevel.Run; + private bool CanRun() => _mainDom.IsMainDom && _runtimeState.Level == RuntimeLevel.Run; private void RebuildIndex(string indexName, TimeSpan delay, CancellationToken cancellationToken) { diff --git a/src/Umbraco.Infrastructure/Examine/RebuildOnStartupHandler.cs b/src/Umbraco.Infrastructure/Examine/RebuildOnStartupHandler.cs index 05b016dbb6..7ec59e1c2e 100644 --- a/src/Umbraco.Infrastructure/Examine/RebuildOnStartupHandler.cs +++ b/src/Umbraco.Infrastructure/Examine/RebuildOnStartupHandler.cs @@ -47,7 +47,7 @@ namespace Umbraco.Cms.Infrastructure.Examine /// public void Handle(UmbracoRequestBeginNotification notification) { - if (_runtimeState.Level < RuntimeLevel.Run) + if (_runtimeState.Level != RuntimeLevel.Run) { return; } diff --git a/src/Umbraco.Infrastructure/Persistence/UmbracoPocoDataBuilder.cs b/src/Umbraco.Infrastructure/Persistence/UmbracoPocoDataBuilder.cs index 3e34a77d7b..2e9fb6cebc 100644 --- a/src/Umbraco.Infrastructure/Persistence/UmbracoPocoDataBuilder.cs +++ b/src/Umbraco.Infrastructure/Persistence/UmbracoPocoDataBuilder.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using NPoco; using Umbraco.Cms.Infrastructure.Persistence.Dtos; @@ -33,6 +33,8 @@ namespace Umbraco.Cms.Infrastructure.Persistence { var columnInfo = base.GetColumnInfo(mi, type); + // TODO: Is this upgrade flag still relevant? It's a lot of hacking to just set this value + // including the interface method ConfigureForUpgrade for this one circumstance. if (_upgrading) { if (type == typeof(UserDto) && mi.Name == "TourData") columnInfo.IgnoreColumn = true; diff --git a/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs b/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs index f7065fe0b5..715b569b96 100644 --- a/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs +++ b/src/Umbraco.Infrastructure/Runtime/CoreRuntime.cs @@ -15,6 +15,7 @@ using Umbraco.Cms.Core.Services; using Umbraco.Cms.Infrastructure.Migrations.Install; using Umbraco.Cms.Infrastructure.Migrations.Upgrade; using Umbraco.Cms.Infrastructure.Persistence; +using Umbraco.Extensions; namespace Umbraco.Cms.Infrastructure.Runtime { @@ -106,7 +107,7 @@ namespace Umbraco.Cms.Infrastructure.Runtime DoUnattendedInstall(); DetermineRuntimeLevel(); - if (State.Level <= RuntimeLevel.BootFailed) + if (!State.UmbracoCanBoot()) { return; // The exception will be rethrown by BootFailedMiddelware } @@ -117,17 +118,14 @@ namespace Umbraco.Cms.Infrastructure.Runtime throw new InvalidOperationException($"An instance of {typeof(IApplicationShutdownRegistry)} could not be resolved from the container, ensure that one if registered in your runtime before calling {nameof(IRuntime)}.{nameof(StartAsync)}"); } - - // if level is Run and reason is UpgradeMigrations, that means we need to perform an unattended upgrade - if (State.Reason == RuntimeLevelReason.UpgradeMigrations && State.Level == RuntimeLevel.Run) + if (State.RunUnattendedBootLogic()) { // do the upgrade DoUnattendedUpgrade(); // upgrade is done, set reason to Run DetermineRuntimeLevel(); - } // create & initialize the components diff --git a/src/Umbraco.Infrastructure/RuntimeState.cs b/src/Umbraco.Infrastructure/RuntimeState.cs index 6db80bbf34..f362ba7ec3 100644 --- a/src/Umbraco.Infrastructure/RuntimeState.cs +++ b/src/Umbraco.Infrastructure/RuntimeState.cs @@ -236,26 +236,36 @@ namespace Umbraco.Cms.Core { // unattended install is not enabled if (_unattendedSettings.Value.InstallUnattended == false) + { return; + } // no connection string set if (_databaseFactory.Configured == false) + { return; + } - var connect = false; var tries = _globalSettings.Value.InstallMissingDatabase ? 2 : 5; + + bool connect; for (var i = 0; ;) { connect = _databaseFactory.CanConnect; if (connect || ++i == tries) + { break; + } + _logger.LogDebug("Could not immediately connect to database, trying again."); Thread.Sleep(1000); } // could not connect to the database if (connect == false) + { return; + } using (var database = _databaseFactory.CreateDatabase()) { diff --git a/src/Umbraco.Web.BackOffice/Authorization/BackOfficeHandler.cs b/src/Umbraco.Web.BackOffice/Authorization/BackOfficeHandler.cs index 97108f5460..fd7f8eb971 100644 --- a/src/Umbraco.Web.BackOffice/Authorization/BackOfficeHandler.cs +++ b/src/Umbraco.Web.BackOffice/Authorization/BackOfficeHandler.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Authorization; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Security; using Umbraco.Cms.Core.Services; +using Umbraco.Extensions; namespace Umbraco.Cms.Web.BackOffice.Authorization { @@ -30,8 +31,7 @@ namespace Umbraco.Cms.Web.BackOffice.Authorization switch (_runtimeState.Level) { - case RuntimeLevel.Install: - case RuntimeLevel.Upgrade: + case var _ when _runtimeState.EnableInstaller(): return Task.FromResult(true); default: if (!_backOfficeSecurity.BackOfficeSecurity.IsAuthenticated()) diff --git a/src/Umbraco.Web.BackOffice/Install/InstallAreaRoutes.cs b/src/Umbraco.Web.BackOffice/Install/InstallAreaRoutes.cs index 28871db452..6280631c83 100644 --- a/src/Umbraco.Web.BackOffice/Install/InstallAreaRoutes.cs +++ b/src/Umbraco.Web.BackOffice/Install/InstallAreaRoutes.cs @@ -1,4 +1,4 @@ -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Routing; using Umbraco.Cms.Core; @@ -29,9 +29,8 @@ namespace Umbraco.Cms.Web.BackOffice.Install switch (_runtime.Level) { - case RuntimeLevel.Install: - case RuntimeLevel.Upgrade: - + case var _ when _runtime.EnableInstaller(): + endpoints.MapUmbracoRoute(installPathSegment, Cms.Core.Constants.Web.Mvc.InstallArea, "api", includeControllerNameInRoute: false); endpoints.MapUmbracoRoute(installPathSegment, Cms.Core.Constants.Web.Mvc.InstallArea, string.Empty, includeControllerNameInRoute: false); diff --git a/src/Umbraco.Web.BackOffice/Install/InstallAuthorizeAttribute.cs b/src/Umbraco.Web.BackOffice/Install/InstallAuthorizeAttribute.cs index ee55f22888..429b204ec6 100644 --- a/src/Umbraco.Web.BackOffice/Install/InstallAuthorizeAttribute.cs +++ b/src/Umbraco.Web.BackOffice/Install/InstallAuthorizeAttribute.cs @@ -1,9 +1,10 @@ -using System; +using System; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Services; +using Umbraco.Extensions; namespace Umbraco.Cms.Web.BackOffice.Install { @@ -44,8 +45,7 @@ namespace Umbraco.Cms.Web.BackOffice.Install { // if not configured (install or upgrade) then we can continue // otherwise we need to ensure that a user is logged in - return _runtimeState.Level == RuntimeLevel.Install - || _runtimeState.Level == RuntimeLevel.Upgrade + return _runtimeState.EnableInstaller() || (authorizationFilterContext.HttpContext.User?.Identity?.IsAuthenticated ?? false); } catch (Exception ex) diff --git a/src/Umbraco.Web.BackOffice/Routing/BackOfficeAreaRoutes.cs b/src/Umbraco.Web.BackOffice/Routing/BackOfficeAreaRoutes.cs index 13d57ccc43..3f171d6439 100644 --- a/src/Umbraco.Web.BackOffice/Routing/BackOfficeAreaRoutes.cs +++ b/src/Umbraco.Web.BackOffice/Routing/BackOfficeAreaRoutes.cs @@ -48,6 +48,7 @@ namespace Umbraco.Cms.Web.BackOffice.Routing { case RuntimeLevel.Install: case RuntimeLevel.Upgrade: + case RuntimeLevel.PackageMigrations: case RuntimeLevel.Run: MapMinimalBackOffice(endpoints); diff --git a/src/Umbraco.Web.BackOffice/Routing/PreviewRoutes.cs b/src/Umbraco.Web.BackOffice/Routing/PreviewRoutes.cs index bda08d0d87..15012728d9 100644 --- a/src/Umbraco.Web.BackOffice/Routing/PreviewRoutes.cs +++ b/src/Umbraco.Web.BackOffice/Routing/PreviewRoutes.cs @@ -36,6 +36,7 @@ namespace Umbraco.Cms.Web.BackOffice.Routing { case RuntimeLevel.Install: case RuntimeLevel.Upgrade: + case RuntimeLevel.PackageMigrations: case RuntimeLevel.Run: endpoints.MapHub(GetPreviewHubRoute()); endpoints.MapUmbracoRoute(_umbracoPathSegment, Constants.Web.Mvc.BackOfficeArea, null); diff --git a/src/Umbraco.Web.Common/Profiler/WebProfiler.cs b/src/Umbraco.Web.Common/Profiler/WebProfiler.cs index caf0b2fae0..7c5a89fa71 100644 --- a/src/Umbraco.Web.Common/Profiler/WebProfiler.cs +++ b/src/Umbraco.Web.Common/Profiler/WebProfiler.cs @@ -42,7 +42,7 @@ namespace Umbraco.Cms.Web.Common.Profiler public void UmbracoApplicationBeginRequest(HttpContext context, RuntimeLevel runtimeLevel) { - if (runtimeLevel < RuntimeLevel.Run) + if (runtimeLevel != RuntimeLevel.Run) { return; } @@ -55,7 +55,7 @@ namespace Umbraco.Cms.Web.Common.Profiler public void UmbracoApplicationEndRequest(HttpContext context, RuntimeLevel runtimeLevel) { - if (runtimeLevel < RuntimeLevel.Run) + if (runtimeLevel != RuntimeLevel.Run) { return; }