Skip to content

Commit 3a83e4a

Browse files
AndyButlandlauraneto
authored andcommitted
Publishing: Resolve exceptions on publish branch (#20464)
* Reduce log level of image cropper converter to avoid flooding logs with expected exceptions. * Don't run publish branch long running operation on a background thread such that UmbracoContext is available. * Revert to background thread and use EnsureUmbracoContext to ensure we can get an IUmbracoContext in the URL providers. * Updated tests. * Applied suggestion from code review. * Clarified comment.
1 parent af3b172 commit 3a83e4a

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/Umbraco.Core/Services/ContentPublishingService.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Umbraco.Cms.Core.Models.ContentPublishing;
99
using Umbraco.Cms.Core.Scoping;
1010
using Umbraco.Cms.Core.Services.OperationStatus;
11+
using Umbraco.Cms.Core.Web;
1112
using Umbraco.Extensions;
1213

1314
namespace Umbraco.Cms.Core.Services;
@@ -26,6 +27,7 @@ internal sealed class ContentPublishingService : IContentPublishingService
2627
private readonly IRelationService _relationService;
2728
private readonly ILogger<ContentPublishingService> _logger;
2829
private readonly ILongRunningOperationService _longRunningOperationService;
30+
private readonly IUmbracoContextFactory _umbracoContextFactory;
2931

3032
public ContentPublishingService(
3133
ICoreScopeProvider coreScopeProvider,
@@ -37,7 +39,8 @@ public ContentPublishingService(
3739
IOptionsMonitor<ContentSettings> optionsMonitor,
3840
IRelationService relationService,
3941
ILogger<ContentPublishingService> logger,
40-
ILongRunningOperationService longRunningOperationService)
42+
ILongRunningOperationService longRunningOperationService,
43+
IUmbracoContextFactory umbracoContextFactory)
4144
{
4245
_coreScopeProvider = coreScopeProvider;
4346
_contentService = contentService;
@@ -53,6 +56,7 @@ public ContentPublishingService(
5356
{
5457
_contentSettings = contentSettings;
5558
});
59+
_umbracoContextFactory = umbracoContextFactory;
5660
}
5761

5862
/// <inheritdoc />
@@ -280,7 +284,7 @@ Attempt<ContentPublishingBranchInternalResult, ContentPublishingOperationStatus>
280284
return MapInternalPublishingAttempt(minimalAttempt);
281285
}
282286

283-
_logger.LogInformation("Starting async background thread for publishing branch.");
287+
_logger.LogDebug("Starting long running operation for publishing branch {Key} on background thread.", key);
284288
Attempt<Guid, LongRunningOperationEnqueueStatus> enqueueAttempt = await _longRunningOperationService.RunAsync(
285289
PublishBranchOperationType,
286290
async _ => await PerformPublishBranchAsync(key, cultures, publishBranchFilter, userKey, returnContent: false),
@@ -314,6 +318,10 @@ private async Task<Attempt<ContentPublishingBranchInternalResult, ContentPublish
314318
Guid userKey,
315319
bool returnContent)
316320
{
321+
// Ensure we have an UmbracoContext in case running on a background thread so operations that run in the published notification handlers
322+
// have access to this (e.g. webhooks).
323+
using UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext();
324+
317325
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
318326
IContent? content = _contentService.GetById(key);
319327
if (content is null)

src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/ImageCropperValueConverter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Umbraco.
22
// See LICENSE for more details.
33

4+
using System.Text.Json;
45
using Microsoft.Extensions.Logging;
56
using Umbraco.Cms.Core.Models.DeliveryApi;
67
using Umbraco.Cms.Core.Models.PublishedContent;
@@ -53,10 +54,10 @@ public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType
5354
{
5455
value = _jsonSerializer.Deserialize<ImageCropperValue>(sourceString);
5556
}
56-
catch (Exception ex)
57+
catch (JsonException ex)
5758
{
58-
// cannot deserialize, assume it may be a raw image URL
59-
_logger.LogError(ex, "Could not deserialize string '{JsonString}' into an image cropper value.", sourceString);
59+
// Cannot deserialize, assume it may be a raw image URL.
60+
_logger.LogDebug(ex, "Could not deserialize string '{JsonString}' into an image cropper value.", sourceString);
6061
value = new ImageCropperValue { Src = sourceString };
6162
}
6263

0 commit comments

Comments
 (0)