Skip to content

Commit

Permalink
chore: added more null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
bielu1 committed Apr 2, 2024
1 parent 87cc495 commit 6301f2f
Showing 1 changed file with 64 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace bielu.Umbraco.Cdn.Core.NotitificationHandlers.Content
{
public class ContentEventsNotificationNotificationHandler : INotificationAsyncHandler<ContentMovingNotification>,
public class ContentEventsNotificationNotificationHandler : INotificationAsyncHandler<ContentMovedNotification>,
INotificationAsyncHandler<ContentSavedNotification>,
INotificationAsyncHandler<ContentDeletingNotification>,
INotificationAsyncHandler<ContentPublishingNotification>,
Expand All @@ -30,7 +30,7 @@ public class ContentEventsNotificationNotificationHandler : INotificationAsyncHa
private readonly IEnumerable<ICdnService> _cdnServices;
private readonly ILogger<ContentEventsNotificationNotificationHandler> _logger;
private readonly ICdnAuditService _auditService;
private BieluCdnOptions _configuration;
private BieluCdnOptions _configuration;

public ContentEventsNotificationNotificationHandler(IUmbracoUrlDeliveryService umbracoUrlDeliveryService,
IEnumerable<ICdnService> cdnServices, ILogger<ContentEventsNotificationNotificationHandler> logger,
Expand All @@ -42,45 +42,51 @@ public ContentEventsNotificationNotificationHandler(IUmbracoUrlDeliveryService u
_auditService = auditService;
_configuration = configuration.CurrentValue;
configuration.OnChange((options, name) => { _configuration = options; });

}

public async Task HandleAsync(ContentMovingNotification notification, CancellationToken cancellationToken)
public async Task HandleAsync(ContentMovedNotification notification, CancellationToken cancellationToken)
{
var pages = new List<string>();
foreach (var content in notification.MoveInfoCollection)
try
{
pages.AddRange(await GetPages(content.Entity));
}
var pages = new List<string>();
foreach (var content in notification.MoveInfoCollection)
{
pages.AddRange(await GetPages(content.Entity));
}

//todo: optimize as now we dont valide which domains is valid for either of cdns
foreach (var cdnServices in _cdnServices.Where(x=>x.IsEnabled()))
{
var result = Task.Run(async () => { return await cdnServices.PurgePages(pages); }).Result;
var errors = false;
EventMessage message;
if (result.Any(x => !x.Success))
//todo: optimize as now we dont valide which domains is valid for either of cdns
foreach (var cdnServices in _cdnServices.Where(x => x.IsEnabled()))
{
foreach (var resultStatus in result.Where(x => !x.Success))
var result = Task.Run(async () => { return await cdnServices.PurgePages(pages); }).Result;
var errors = false;
EventMessage message;
if (result.Any(x => !x.Success))
{
if (resultStatus.MessageType == EventMessageType.Error)
foreach (var resultStatus in result.Where(x => !x.Success))
{
_logger.LogError(resultStatus.Exception, resultStatus.Message);
errors = true;
if (resultStatus.MessageType == EventMessageType.Error)
{
_logger.LogError(resultStatus.Exception, resultStatus.Message);
errors = true;
}
}
}


message = new EventMessage("CDN", result.FirstOrDefault(x => !x.Success).Message,
EventMessageType.Error);
}
else
{
message = new EventMessage("CDN", result.FirstOrDefault(x => x.Success).Message,
EventMessageType.Info);
}
message = new EventMessage("CDN", result.FirstOrDefault(x => !x.Success).Message,
EventMessageType.Error);
}
else
{
message = new EventMessage("CDN", result.FirstOrDefault(x => x.Success).Message,
EventMessageType.Info);
}

notification.Messages.Add(message);
notification.Messages.Add(message);
}
}
catch (Exception e)
{
_logger.LogError(e, "Exception on refreshing on move");
}
}

Expand All @@ -89,7 +95,7 @@ private async Task<List<string>> GetPages(IContent content)
var pages = new List<string>();
if (_configuration.Auditing)
{
await _auditService.LogRefresh( content.Id);
await _auditService.LogRefresh(content.Id);
}

pages.AddRange(_umbracoUrlDeliveryService.GetUrlsByContent(content));
Expand All @@ -100,6 +106,7 @@ private async Task<List<string>> GetPages(IContent content)

return pages;
}

public async Task HandleAsync(ContentDeletingNotification notification, CancellationToken cancellationToken)
{
var pages = new List<string>();
Expand All @@ -110,7 +117,7 @@ public async Task HandleAsync(ContentDeletingNotification notification, Cancella
}

//todo: optimize as now we dont valide which domains is valid for either of cdns
foreach (var cdnServices in _cdnServices.Where(x=>x.IsEnabled()))
foreach (var cdnServices in _cdnServices.Where(x => x.IsEnabled()))
{
try
{
Expand Down Expand Up @@ -206,15 +213,25 @@ public async Task HandleAsync(ContentPublishedNotification notification, Cancell
var pages = new List<string>();
foreach (var content in notification.PublishedEntities)
{
pages.AddRange(await GetPages(content));
try
{
pages.AddRange(await GetPages(content));
}
catch (Exception e)
{
_logger.LogError(e, "failed to get url for {Id}", content.Id);
}
}

try

//todo: optimize as now we dont valide which domains is valid for either of cdns
foreach (var cdnServices in _cdnServices.Where(x => x.IsEnabled()))
{
//todo: optimize as now we dont valide which domains is valid for either of cdns
foreach (var cdnServices in _cdnServices.Where(x=>x.IsEnabled()))
try
{
var result = Task.Run(async () => { return await cdnServices.PurgePages(pages); }).Result;
if (result == null)
continue;
EventMessage message;

foreach (var resultStatus in result)
Expand All @@ -240,12 +257,13 @@ public async Task HandleAsync(ContentPublishedNotification notification, Cancell
EventMessageType.Info);
}


notification.Messages.Add(message);
}
}
catch (Exception e)
{
_logger.LogError(e, "failed on handling publish ");
catch (Exception e)
{
_logger.LogError(e, "failed on handling publish with {Provider}", cdnServices.GetType().Name);
}
}
}

Expand All @@ -264,7 +282,7 @@ public async Task HandleAsync(ContentPublishingNotification notification, Cancel
{
try
{
var pages = new List<string>();
var pages = new List<string>();


foreach (var content in notification.PublishedEntities)
Expand Down Expand Up @@ -292,15 +310,15 @@ public async Task HandleAsync(ContentSavedNotification notification, Cancellatio
try
{
var pages = new List<string>();
foreach (var content in notification.SavedEntities)
foreach (var content in notification.SavedEntities)
{
if (content.Published)
{
pages.AddRange(await GetPages(content));
}
}

foreach (var cdnServices in _cdnServices.Where(x=>x.IsEnabled()))
foreach (var cdnServices in _cdnServices.Where(x => x.IsEnabled()))
{
var result = Task.Run(async () => { return await cdnServices.PurgePages(pages); }).Result.ToList();
EventMessage message;
Expand All @@ -319,12 +337,14 @@ public async Task HandleAsync(ContentSavedNotification notification, Cancellatio

if (result.Any(x => !x.Success))
{
message = new EventMessage("CDN", result.FirstOrDefault(x => !x.Success)?.Message ?? "Some urls failed to purge",
message = new EventMessage("CDN",
result.FirstOrDefault(x => !x.Success)?.Message ?? "Some urls failed to purge",
EventMessageType.Error);
}
else
{
message = new EventMessage("CDN", result.FirstOrDefault(x => x.Success)?.Message ?? "All urls purged",
message = new EventMessage("CDN",
result.FirstOrDefault(x => x.Success)?.Message ?? "All urls purged",
EventMessageType.Info);
}

Expand Down

0 comments on commit 6301f2f

Please sign in to comment.