Hello Slava,

Unfortunately demo does not work for me: IComputeJob.Cancel is never called.
In my case an execution of job may take few minutes and I want to receive an 
notification about cancellation.

Please check the following code, SimpleJob will never finish, because 
IComputeJob.Cancel is never called:

            using (var ignite = Ignition.Start())
            using (var cts = new CancellationTokenSource())
            {
                var task = ignite.GetCompute().ExecuteAsync(new SimpleTask(), 
3, cts.Token);

                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);

                cts.Cancel();

                await task.ConfigureAwait(false);
            }

    public class SimpleTask : IComputeTask<int, int, int>
    {
        public IDictionary<IComputeJob<int>, IClusterNode> 
Map(IList<IClusterNode> subGrid, int jobsPerGridCount)
        {
            return Enumerable
                .Range(1, jobsPerGridCount)
                .SelectMany(i => subGrid)
                .ToDictionary(i => (IComputeJob<int>)new SimpleJob(), i => i);
        }

        public ComputeJobResultPolicy OnResult(IComputeJobResult<int> res, 
IList<IComputeJobResult<int>> rcvd)
        {
            return ComputeJobResultPolicy.Wait;
        }

        public int Reduce(IList<IComputeJobResult<int>> results)
        {
            return 1;
        }
    }

    public class SimpleJob : IComputeJob<int>
    {
        private bool _isCancelled;

        public int Execute()
        {
            Console.WriteLine("execute task");

            while (!Volatile.Read(ref _isCancelled))
            {
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            return 1;
        }

        public void Cancel()
        {
            // never happens !!!
            Console.WriteLine("cancel task");
            Volatile.Write(ref _isCancelled, true);
        }
    }

Thanks,
Max

-----Original Message-----
From: slava.koptilin [mailto:slava.kopti...@gmail.com] 
Sent: Donnerstag, 2. August 2018 17:56
To: user@ignite.apache.org
Subject: Re: Ignite.NET how to cancel tasks

Hello Maksym,

It seems that you need to call Cancel() method.
something like as follows:

var cts = new CancellationTokenSource(); var task = 
Compute.ExecuteJavaTaskAsync(ComputeApiTest.BroadcastTask, null, cts.Token); 
cts.Cancel();

Please take a look at this example:
https://github.com/apache/ignite/blob/master/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/CancellationTest.cs

Thanks,
Slava.



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Reply via email to