Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ObjectDisposedException handling for Mediatr notifications in jobs #4682

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Health.Core;
using Microsoft.Health.Core.Features.Context;
using Microsoft.Health.Fhir.Api.Extensions;
using Microsoft.Health.Fhir.Core.Extensions;
using Microsoft.Health.Fhir.Core.Features.Context;

namespace Microsoft.Health.Fhir.Api.Features.ApiNotifications
Expand Down Expand Up @@ -79,7 +80,7 @@ protected virtual async Task PublishNotificationAsync(HttpContext context, Reque
apiNotification.ResourceType = fhirRequestContext.ResourceType;
apiNotification.StatusCode = (HttpStatusCode)context.Response.StatusCode;

await _mediator.Publish(apiNotification, CancellationToken.None);
await _mediator.PublishNotificationWithExceptionHandling(nameof(ApiNotificationMiddleware), apiNotification, _logger, CancellationToken.None);
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task Invoke(HttpContext context)
exceptionNotification.IsRequestRateExceeded = exception.IsRequestRateExceeded();
exceptionNotification.BaseException = exception;

await _mediator.Publish(exceptionNotification, CancellationToken.None);
await _mediator.PublishNotificationWithExceptionHandling(nameof(ExceptionNotificationMiddleware), exceptionNotification, _logger, CancellationToken.None);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Logging;
using Microsoft.Health.Core.Features.Context;
using Microsoft.Health.Extensions.DependencyInjection;
using Microsoft.Health.Fhir.Core.Features.Context;
Expand Down Expand Up @@ -43,7 +44,7 @@ public BulkDeleteProcessingJobTests()
.Returns(Task.FromResult(new SearchResult(5, new List<Tuple<string, string>>())));
_queueClient = Substitute.For<IQueueClient>();
_deleter = Substitute.For<IDeletionService>();
_processingJob = new BulkDeleteProcessingJob(_deleter.CreateMockScopeFactory(), Substitute.For<RequestContextAccessor<IFhirRequestContext>>(), Substitute.For<IMediator>(), _searchService.CreateMockScopeFactory(), _queueClient);
_processingJob = new BulkDeleteProcessingJob(_deleter.CreateMockScopeFactory(), Substitute.For<RequestContextAccessor<IFhirRequestContext>>(), Substitute.For<IMediator>(), _searchService.CreateMockScopeFactory(), _queueClient, Substitute.For<ILogger<BulkDeleteProcessingJob>>());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using System;
using System.Threading;
using System.Threading.Tasks;
using EnsureThat;
using MediatR;
using Microsoft.Extensions.Logging;
using Microsoft.Health.Fhir.Core.Features.Operations.Export;
using Microsoft.Health.Fhir.Core.Messages.Export;
using Microsoft.Health.Fhir.Core.Models;

namespace Microsoft.Health.Fhir.Core.Extensions
{
public static class FhirMediatorExtensions
{
public static async Task PublishNotificationWithExceptionHandling(this IMediator mediator, string metricName, object notification, ILogger logger, CancellationToken cancellationToken)
{
try
{
await mediator.Publish(notification, cancellationToken);
}
catch (ObjectDisposedException ode)
{
if (cancellationToken.IsCancellationRequested)
{
logger.LogWarning(ode, $"ObjectDisposedException. Unable to publish {metricName} metric. Cancellation was requested.");
}
else
{
logger.LogCritical(ode, $"ObjectDisposedException. Unable to publish {metricName} metric.");
}
}
catch (OperationCanceledException oce)
{
logger.LogWarning(oce, $"OperationCanceledException. Unable to publish {metricName} metric. Cancellation was requested.");
}
catch (Exception ex)
{
logger.LogCritical(ex, $"Unable to publish {metricName} metric.");
}
Dismissed Show dismissed Hide dismissed
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
using System.Threading.Tasks;
using EnsureThat;
using MediatR;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Microsoft.Health.Core.Features.Context;
using Microsoft.Health.Extensions.DependencyInjection;
using Microsoft.Health.Fhir.Core.Exceptions;
using Microsoft.Health.Fhir.Core.Extensions;
using Microsoft.Health.Fhir.Core.Features.Context;
using Microsoft.Health.Fhir.Core.Features.Operations.BulkDelete.Messages;
using Microsoft.Health.Fhir.Core.Features.Persistence;
Expand All @@ -33,19 +35,22 @@ public class BulkDeleteProcessingJob : IJob
private readonly IMediator _mediator;
private readonly Func<IScoped<ISearchService>> _searchService;
private readonly IQueueClient _queueClient;
private readonly ILogger<BulkDeleteProcessingJob> _logger;

public BulkDeleteProcessingJob(
Func<IScoped<IDeletionService>> deleterFactory,
RequestContextAccessor<IFhirRequestContext> contextAccessor,
IMediator mediator,
Func<IScoped<ISearchService>> searchService,
IQueueClient queueClient)
IQueueClient queueClient,
ILogger<BulkDeleteProcessingJob> logger)
{
_deleterFactory = EnsureArg.IsNotNull(deleterFactory, nameof(deleterFactory));
_contextAccessor = EnsureArg.IsNotNull(contextAccessor, nameof(contextAccessor));
_mediator = EnsureArg.IsNotNull(mediator, nameof(mediator));
_searchService = EnsureArg.IsNotNull(searchService, nameof(searchService));
_queueClient = EnsureArg.IsNotNull(queueClient, nameof(queueClient));
_logger = EnsureArg.IsNotNull(logger, nameof(logger));
}

public async Task<string> ExecuteAsync(JobInfo jobInfo, CancellationToken cancellationToken)
Expand Down Expand Up @@ -100,7 +105,7 @@ public async Task<string> ExecuteAsync(JobInfo jobInfo, CancellationToken cancel

result.ResourcesDeleted.Add(types[0], numDeleted);

await _mediator.Publish(new BulkDeleteMetricsNotification(jobInfo.Id, numDeleted), cancellationToken);
await _mediator.PublishNotificationWithExceptionHandling(nameof(BulkDeleteProcessingJob), new BulkDeleteMetricsNotification(jobInfo.Id, numDeleted), _logger, cancellationToken);

if (exception != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private async Task CompleteJobAsync(OperationStatus completionStatus, Cancellati
dataSize,
isAnonymizedExport);

await _mediator.Publish(new ExportTaskMetricsNotification(_exportJobRecord), CancellationToken.None);
await _mediator.PublishNotificationWithExceptionHandling(nameof(ExportJobTask), new ExportTaskMetricsNotification(_exportJobRecord), _logger, cancellationToken);
}

private async Task UpdateJobRecordAsync(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,29 +168,7 @@ private async Task AddRequestChargeToFhirRequestContextAsync(double responseRequ
cosmosMetrics.IsThrottled = true;
}

try
{
await _mediator.Publish(cosmosMetrics, cancellationToken);
}
catch (ObjectDisposedException ode)
{
if (cancellationToken.IsCancellationRequested)
{
_logger.LogWarning(ode, "ObjectDisposedException. Unable to publish CosmosDB metric. Cancellation was requested.");
}
else
{
_logger.LogCritical(ode, "ObjectDisposedException. Unable to publish CosmosDB metric.");
}
}
catch (OperationCanceledException oce)
{
_logger.LogWarning(oce, "OperationCanceledException. Unable to publish CosmosDB metric. Cancellation was requested.");
}
catch (Exception ex)
{
_logger.LogCritical(ex, "Unable to publish CosmosDB metric.");
}
await _mediator.PublishNotificationWithExceptionHandling(string.Join(':', [nameof(CosmosResponseProcessor), nameof(AddRequestChargeToFhirRequestContextAsync)]), cosmosMetrics, _logger, cancellationToken);
}

private static string GetCustomerManagedKeyErrorMessage(int subStatusCode)
Expand Down Expand Up @@ -257,18 +235,11 @@ private async Task EmitExceptionNotificationAsync(HttpStatusCode statusCode, Exc
exceptionNotification.BaseException = exception;

await _mediator.Publish(exceptionNotification, cancellationToken);
}
catch (ObjectDisposedException ode)
{
_logger.LogWarning(ode, "ObjectDisposedException. Failure while publishing Exception notification.");
}
catch (OperationCanceledException oce)
{
_logger.LogWarning(oce, "OperationCanceledException. Unable to publish CosmosDB metric. Cancellation was requested.");
await _mediator.PublishNotificationWithExceptionHandling(string.Join(':', [nameof(CosmosResponseProcessor), nameof(EmitExceptionNotificationAsync)]), exceptionNotification, _logger, cancellationToken);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Unable to publish CosmosDB metric.");
_logger.LogCritical(ex, "Unable to publish CosmosDB metric.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ private async Task PublishNotification(Hl7.Fhir.Model.Bundle responseBundle, Bun
});
}

await _mediator.Publish(new BundleMetricsNotification(apiCallResults, bundleType == BundleType.Batch ? AuditEventSubType.Batch : AuditEventSubType.Transaction), CancellationToken.None);
var notification = new BundleMetricsNotification(apiCallResults, bundleType == BundleType.Batch ? AuditEventSubType.Batch : AuditEventSubType.Transaction);
await _mediator.PublishNotificationWithExceptionHandling(nameof(BundleHandler), notification, _logger, CancellationToken.None);
}

private async Task<BundleResponse> ExecuteTransactionForAllRequestsAsync(Hl7.Fhir.Model.Bundle responseBundle, BundleProcessingLogic processingLogic, CancellationToken cancellationToken)
Expand Down
Loading