Skip to content

Commit

Permalink
Ensure Context so we can publish from outside of a request.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Sep 27, 2019
1 parent 1d1c45c commit 0352af0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 59 deletions.
2 changes: 0 additions & 2 deletions uSync8.BackOffice/uSync8BackOffice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 23 additions & 19 deletions uSync8.BackOffice/uSyncBackofficeComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -44,6 +47,8 @@ public uSyncBackofficeComponent(
this.syncFileService = fileService;
this.uSyncService = uSyncService;

this.umbracoContextFactory = umbracoContextFactory;

}

public void Initialize()
Expand Down Expand Up @@ -86,37 +91,36 @@ 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<uSyncBackofficeComponent>($"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<uSyncBackofficeComponent>($"Starting up Handler {syncHandler.Handler.Name}");
syncHandler.Handler.Initialize(syncHandler.Settings);
}
}
}
}
catch(Exception ex)
{
logger.Warn<uSyncBackofficeComponent>($"Error Importing at startup {ex.Message}");
}
finally
{
uSync8BackOffice.inStartup = true;
}

}

Expand Down
83 changes: 45 additions & 38 deletions uSync8.ContentEdition/Serializers/ContentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,52 +196,40 @@ protected override void HandleTrashedState(IContent item, bool trashed)

protected virtual Attempt<string> 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<string>();
foreach (var culturePublish in publishedNode.Elements("Published"))
{
// culture based publishing.
var publishedCultures = new List<string>();
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);
}
}
}
Expand All @@ -257,6 +245,25 @@ protected virtual Attempt<string> DoSaveOrPublish(IContent item, XElement node)
// 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("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)
Expand Down

0 comments on commit 0352af0

Please sign in to comment.