Skip to content

BackgroundService TaskStatus #35991

Closed

Description

@grahamehorner

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

Activity

  1. davidfowl commented on Jun 17, 2019

    @davidfowl
    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.

  2. kevbry commented on Jul 17, 2019

    @kevbry

    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.

  3. davidfowl commented on Jul 17, 2019

    @davidfowl
    Member

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

  4. kevbry commented on Jul 17, 2019

    @kevbry

    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.

  5. davidfowl commented on Jul 17, 2019

    @davidfowl
    Member

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

  6. francisminu commented on Apr 27, 2020

    @francisminu

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

  7. added
    untriaged
    on May 7, 2020
  8. added this to the Future milestone on May 7, 2020
  9. removed
    untriaged
    on Jun 22, 2020
  10. added
    api-suggestion
    on Aug 20, 2020
  11. modified the milestones: Future, 6.0.0 on Aug 20, 2020
  12. added
    api-ready-for-review
    and removed
    api-suggestion
    on Aug 20, 2020
  13. eerhardt commented on Aug 20, 2020

    @eerhardt
    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.

  14. added
    api-approved
    and removed
    api-ready-for-review
    on Aug 25, 2020
  15. terrajobst commented on Aug 25, 2020

    @terrajobst
    Contributor

    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; }
        }
    }
  16. added a commit that references this issue on Oct 2, 2020
    4cd3a3a
  17. added a commit that references this issue on Oct 6, 2020
    e18d25e
  18. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-Extensions-Hosting

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions