You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
As a developer I'm unable able to determine if a registered BackgroundService is running and/or has ran to competition or faulted.
Describe the solution you'd like
The based abstract class should have a TaskStatus property added which shows the status of the ExecuteAsync task.
Proposed API
namespace Microsoft.Extensions.Hosting
{
/// <summary>
/// Base class for implementing a long running <see cref="IHostedService"/>.
/// </summary>
public abstract class BackgroundService
{
+ /// <summary>+ /// Gets the Task that is executing the background operation.+ /// </summary>+ public Task ExecutingTask;
}
}
I'm frequently building queue workers on BackgroundService, and having to build the infrastructure for stop/pause/restart and status myself. Would be valuable to have some of that control/status reporting built into the framework, or at least some method of interacting with the service.
Is hosted service lifecycle control never going to be a feature? Felt to me as though exposing status would have necessitated some sort of stop/start control aside from restarting the entire host. Pause I can totally understand since that's pretty implementation specific.
One of my specific use cases is a set of three backgroundservice that consume from a queue and feed work to each-other. Would like to be able to shut them down in a specific order on command, then potentially re-start them. Right now I can achieve that with a shared object with flags to indicate if each service should continue to do work (pause), but it felt as though having access to the state of each service and some method of triggering StopAsync and waiting for each shutdown to complete could make that simpler. That'd end up depending on having the ability to re-start at some point as well.
I'd imagine that ensuring only a single instance each service was active at any time would get more difficult if this was the case.
Activity
davidfowl commentedon Jun 17, 2019
This is a good issue and ties into the improvements I'm going to make. Now I have another good reason to expose the task of the ExecuteAsync method.
kevbry commentedon Jul 17, 2019
I'm frequently building queue workers on BackgroundService, and having to build the infrastructure for stop/pause/restart and status myself. Would be valuable to have some of that control/status reporting built into the framework, or at least some method of interacting with the service.
davidfowl commentedon Jul 17, 2019
That has to be built on top, you can't pause them or restart them.
kevbry commentedon Jul 17, 2019
Is hosted service lifecycle control never going to be a feature? Felt to me as though exposing status would have necessitated some sort of stop/start control aside from restarting the entire host. Pause I can totally understand since that's pretty implementation specific.
One of my specific use cases is a set of three backgroundservice that consume from a queue and feed work to each-other. Would like to be able to shut them down in a specific order on command, then potentially re-start them. Right now I can achieve that with a shared object with flags to indicate if each service should continue to do work (pause), but it felt as though having access to the state of each service and some method of triggering StopAsync and waiting for each shutdown to complete could make that simpler. That'd end up depending on having the ability to re-start at some point as well.
I'd imagine that ensuring only a single instance each service was active at any time would get more difficult if this was the case.
davidfowl commentedon Jul 17, 2019
That’s correct it’s never going to be a feature. Any task scheduler like thing is likely to be built on top.
francisminu commentedon Apr 27, 2020
Hi David @davidfowl
Is there any update on the TaskStatus implementation?
eerhardt commentedon Aug 20, 2020
Updating the top-post to make an official API proposal, and marking it ready-for-review.
Exposing this Task is generally useful to a few different scenarios - ex. it is blocking #36017.
terrajobst commentedon Aug 25, 2020
Video
ExecuteTaskto make it clear that's the task returned byExecuteAsyncrather than being some transient task.Expose BackgroundService.ExecuteTask
Expose BackgroundService.ExecuteTask (#42981)