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

BackgroundService TaskStatus #35991

Closed
grahamehorner opened this issue Jun 17, 2019 · 9 comments · Fixed by #42981
Closed

BackgroundService TaskStatus #35991

grahamehorner opened this issue Jun 17, 2019 · 9 comments · Fixed by #42981
Labels
api-approved API was approved in API review, it can be implemented area-Extensions-Hosting
Milestone

Comments

@grahamehorner
Copy link

grahamehorner commented Jun 17, 2019

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;
    }
}

Usage example

BackgroundService s = ...;
s.ExecutingTask.ContinueWith(t =>
{
    if (t.IsFaulted)
    {
        Console.WriteLine(t.Exception.ToString());
    }
});
@davidfowl davidfowl transferred this issue from dotnet/aspnetcore Jun 17, 2019
@davidfowl
Copy link
Member

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
Copy link

kevbry commented 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
Copy link
Member

That has to be built on top, you can't pause them or restart them.

@kevbry
Copy link

kevbry commented 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
Copy link
Member

That’s correct it’s never going to be a feature. Any task scheduler like thing is likely to be built on top.

@francisminu
Copy link

Hi David @davidfowl
Is there any update on the TaskStatus implementation?

@analogrelay analogrelay transferred this issue from dotnet/extensions May 7, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Threading.Tasks untriaged New issue has not been triaged by the area owner labels May 7, 2020
@ghost
Copy link

ghost commented May 7, 2020

Tagging subscribers to this area: @tarekgh
Notify danmosemsft if you want to be subscribed.

@analogrelay analogrelay added this to the Future milestone May 7, 2020
@ericstj ericstj removed the untriaged New issue has not been triaged by the area owner label Jun 22, 2020
@eerhardt eerhardt added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Aug 20, 2020
@eerhardt eerhardt modified the milestones: Future, 6.0.0 Aug 20, 2020
@eerhardt eerhardt added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Aug 20, 2020
@eerhardt
Copy link
Member

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 terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Aug 25, 2020
@terrajobst
Copy link
Member

terrajobst commented Aug 25, 2020

Video

  • Should be a property ;-)
  • Let's rename it to ExecuteTask to make it clear that's the task returned by ExecuteAsync rather than being some transient task.
namespace Microsoft.Extensions.Hosting
{
    public abstract class BackgroundService
    {
        public virtual Task ExecuteTask { get; }
    }
}

eerhardt added a commit to eerhardt/runtime that referenced this issue Oct 2, 2020
Expose the task that executes the background service, so consumers can check if it is running and/or has ran to competition or faulted.

Use the new ExecuteTask to log exceptions when a BackgroundService fails after await, instead of appearing to hang.

Fix dotnet#35991
Fix dotnet#36017
eerhardt added a commit that referenced this issue Oct 6, 2020
* Expose BackgroundService.ExecuteTask

Expose the task that executes the background service, so consumers can check if it is running and/or has ran to competition or faulted.

Use the new ExecuteTask to log exceptions when a BackgroundService fails after await, instead of appearing to hang.

Fix #35991
Fix #36017
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-Extensions-Hosting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants