Skip to content

Commit

Permalink
V8/content startup issues (#56)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
KevinJump authored Sep 27, 2019
1 parent 08dbabe commit 475d734
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 41 deletions.
55 changes: 36 additions & 19 deletions uSync8.BackOffice/uSyncBackofficeComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,28 @@ 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;

this.syncFileService = fileService;
this.uSyncService = uSyncService;

this.umbracoContextFactory = umbracoContextFactory;

}

public void Initialize()
Expand Down Expand Up @@ -82,28 +87,40 @@ private void ServerVariablesParser_Parsing(object sender, Dictionary<string, obj
});
}

private void InitBackOffice()
private void InitBackOffice()
{
if (globalSettings.ExportAtStartup || (globalSettings.ExportOnSave && !syncFileService.RootExists(globalSettings.RootFolder)))
{
uSyncService.Export(globalSettings.RootFolder, default(SyncHandlerOptions));
}

if (globalSettings.ImportAtStartup)
try
{
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)
using (var reference = umbracoContextFactory.EnsureUmbracoContext())
{
logger.Debug<uSyncBackofficeComponent>($"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<uSyncBackofficeComponent>($"Starting up Handler {syncHandler.Handler.Name}");
syncHandler.Handler.Initialize(syncHandler.Settings);
}
}
}
}
catch(Exception ex)
{
logger.Warn<uSyncBackofficeComponent>($"Error Importing at startup {ex.Message}");
}

}

Expand Down
60 changes: 42 additions & 18 deletions uSync8.ContentEdition/Serializers/ContentSerializer.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -195,7 +195,6 @@ protected override void HandleTrashedState(IContent item, bool trashed)

protected virtual Attempt<string> DoSaveOrPublish(IContent item, XElement node)
{

var publishedNode = node.Element("Info")?.Element("Published");
if (publishedNode != null)
{
Expand All @@ -216,25 +215,17 @@ protected virtual Attempt<string> 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
{
// 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);
Expand All @@ -243,11 +234,33 @@ protected virtual Attempt<string> 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<string> 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
Expand Down Expand Up @@ -289,7 +302,18 @@ public override void Save(IEnumerable<IContent> 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);
Expand Down
6 changes: 2 additions & 4 deletions uSync8.ContentEdition/Serializers/ContentSerializerBase.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 475d734

Please sign in to comment.