/** returns true if either the current thread or the current task is interrupted/cancelled */ public static boolean isInterrupted() { if (Thread.currentThread().isInterrupted()) return true; Task<?> t = current(); if (t==null) return false; return t.isCancelled(); }
/** returns the error thrown by the task if {@link Task#isError()}, or null if no error or not done */ public static Throwable getError(Task<?> t) { if (t==null) return null; if (!t.isDone()) return null; if (t.isCancelled()) return new CancellationException(); try { t.get(); return null; } catch (Throwable error) { // do not propagate as we are pretty much guaranteed above that it wasn't this // thread which originally threw the error return error; } } public static Task<Void> fail(final String name, final Throwable optionalError) {
public static boolean isAncestorCancelled(Task<?> t) { if (t==null) return false; if (t.isCancelled()) return true; return isAncestorCancelled(t.getSubmittedByTask()); }
/** invoked in a task's thread when a task is starting to run (may be some time after submitted), * but before doing any of the task's work, so that we can update bookkeeping and notify callbacks */ protected void internalBeforeStart(Map<?,?> flags, Task<?> task) { int count = activeTaskCount.incrementAndGet(); if (count % 1000==0) { log.warn("High number of active tasks: task #"+count+" is "+task); } //set thread _before_ start time, so we won't get a null thread when there is a start-time if (log.isTraceEnabled()) log.trace(""+this+" beforeStart, task: "+task + " running on thread " + Thread.currentThread().getName()); if (!task.isCancelled()) { Thread thread = Thread.currentThread(); ((TaskInternal<?>)task).setThread(thread); if (RENAME_THREADS) { threadOriginalName.set(thread.getName()); String newThreadName = "brooklyn-" + CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, task.getDisplayName().replace(" ", "")) + "-" + task.getId().substring(0, 8); thread.setName(newThreadName); } PerThreadCurrentTaskHolder.perThreadCurrentTask.set(task); ((TaskInternal<?>)task).setStartTimeUtc(System.currentTimeMillis()); } jitterThreadStart(task); invokeCallback(flags.get("newTaskStartCallback"), task); }
private void assertAllOurConfigTasksNotCancelled() { for (Task<?> t: tasksMadeByFactory) { Assert.assertFalse( t.isCancelled(), "Task should not have been cancelled: "+t+" - "+t.getStatusDetail(false) ); } }
private void assertAllOurConfigTasksCancelled() { // TODO added Feb 2017 - but might need an "eventually" here, if cancel is happening in a BG thread // (but I think it is always foreground) for (Task<?> t: tasksMadeByFactory) { Assert.assertTrue( t.isCancelled(), "Task should have been cancelled: "+t+" - "+t.getStatusDetail(false) ); } }
try { beforeStartAtomicTask(flags, task); if (!task.isCancelled()) { result = ((TaskInternal<T>)task).getJob().call(); } else throw new CancellationException();
log.trace("CLFFT cancelling "+task+" mode "+mode); if (!task.isCancelled()) result |= ((TaskInternal<T>)task).cancel(mode); result |= delegate().cancel(mode.isAllowedToInterruptTask());
public void run() { Asserts.assertEquals(outerTasks.size(), NUM_CALLS); for (Task<?> task : outerTasks) { Asserts.assertTrue(task.isDone()); Asserts.assertTrue(task.isCancelled()); } } });
Assert.assertTrue(task2.isBegun()); Assert.assertTrue(task2.isDone()); Assert.assertTrue(task2.isCancelled()); Assert.assertTrue(task3.isCancelled()); Assert.assertFalse(task3.isBegun());
} else { if (tt==null || !tt.isAlive()) { if (!t.isCancelled()) {
Assert.assertTrue(t.isCancelled()); try { t.get();
task.getStatusSummary(), result, task.isError(), task.isCancelled(), children, asLink(task.getSubmittedByTask(), ub), task.isDone() ? null : task instanceof TaskInternal ? asLink(((TaskInternal<?>)task).getBlockingTask(), ub) : null,
Assert.assertEquals(t.isCancelled(), true); Assert.assertTrue(t1.isCancelled()); Asserts.eventually(new Supplier<Number>() { @Override public Number get() { return t1.getEndTimeUtc(); }}, } else { Time.sleep(TINY_TIME); Assert.assertFalse(t1.isCancelled()); Assert.assertFalse(t1.isDone());