Click or drag to resize

Cancel job and all its child tasks

Problem

You want to cancel a job.

Solution

There are two ways to cancel tasks on a job.

Method 1: Set the Job.CancelOnTaskFailure property to true to cancel a job's tasks automatically if one of the tasks fails. Use this option if the result of a job depends on all its child tasks completing successfully.

Java
package stkparallelcomputingserversdk.howto;

import agi.parallel.client.ClusterJobScheduler;
import agi.parallel.client.IJobScheduler;
import agi.parallel.client.Job;
import agi.parallel.infrastructure.Task;

public class CancelJobUsingCancelOnTaskFailure {
    public static void main(String[] args) {
        try (IJobScheduler scheduler = new ClusterJobScheduler("localhost")) {
            scheduler.connect();

            Job job = scheduler.createJob();
            job.setCancelOnTaskFailure(true);

            for (int i = 0; i < 3; ++i) {
                job.addTask(new WaitTask());
            }

            // This task will fail. Watch as the other tasks are canceled.
            job.addTask(new FailureTask());

            job.submit();
            job.waitUntilDone();

            for (int i = 0; i < 4; ++i) {
                Task task = job.getTasks().get(i);
                System.out.format("Task #%s Status %s%n", i + 1, task.getTaskStatus());
            }
        }

        /*
         * The output of the application should resemble:
         * Task #1 Status CANCELED
         * Task #2 Status CANCELED
         * Task #3 Status CANCELED
         * Task #4 Status FAILED
         */
    }

    private static class WaitTask extends Task {
        @Override
        public void execute() {
            for (int i = 0; i < 100; ++i) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static class FailureTask extends Task {
        @Override
        public void execute() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            throw new RuntimeException("Failure");
        }
    }
}

Method 2: The other option is to explicitly cancel the job using the cancel() method.

Tip Tip

By default, the client will not wait until the task is fully canceled. To wait for the job to complete, use the overload to cancel(boolean) and pass true to the listenForCancellationEvent parameter.

Java
package stkparallelcomputingserversdk.howto;

import agi.parallel.client.ClusterJobScheduler;
import agi.parallel.client.IJobScheduler;
import agi.parallel.client.Job;
import agi.parallel.infrastructure.Task;

public class CancelJobExplicitly {
    public static void main(String[] args) throws InterruptedException {
        try (IJobScheduler scheduler = new ClusterJobScheduler("localhost")) {
            scheduler.connect();

            Job job = scheduler.createJob();
            for (int i = 0; i < 100; ++i) {
                job.addTask(new WaitTask());
            }

            job.submit();

            // Wait 10 seconds so the tasks queue up.
            Thread.sleep(10_000);

            // Cancel all the tasks in this job.
            job.cancel();
            job.waitUntilDone();
        }
    }

    private static class WaitTask extends Task {
        @Override
        public void execute() {
            for (int i = 0; i < 100; ++i) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Discussion

Within a task, check the Task.IsCancelling flag. If IsCancelling is true, try to exit out of the task as soon as possible. Each task is given 30 seconds to gracefully finish before the process is terminated. This value can be configured by setting TaskProperties.GRACEFUL_CANCELLATION_TIME_OUT.

See Also

STK Parallel Computing Server 2.9 API for Java