Open Closed

ICancellationTokenProvider for Background Worker? #4293


User avatar
0
MichelZ created
  • ABP Framework version: v6.0.0
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

We are using ICancellationTokenProvider for our repositories with the GetCancellationToken() method, so we don't have to sprinkle the CancellationTokens into every method. This works well with the HttpContext Token Provider.

We would like to do the same thing with Background Workers (we are using HangfireBackgroundWorkerBase) - but we are not sure how to... any pointers? Or do we need to convert our Repositories to take CancellationTokens as parameters for this to work properly?

Thanks


7 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    We will add this feature in next version

    See https://github.com/abpframework/abp/pull/15282

  • User Avatar
    0
    MichelZ created

    Thanks, but this is not what I'm asking (I think)

    Background WORKERS already support CancellationTokens:

    public abstract class HangfireBackgroundWorkerBase : BackgroundWorkerBase, IHangfireBackgroundWorker`
    {
        public string RecurringJobId { get; set; }
        public string CronExpression { get; set; }
        public TimeZoneInfo TimeZone { get; set; }
        public string Queue { get; set; }
        public abstract Task DoWorkAsync(CancellationToken cancellationToken = default);
        protected HangfireBackgroundWorkerBase()
       {
            TimeZone = null;
            Queue = "default";
        }
    }
    

    But here, I have to pass the CancellationToken down to my repository methods (which I don't have to do in the normal Http project, as it uses the HttpContextCancellationTokenProvider)

    My question is if I can somehow make BackgroundWorkers work the same, without adding CancellationToken overloads to the Repository methods, as my Repository methods use GetCancellationToken() to pass the token from the ICancellationTokenProvider

    Thanks

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, I will check this.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    The AddBackgroundWorkerAsync method has a CancellationToken parameter.

    https://github.com/abpframework/abp/blob/6cd1e05fdc0d15888d2e25f1cb0765f1e8db9008/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs#L11

    https://github.com/abpframework/abp/pull/12420

  • User Avatar
    0
    MichelZ created

    Hi, I don't think this is what I need, altough might be helpful.

    Maybe I need to describe it differently what I want. I want on shutdown of the Application that hosts the Background Workers to issue a cancellation token that can somehow be used/injected as ICancellationTokenProvider, as we (currently) exclusively use the ICancellationTokenProvider to cancel work in the Repositories.

    How would AddBackgroundWorkerAsync with a CancellationToken parameter work here?

    Thanks

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    I want on shutdown of the Application that hosts the Background Workers to issue a cancellation token that can somehow be used/injected as ICancellationTokenProvider,

    Currently not possible.

    How would AddBackgroundWorkerAsync with a CancellationToken parameter work here?

    Something like this

    https://github.com/abpframework/abp/blob/0c21d295c5989da948fe208c74277d0cbb895aa2/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs#L36

  • User Avatar
    0
    MichelZ created

    I want on shutdown of the Application that hosts the Background Workers to issue a cancellation token that can somehow be used/injected as ICancellationTokenProvider,

    Currently not possible.

    OK, thank you

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09