diff --git a/uSync8.BackOffice/uSync8BackOffice.cs b/uSync8.BackOffice/uSync8BackOffice.cs index 12728dca..47bc2797 100644 --- a/uSync8.BackOffice/uSync8BackOffice.cs +++ b/uSync8.BackOffice/uSync8BackOffice.cs @@ -9,8 +9,6 @@ namespace uSync8.BackOffice public class uSync8BackOffice { public static bool eventsPaused { get; set; } - - public static bool inStartup { get; set; } } internal class uSync diff --git a/uSync8.BackOffice/uSyncBackofficeComponent.cs b/uSync8.BackOffice/uSyncBackofficeComponent.cs index 2ea236b3..0bff24ef 100644 --- a/uSync8.BackOffice/uSyncBackofficeComponent.cs +++ b/uSync8.BackOffice/uSyncBackofficeComponent.cs @@ -27,12 +27,15 @@ public class uSyncBackofficeComponent : IComponent private readonly uSyncService uSyncService; private readonly IRuntimeState runtimeState; + private readonly IUmbracoContextFactory umbracoContextFactory; + public uSyncBackofficeComponent( SyncHandlerFactory handlerFactory, IProfilingLogger logger, SyncFileService fileService, uSyncService uSyncService, - IRuntimeState runtimeState) + IRuntimeState runtimeState, + IUmbracoContextFactory umbracoContextFactory) { globalSettings = Current.Configs.uSync(); @@ -44,6 +47,8 @@ public uSyncBackofficeComponent( this.syncFileService = fileService; this.uSyncService = uSyncService; + this.umbracoContextFactory = umbracoContextFactory; + } public void Initialize() @@ -86,26 +91,29 @@ private void InitBackOffice() { try { - uSync8BackOffice.inStartup = true; - if (globalSettings.ExportAtStartup || (globalSettings.ExportOnSave && !syncFileService.RootExists(globalSettings.RootFolder))) + using (var reference = umbracoContextFactory.EnsureUmbracoContext()) { - uSyncService.Export(globalSettings.RootFolder, default(SyncHandlerOptions)); - } - if (globalSettings.ImportAtStartup) - { - uSyncService.Import(globalSettings.RootFolder, false, default(SyncHandlerOptions)); - } + if (globalSettings.ExportAtStartup || (globalSettings.ExportOnSave && !syncFileService.RootExists(globalSettings.RootFolder))) + { + uSyncService.Export(globalSettings.RootFolder, default(SyncHandlerOptions)); + } - if (globalSettings.ExportOnSave) - { - var handlers = handlerFactory.GetValidHandlers(new SyncHandlerOptions(handlerFactory.DefaultSet, HandlerActions.Save)); + if (globalSettings.ImportAtStartup) + { + uSyncService.Import(globalSettings.RootFolder, false, default(SyncHandlerOptions)); + } - foreach (var syncHandler in handlers) + if (globalSettings.ExportOnSave) { - logger.Debug($"Starting up Handler {syncHandler.Handler.Name}"); - syncHandler.Handler.Initialize(syncHandler.Settings); + 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); + } } } } @@ -113,10 +121,6 @@ private void InitBackOffice() { logger.Warn($"Error Importing at startup {ex.Message}"); } - finally - { - uSync8BackOffice.inStartup = true; - } } diff --git a/uSync8.ContentEdition/Serializers/ContentSerializer.cs b/uSync8.ContentEdition/Serializers/ContentSerializer.cs index df97f5f3..ced2526d 100644 --- a/uSync8.ContentEdition/Serializers/ContentSerializer.cs +++ b/uSync8.ContentEdition/Serializers/ContentSerializer.cs @@ -196,52 +196,40 @@ protected override void HandleTrashedState(IContent item, bool trashed) protected virtual Attempt DoSaveOrPublish(IContent item, XElement node) { - // at the moment <= 8.1.5 we can't publish during startup. - if (!uSync8BackOffice.inStartup) + var publishedNode = node.Element("Info")?.Element("Published"); + if (publishedNode != null) { - var publishedNode = node.Element("Info")?.Element("Published"); - if (publishedNode != null) + if (publishedNode.HasElements) { - if (publishedNode.HasElements) + // culture based publishing. + var publishedCultures = new List(); + foreach (var culturePublish in publishedNode.Elements("Published")) { - // culture based publishing. - var publishedCultures = new List(); - foreach (var culturePublish in publishedNode.Elements("Published")) - { - var culture = culturePublish.Attribute("Culture").ValueOrDefault(string.Empty); - var status = culturePublish.ValueOrDefault(false); - - if (!string.IsNullOrWhiteSpace(culture) && status) - { - publishedCultures.Add(culture); - } - } + var culture = culturePublish.Attribute("Culture").ValueOrDefault(string.Empty); + var status = culturePublish.ValueOrDefault(false); - if (publishedCultures.Count > 0) + if (!string.IsNullOrWhiteSpace(culture) && status) { - var culturePublishResult = contentService.SaveAndPublish(item, publishedCultures.ToArray()); - if (culturePublishResult.Success) - return Attempt.Succeed($"Published {publishedCultures.Count} Cultures"); - - return Attempt.Fail("Publish Failed " + culturePublishResult.EventMessages); + publishedCultures.Add(culture); } } - else - { - // 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); - } - else if (item.Published) - { - // unpublish - contentService.Unpublish(item); - } + if (publishedCultures.Count > 0) + { + return PublishItem(item, publishedCultures.ToArray()); + } + } + else + { + // default publish the lot. + if (publishedNode.Attribute("Default").ValueOrDefault(false)) + { + return PublishItem(item, null); + } + else if (item.Published) + { + // unpublish + contentService.Unpublish(item); } } } @@ -257,6 +245,25 @@ protected virtual Attempt DoSaveOrPublish(IContent item, XElement node) // 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("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 protected override IContent CreateItem(string alias, ITreeEntity parent, string itemType)