protected void waitForTasksOnStart(Map<Entity, Task<?>> tasks) { // TODO Could do best-effort for waiting for remaining tasks, rather than failing on first? for (Map.Entry<Entity, Task<?>> entry: tasks.entrySet()) { try { entry.getValue().get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw Throwables.propagate(e); } catch (ExecutionException ee) { throw Throwables.propagate(ee.getCause()); } } }
/** blocks for the task to be completed * <p> * needed in any context where subsequent commands assume the task has completed. * not needed in a context where the task is simply being built up and queued. * <p> * throws if there are any errors */ public void andWaitForSuccess() { task.getUnchecked(); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public TaskSummary(Task task) { Preconditions.checkNotNull(task); // 'ported' from groovy web console TaskSummary.groovy , not sure if always works as intended Entity entity = (Entity) Iterables.tryFind(task.getTags(), Predicates.instanceOf(Entity.class)).orNull(); if (entity!=null) { this.entityId = entity.getId(); this.entityDisplayName = entity.getDisplayName(); } else { this.entityId = null; this.entityDisplayName = null; } this.tags = task.getTags(); this.displayName = task.getDisplayName(); this.description = task.getDescription(); this.id = task.getId(); this.rawSubmitTimeUtc = task.getSubmitTimeUtc(); this.submitTimeUtc = (task.getSubmitTimeUtc() == -1) ? "" : formatter.get().format(new Date(task.getSubmitTimeUtc())); this.startTimeUtc = (task.getStartTimeUtc() == -1) ? "" : formatter.get().format(new Date(task.getStartTimeUtc())); this.endTimeUtc = (task.getEndTimeUtc() == -1) ? "" : formatter.get().format(new Date(task.getEndTimeUtc())); this.currentStatus = task.getStatusSummary(); this.detailedStatus = task.getStatusDetail(true); }
@Override public int compare(Task<?> t1, Task<?> t2) { long end1 = t1.isDone() ? t1.getEndTimeUtc() : Long.MAX_VALUE; long end2 = t2.isDone() ? t2.getEndTimeUtc() : Long.MAX_VALUE; return (end1 < end2) ? -1 : ((end1 == end2) ? 0 : 1); } });
public boolean shouldDeleteTask(Task<?> task) { Set<Object> tags = task.getTags(); if (tags.contains(ManagementContextInternal.TRANSIENT_TASK_TAG)) return true; if (tags.contains(ManagementContextInternal.EFFECTOR_TAG) || tags.contains(ManagementContextInternal.NON_TRANSIENT_TASK_TAG)) return false; if (task.getSubmittedByTask()!=null && !shouldDeleteTask(task.getSubmittedByTask())) return false; // e.g. scheduled tasks, sensor events, etc // TODO (in future may keep these for a shorter duration, e.g. to investigate) return true; }
@SuppressWarnings("deprecation") protected void beforeStart(Map flags, Task<?> task) { activeTaskCount.incrementAndGet(); //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); if (!task.isCancelled()) { ((TaskInternal<?>)task).setThread(Thread.currentThread()); if (RENAME_THREADS) { String newThreadName = "brooklyn-" + CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, task.getDisplayName().replace(" ", "")) + "-" + task.getId().substring(0, 8); task.getThread().setName(newThreadName); } PerThreadCurrentTaskHolder.perThreadCurrentTask.set(task); ((TaskInternal<?>)task).setStartTimeUtc(System.currentTimeMillis()); } for (Object to : (Collection)flags.get("tagLinkedPreprocessors")) { TaskPreprocessor t = (TaskPreprocessor)to; t.onStart(flags, task); } ExecutionUtils.invoke(flags.get("newTaskStartCallback"), task); }
out.append(" = "); if (isSecret(it.getName())) out.append("xxxxxxxx"); else if ((v instanceof Task) && ((Task<?>)v).isDone()) { if (((Task<?>)v).isError()) { out.append("ERROR in "+v); } else { try { out.append(((Task<?>)v).get() + " (from "+v+")"); } catch (ExecutionException ee) { throw new IllegalStateException("task "+v+" done and !isError, but threw exception on get", ee);
public T call() throws Exception { if (!task.asTask().isSubmitted()) { BasicExecutionContext.getCurrentExecutionContext().submit(task); } return transformer.apply(task.asTask().get()); }}); }
try { if (RENAME_THREADS) { String newThreadName = oldThreadName+"-"+task.getDisplayName()+ "["+task.getId().substring(0, 8)+"]"; Thread.currentThread().setName(newThreadName); if (!task.isCancelled()) { result = (T) ((TaskInternal)task).getJob().call(); } else throw new CancellationException();
public static LinkWithMetadata asLink(Task<?> t) { if (t==null) return null; MutableMap<String,Object> data = new MutableMap<String,Object>(); data.put("id", t.getId()); if (t.getDisplayName()!=null) data.put("taskName", t.getDisplayName()); Entity entity = BrooklynTasks.getContextEntity(t); if (entity!=null) { data.put("entityId", entity.getId()); if (entity.getDisplayName()!=null) data.put("entityDisplayName", entity.getDisplayName()); } return new LinkWithMetadata("/v1/activities/"+t.getId(), data); }
if (task instanceof Task<?>) taskTags.addAll( ((Task<?>)task).getTags() ); Entity target = BrooklynTasks.getWrappedEntityOfType(taskTags, BrooklynTasks.TARGET_ENTITY); if (target!=null && !tags.contains(BrooklynTasks.tagForContextEntity(target))) { return submit(Tasks.<T>builder().name("Cross-context execution: "+t.getDescription()).dynamic(true).body(new Callable<T>() { public T call() { return DynamicTasks.get(t); }
/** returns the tag for the indicated stream, or null */ public static WrappedStream stream(Task<?> task, String streamType) { for (Object tag: task.getTags()) if ((tag instanceof WrappedStream) && ((WrappedStream)tag).streamType.equals(streamType)) return (WrappedStream)tag; return null; }
@Override public boolean equals(Object obj) { if (obj instanceof Task) return ((Task<?>)obj).getId().equals(getId()); return false; }
protected String getActiveTaskStatusString(int verbosity) { StringBuilder rv = new StringBuilder("Scheduler"); if (runCount>0) rv.append(", iteration "+(runCount+1)); if (recentRun!=null) rv.append(", last run "+(System.currentTimeMillis()-recentRun.getStartTimeUtc())+" ms ago"); if (truth(getNextScheduled())) { long untilNext = getNextScheduled().getDelay(TimeUnit.MILLISECONDS); if (untilNext>0) rv.append(", next in "+untilNext+" ms"); else rv.append(", next imminent"); } return rv.toString(); }
rv += "Children:\n"; for (Task<?> child: childrenTasks) { rv += " "+child+": "+child.getStatusDetail(false)+"\n";
/** returns the first tag found on the given task which matches the given type, looking up the submission hierarachy if necessary */ @SuppressWarnings("unchecked") public static <T> T tag(@Nullable Task<?> task, Class<T> type, boolean recurseHierarchy) { // support null task to make it easier for callers to walk hierarchies if (task==null) return null; for (Object tag: task.getTags()) if (type.isInstance(tag)) return (T)tag; if (!recurseHierarchy) return null; return tag(task.getSubmittedByTask(), type, true); }
if (numTasksToDelete > 0) { for (Task<?> taskToDelete : sortedTasks.subList(0, numTasksToDelete)) { if (!taskToDelete.isDone()) break; executionManager.deleteTask(taskToDelete); if (taskContender.isDone() && (System.currentTimeMillis() - taskContender.getEndTimeUtc() > maxTaskAge)) { executionManager.deleteTask(taskContender); } else {
/** returns the set of tags indicating the streams available on a task */ public static Set<WrappedStream> streams(Task<?> task) { Set<WrappedStream> result = new LinkedHashSet<BrooklynTasks.WrappedStream>(); for (Object tag: task.getTags()) { if (tag instanceof WrappedStream) { result.add((WrappedStream)tag); } } return ImmutableSet.copyOf(result); }