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

Implement typed generic wrapper for IBackgroundServiceHealthService #8

Open
Anders-Toegersen opened this issue Sep 22, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@Anders-Toegersen
Copy link
Contributor

Anders-Toegersen commented Sep 22, 2023

Background:

The current implementation of IBackgroundServiceHealthService uses a string (serviceName) to register and manage the health check state of a background service. As the background services are registered as singletons, we can use the type to make a unique service name.

Proposed Changes:

  1. Implement IBackgroundServiceHealthService<TBackgroundService> that utilizes the type TBackgroundService to create a unique name to call into IBackgroundServiceHealthService.

Impact:

  • Increased ease of use as we reduce the boilerplate of finding the serviceName for each call.
  • Reduced chance of errors as the call can be made more consistent.
  • Added complexity for service registration of IBackgroundServiceHealthService<TBackgroundService>
@Anders-Toegersen
Copy link
Contributor Author

Anders-Toegersen commented Sep 22, 2023

The new IBackgroundServiceHealthService<TBackgroundService> could look like this:

public interface IBackgroundServiceHealthService<TBackgroundService>
{
    void SetMaxStalenessInSeconds(ushort seconds);

    void SetRunningState(bool isRunning);

    bool IsServiceRunning();
}

and be consumed like this:

public class MyBackgroundService : BackgroundServiceBase<MyBackgroundService>
{
    private readonly IBackgroundServiceHealthService<MyBackgroundService> healthService;

    public MyBackgroundService(IBackgroundServiceHealthService<MyBackgroundService> healthService)
    {
        this.healthService = healthService;
    }

    public override async Task DoWorkAsync(CancellationToken stoppingToken)
    {
        ....
        healthService.SetRunningState(true);
        ....
    }
}

@Anders-Toegersen Anders-Toegersen added the enhancement New feature or request label Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant