Skip to content

Commit

Permalink
Added PublisherCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Cerqueira committed Feb 20, 2024
1 parent ce6631d commit f9f7871
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using HealthCheckPlus;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace HealthCheckPlusDemoBackgroudService
{
public class SamplePublishHealth : IHealthCheckPublisher
public class SamplePublishHealth : IHealthCheckPublisher, IHealthCheckPlusPublisher
{
public Func<HealthReport, bool> PublisherCondition => (_) => true;
public Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
{
return Task.CompletedTask;
Expand Down
2 changes: 2 additions & 0 deletions docs/apis/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

- [IDataHealthPlus](./healthcheckplus.idatahealthplus.md)

- [IHealthCheckPlusPublisher](./healthcheckplus.ihealthcheckpluspublisher.md)

- [IStateHealthChecksPlus](./healthcheckplus.istatehealthchecksplus.md)

## Namespace HealthCheckPlus.options
Expand Down
36 changes: 36 additions & 0 deletions docs/apis/healthcheckplus.ihealthcheckpluspublisher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# <img align="left" width="100" height="100" src="../images/icon.png">HealthCheckPlus API:IHealthCheckPlusPublisher

[![Build](https://github.com/FRACerqueira/HealthCheckPlus/workflows/Build/badge.svg)](https://github.com/FRACerqueira/HealthCheckPlus/actions/workflows/build.yml)
[![License](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://github.com/FRACerqueira/HealthCheckPlus/blob/master/LICENSE)
[![NuGet](https://img.shields.io/nuget/v/HealthCheckPlus)](https://www.nuget.org/packages/HealthCheckPlus/)
[![Downloads](https://img.shields.io/nuget/dt/HealthCheckPlus)](https://www.nuget.org/packages/HealthCheckPlus/)

[**Back to List Api**](./apis.md)

# IHealthCheckPlusPublisher

Namespace: HealthCheckPlus

Represents a publisher of information.

```csharp
public interface IHealthCheckPlusPublisher
```

## Properties

### <a id="properties-publishercondition"/>**PublisherCondition**

The Publisher Condition to execute. Default value is null (always run)

```csharp
public abstract Func<HealthReport, Boolean> PublisherCondition { get; }
```

#### Property Value

[Func&lt;HealthReport, Boolean&gt;](https://docs.microsoft.com/en-us/dotnet/api/system.func-2)<br>


- - -
[**Back to List Api**](./apis.md)
20 changes: 20 additions & 0 deletions src/IHealthCheckPlusPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace HealthCheckPlus
{
/// <summary>
/// Represents a publisher of <see cref="HealthReport"/> information.
/// </summary>
public interface IHealthCheckPlusPublisher
{
/// <summary>
/// The Publisher Condition to execute. Default value is null (always run)
/// </summary>
public Func<HealthReport, bool> PublisherCondition { get; }
}
}
16 changes: 15 additions & 1 deletion src/Internal/HealthCheckPlusBackGroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,21 @@ private async void CheckHealthAsync()
var tasks = new Task[_publishers.Length];
for (var i = 0; i < _publishers.Length; i++)
{
tasks[i] = RunPublisherAsync(_publishers[i], report, _stopping.Token);
if (_publishers[i].GetType().GetInterface(nameof(IHealthCheckPlusPublisher)) != null)
{
if (((IHealthCheckPlusPublisher)_publishers[i]).PublisherCondition.Invoke(report))
{
tasks[i] = RunPublisherAsync(_publishers[i], report, _stopping.Token);
}
else
{
tasks[i] = Task.Run(() => { });
}
}
else
{
tasks[i] = RunPublisherAsync(_publishers[i], report, _stopping.Token);
}
}
await Task.WhenAll(tasks).ConfigureAwait(false);
}
Expand Down

0 comments on commit f9f7871

Please sign in to comment.