Skip to content

Health Checking / Heartbeat for BackgroundService, IHostedService #47616

Open
@milen-denev

Description

@milen-denev

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Introduction:
I am not quite sure where should I post this feature request, but because the nuget packages that I am trying to use to achieve this results lead to this repo, I am posting here my proposition.

What my configuration is:
I have a Host app, running as WindowsService on a server, with several services added using .AddHostedService<>()
Also I have services.Configure -> BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore
What I am trying to achieve:
Sometimes my services may crash due to dynamic changes, and misconfiguration and mostly due to the fact that I am not using try catch patterns. In case a background service crashes I get an event in the Event Viewer, but this has to be manually checked, or reported by users that something isn't running at all or use try finally to make the service report itself.
In case if there is permanent issue that has to be resolved manually, it may be better to leave the BackgroundService "crashed" / inactive and after the issue is fixed, having the whole service restarted in order to be updated by the "patching" service.

Describe the solution you'd like

  1. The solution that I would love to see is to be able from other service or from other place (ex. in the Main() method) to check the status of the BackgroundService, ex. if it's running, starting, stopping, inactive.
  2. It may be great to be able to have another method within the service itself, that runs in parallel to the main thread / Task, and allows to be used like heartbeat that reports somewhere that the service is alive, and still use the Task.Delay() to configure how frequenlty to give a heartbeat.
  3. A control over the services like Something.Stop() / Start() / Pause() may be useful (ex. updating some app that the service depends on and pausing until it's done).
  4. Some straightforward way to collect "diagnostic" or other data from the main thread, like how much time took for the latest query to execute, like back-and-forth thingie between the hearbeat thread and main the thread.

Additional context

Currently I digged as much as I can and I didn't find absolutely anything that could help me with monitoring, controlling or at least getting the state of a BackgroundService (without using Azure Monitoring). Azure Monitoring and Application Insights it's a great service , but it's not free (1.94 EUR for log injection per GB) and if I want to actully use this data for something outside the Azure portal, I have to use the LogAnalytics API and kusto query language. If there is any other way except this one, I would be very happy to hear it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-healthchecksIncludes: Healthchecks (some bugs also in Extensions repo)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions