简体   繁体   中英

Cancellation Token usage in Service fabric

I have created a Service Fabric Application. There is a long-running job in the RunAsync() method like "LoadData() which migrates million of records from Database to ServiceFabric dictionary".

As per the MSDN documentation, Services that want to implement a background task, which runs when the service comes up, should override this method with their logic "RunAsync(CancellationToken)"

Cancellation token is used to monitor for cancellation requests and it exists from RunAsync() and shut down the service. So I had used CancellationToken in my project.

Here is my code

RunAsync (System.Threading.CancellationToken cancellationToken);
{
 LoadData(CancellationToken);
}
Async Task LoadData(CancellationToken)
{
 Method1(CancellationToken) -- Aync call
 Method2() -- Normal call
 Method3() -- Normal call
}

As you can see, I have Method1, which is an asynchronous call and it runs as a separate thread so the same token is being passed to this method as the main thread will not aware of the child thread. However, Method2 and Method3 are just functional calls so the CancellationToken is not passed because there are running in the context of the main thread.

I have a couple of questions on CancellationToken usage in Service Fabric. 1. Do we need to pass the CancellationToken for all methods that are being called from the Long-running method( Async/ Sync)? 2. The way how I have handled CancellationToken is correct or Do I need to modify anything?

Do we need to pass the CancellationToken for all methods that are being called from the Long-running method( Async/ Sync)?

Yes, if you are able to respect it. It makes no sense to pass it to a method if it does not do anything with it.

The way how I have handled CancellationToken is correct or Do I need to modify anything?

Hard to tell, we need to know what your code does.

If the service is going to shut down it will use the CancellationToken to cancel any pending work. If you have any async calls that accept a CancellationToken you should pass it to that method so it can exit when cancellation is requested.

If you have some other methods like Method1() and it does some long running work you can still exit when cancellation is requested by either using CancellationToken.IsCancellationRequested or CancellationToken.ThrowIfCancellationRequested . The latter will throw an OperationCanceledException if cancellation is requested.

For example, you can do this:

public void Method1(CancellationToken cancellationToken)
{
    // some work

    if(cancellationToken.IsCancellationRequested)
        return;

    // some more work

    if (cancellationToken.IsCancellationRequested)
        return;

    // some more work
}

It is up to you to decide whether the method has some logical points to check for cancellation or that the method is so fast it won't make much difference.

By the way, this is not specific to service fabric, you can find more docs here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM