From 475d73465ec315b3a9b85ab84dbd17c3179c35a7 Mon Sep 17 00:00:00 2001 From: Kevin Jump Date: Fri, 27 Sep 2019 16:52:54 +0100 Subject: [PATCH] V8/content startup issues (#56) * Working around the issues you get trying to work with content at startup. * Ensure Context so we can publish from outside of a request. * Tidy usings / * Tidy ContentSerializer's usings. --- uSync8.BackOffice/uSyncBackofficeComponent.cs | 55 +++++++++++------ .../Serializers/ContentSerializer.cs | 60 +++++++++++++------ .../Serializers/ContentSerializerBase.cs | 6 +- 3 files changed, 80 insertions(+), 41 deletions(-) diff --git a/uSync8.BackOffice/uSyncBackofficeComponent.cs b/uSync8.BackOffice/uSyncBackofficeComponent.cs index c260accc..0bff24ef 100644 --- a/uSync8.BackOffice/uSyncBackofficeComponent.cs +++ b/uSync8.BackOffice/uSyncBackofficeComponent.cs @@ -27,16 +27,19 @@ public class uSyncBackofficeComponent : IComponent private readonly uSyncService uSyncService; private readonly IRuntimeState runtimeState; + private readonly IUmbracoContextFactory umbracoContextFactory; + public uSyncBackofficeComponent( SyncHandlerFactory handlerFactory, IProfilingLogger logger, - SyncFileService fileService, + SyncFileService fileService, uSyncService uSyncService, - IRuntimeState runtimeState) + IRuntimeState runtimeState, + IUmbracoContextFactory umbracoContextFactory) { globalSettings = Current.Configs.uSync(); - this.runtimeState = runtimeState; + this.runtimeState = runtimeState; this.logger = logger; this.handlerFactory = handlerFactory; @@ -44,6 +47,8 @@ public uSyncBackofficeComponent( this.syncFileService = fileService; this.uSyncService = uSyncService; + this.umbracoContextFactory = umbracoContextFactory; + } public void Initialize() @@ -82,28 +87,40 @@ private void ServerVariablesParser_Parsing(object sender, Dictionary($"Starting up Handler {syncHandler.Handler.Name}"); - syncHandler.Handler.Initialize(syncHandler.Settings); + + if (globalSettings.ExportAtStartup || (globalSettings.ExportOnSave && !syncFileService.RootExists(globalSettings.RootFolder))) + { + uSyncService.Export(globalSettings.RootFolder, default(SyncHandlerOptions)); + } + + if (globalSettings.ImportAtStartup) + { + uSyncService.Import(globalSettings.RootFolder, false, default(SyncHandlerOptions)); + } + + if (globalSettings.ExportOnSave) + { + var handlers = handlerFactory.GetValidHandlers(new SyncHandlerOptions(handlerFactory.DefaultSet, HandlerActions.Save)); + + foreach (var syncHandler in handlers) + { + logger.Debug($"Starting up Handler {syncHandler.Handler.Name}"); + syncHandler.Handler.Initialize(syncHandler.Settings); + } + } } } + catch(Exception ex) + { + logger.Warn($"Error Importing at startup {ex.Message}"); + } } diff --git a/uSync8.ContentEdition/Serializers/ContentSerializer.cs b/uSync8.ContentEdition/Serializers/ContentSerializer.cs index a69be074..fe0916c9 100644 --- a/uSync8.ContentEdition/Serializers/ContentSerializer.cs +++ b/uSync8.ContentEdition/Serializers/ContentSerializer.cs @@ -1,14 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Xml.Linq; + using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; using Umbraco.Core.Services; + using uSync8.ContentEdition.Mapping; using uSync8.Core; using uSync8.Core.Extensions; @@ -195,7 +195,6 @@ protected override void HandleTrashedState(IContent item, bool trashed) protected virtual Attempt DoSaveOrPublish(IContent item, XElement node) { - var publishedNode = node.Element("Info")?.Element("Published"); if (publishedNode != null) { @@ -216,11 +215,7 @@ protected virtual Attempt DoSaveOrPublish(IContent item, XElement node) if (publishedCultures.Count > 0) { - var culturePublishResult = contentService.SaveAndPublish(item, publishedCultures.ToArray()); - if (culturePublishResult.Success) - return Attempt.Succeed($"Published {publishedCultures.Count} Cultures"); - - return Attempt.Fail("Publish Failed " + culturePublishResult.EventMessages); + return PublishItem(item, publishedCultures.ToArray()); } } else @@ -228,13 +223,9 @@ protected virtual Attempt DoSaveOrPublish(IContent item, XElement node) // default publish the lot. if (publishedNode.Attribute("Default").ValueOrDefault(false)) { - var publishResult = contentService.SaveAndPublish(item); - if (publishResult.Success) - return Attempt.Succeed("Published"); - - return Attempt.Fail("Publish Failed " + publishResult.EventMessages); + return PublishItem(item, null); } - else if (item.Published) + else if (item.Published) { // unpublish contentService.Unpublish(item); @@ -243,11 +234,33 @@ protected virtual Attempt DoSaveOrPublish(IContent item, XElement node) } // if we get here, save + /* var result = contentService.Save(item); - if (result.Success) - return Attempt.Succeed("Saved"); + if (result.Success) */ + + this.SaveItem(item); + return Attempt.Succeed("Saved"); + + // return Attempt.Fail("Save Failed " + result.EventMessages); + } + + private Attempt PublishItem(IContent item, string[] cultures) + { + try + { + var culturePublishResult = contentService.SaveAndPublish(item, cultures); + if (culturePublishResult.Success) + return Attempt.Succeed($"Published {cultures != null: cultures.Count : 'All'} Cultures"); - return Attempt.Fail("Save Failed " + result.EventMessages); + return Attempt.Fail("Publish Failed " + culturePublishResult.EventMessages); + } + catch (ArgumentNullException ex) + { + // we can get thrown a null argument exception by the notifer, + // which is non critical! but we are ignoring this error. ! <= 8.1.5 + if (!ex.Message.Contains("siteUri")) throw ex; + return Attempt.Succeed($"Published"); + } } #endregion @@ -289,7 +302,18 @@ public override void Save(IEnumerable items) => contentService.Save(items); protected override void SaveItem(IContent item) - => contentService.Save(item); + { + try + { + contentService.Save(item); + } + catch (ArgumentNullException ex) + { + // we can get thrown a null argument exception by the notifer, + // which is non critical! but we are ignoring this error. ! <= 8.1.5 + if (!ex.Message.Contains("siteUri")) throw ex; + } + } protected override void DeleteItem(IContent item) => contentService.Delete(item); diff --git a/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs b/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs index e96e8b90..9d4b4d07 100644 --- a/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs +++ b/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs @@ -1,18 +1,16 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Xml.Linq; + using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.Entities; using Umbraco.Core.Services; + using uSync8.ContentEdition.Mapping; -using uSync8.Core; using uSync8.Core.Extensions; -using uSync8.Core.Models; using uSync8.Core.Serialization; namespace uSync8.ContentEdition.Serializers