From 154b59c84b9f365391e381405b6dcc0da3031d9d Mon Sep 17 00:00:00 2001 From: Stephan Date: Mon, 11 Feb 2019 14:09:40 +0100 Subject: [PATCH] Fix redirect tracking --- .../Routing/RedirectTrackingComponent.cs | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Umbraco.Web/Routing/RedirectTrackingComponent.cs b/src/Umbraco.Web/Routing/RedirectTrackingComponent.cs index c49dffd6e4..acbd022f2e 100644 --- a/src/Umbraco.Web/Routing/RedirectTrackingComponent.cs +++ b/src/Umbraco.Web/Routing/RedirectTrackingComponent.cs @@ -38,16 +38,24 @@ namespace Umbraco.Web.Routing { get { - var oldRoutes = - (Dictionary) UmbracoContext.Current.HttpContext.Items[ - ContextKey3]; + var oldRoutes = (Dictionary) UmbracoContext.Current.HttpContext.Items[ContextKey3]; if (oldRoutes == null) - UmbracoContext.Current.HttpContext.Items[ContextKey3] = - oldRoutes = new Dictionary(); + UmbracoContext.Current.HttpContext.Items[ContextKey3] = oldRoutes = new Dictionary(); return oldRoutes; } } + private static bool HasOldRoutes + { + get + { + if (Current.UmbracoContext == null) return false; + if (Current.UmbracoContext.HttpContext == null) return false; + if (Current.UmbracoContext.HttpContext.Items[ContextKey3] == null) return false; + return true; + } + } + private static bool LockedEvents { get => Moving && UmbracoContext.Current.HttpContext.Items[ContextKey2] != null; @@ -97,8 +105,8 @@ namespace Umbraco.Web.Routing ContentService.Published += ContentService_Published; ContentService.Moving += ContentService_Moving; ContentService.Moved += ContentService_Moved; - ContentCacheRefresher.CacheUpdated += ContentCacheRefresher_CacheUpdated; + ContentCacheRefresher.CacheUpdated += ContentCacheRefresher_CacheUpdated; // kill all redirects once a content is deleted //ContentService.Deleted += ContentService_Deleted; @@ -111,21 +119,26 @@ namespace Umbraco.Web.Routing public void Terminate() { } - private static void ContentCacheRefresher_CacheUpdated(ContentCacheRefresher sender, - CacheRefresherEventArgs args) + private static void ContentCacheRefresher_CacheUpdated(ContentCacheRefresher sender, CacheRefresherEventArgs args) { + // that event is a distributed even that triggers on all nodes + // BUT it should totally NOT run on nodes other that the one that handled the other events + // and besides, it cannot run on a background thread! + if (!HasOldRoutes) + return; + // sanity checks if (args.MessageType != MessageType.RefreshByPayload) { throw new InvalidOperationException("ContentCacheRefresher MessageType should be ByPayload."); } + if (args.MessageObject == null) { return; } - var payloads = args.MessageObject as ContentCacheRefresher.JsonPayload[]; - if (payloads == null) + if (!(args.MessageObject is ContentCacheRefresher.JsonPayload[])) { throw new InvalidOperationException("ContentCacheRefresher MessageObject should be JsonPayload[]."); } @@ -137,8 +150,7 @@ namespace Umbraco.Web.Routing { // assuming we cannot have 'CacheUpdated' for only part of the infos else we'd need // to set a flag in 'Published' to indicate which entities have been refreshed ok - CreateRedirect(oldRoute.Key.ContentId, oldRoute.Key.Culture, oldRoute.Value.ContentKey, - oldRoute.Value.OldRoute); + CreateRedirect(oldRoute.Key.ContentId, oldRoute.Key.Culture, oldRoute.Value.ContentKey, oldRoute.Value.OldRoute); removeKeys.Add(oldRoute.Key); }