public void onUnmanaged(Entity entity) { // defer task deletions until the entity is completely unmanaged // (this is usually invoked during the stop sequence) synchronized (unmanagedEntitiesNeedingGc) { unmanagedEntitiesNeedingGc.put(entity, Tasks.current()); } }
public static void resetBlockingDetails() { Task<?> current = current(); if (current instanceof TaskInternal) ((TaskInternal<?>)current).resetBlockingDetails(); } public static Task<?> setBlockingTask(Task<?> blocker) {
@Nullable /** callers should allow this to be null so task can be used outside of an entity */ protected Entity entity() { return BrooklynTaskTags.getTargetOrContextEntity(Tasks.current()); } protected <T> T entityConfig(ConfigKey<T> key) {
/** sets extra status details on the current task, if possible (otherwise does nothing). * the extra status is presented in Task.getStatusDetails(true) */ public static void setExtraStatusDetails(String notes) { Task<?> current = current(); if (current instanceof TaskInternal) ((TaskInternal<?>)current).setExtraStatusText(notes); }
public static TaskQueueingContext getTaskQueuingContext() { TaskQueueingContext adder = getThreadTaskQueuingContext(); if (adder!=null) return adder; Task<?> t = Tasks.current(); if (t instanceof TaskQueueingContext) return (TaskQueueingContext) t; return null; }
public static Task<?> setBlockingTask(Task<?> blocker) { Task<?> current = current(); if (current instanceof TaskInternal) return ((TaskInternal<?>)current).setBlockingTask(blocker); return null; } public static void resetBlockingTask() {
/** convenience for setting "blocking details" on any task where the current thread is running; * typically invoked prior to a wait, for transparency to a user; * then invoked with 'null' just after the wait */ public static String setBlockingDetails(String description) { Task<?> current = current(); if (current instanceof TaskInternal) return ((TaskInternal<?>)current).setBlockingDetails(description); return null; } public static void resetBlockingDetails() {
/** as {@link TaskTags#addTagDynamically(TaskAdaptable, Object)} but for current task, skipping if no current task */ public static void addTagDynamically(Object tag) { Task<?> t = Tasks.current(); if (t!=null) TaskTags.addTagDynamically(t, tag); }
protected RuntimeException logWithDetailsAndThrow(String message, Throwable optionalCause) { LOG.warn(message + " (throwing)"); int maxLength = 1024; LOG.warn(message + " (throwing)"); Streams.logStreamTail(LOG, "STDERR of problem in "+Tasks.current(), stderr, maxLength); Streams.logStreamTail(LOG, "STDOUT of problem in "+Tasks.current(), stdout, maxLength); Streams.logStreamTail(LOG, "STDIN of problem in "+Tasks.current(), Streams.byteArrayOfString(command != null ? command : psCommand), 4096); if (optionalCause!=null) throw new IllegalStateException(message, optionalCause); throw new IllegalStateException(message); }
@Override protected synchronized void onReleased(int permits) { super.onReleased(permits); for (int i=0; i<permits; i++) owningTasks.removeIfNotNull(Tasks.current()); }
@Override public String getCatalogItemIdFromContext() { Task<?> currentTask = Tasks.current(); if (currentTask != null) { Entity contextEntity = BrooklynTaskTags.getContextEntity(currentTask); if (contextEntity != null) { return contextEntity.getCatalogItemId(); } } return null; }
/** invoked when a task is submitted */ protected void internalBeforeSubmit(Map<?,?> flags, Task<?> task) { incompleteTaskIds.add(task.getId()); Task<?> currentTask = Tasks.current(); if (currentTask!=null) ((TaskInternal<?>)task).setSubmittedByTask(currentTask); ((TaskInternal<?>)task).setSubmitTimeUtc(System.currentTimeMillis()); if (flags.get("tag")!=null) ((TaskInternal<?>)task).getMutableTags().add(flags.remove("tag")); if (flags.get("tags")!=null) ((TaskInternal<?>)task).getMutableTags().addAll((Collection<?>)flags.remove("tags")); for (Object tag: ((TaskInternal<?>)task).getTags()) { tasksWithTagCreating(tag).add(task); } }
protected ExecutionContext getExecutionContext() { // TODO Transformer is cached so on next getConfig call the context could be different - something to watch out for. Entity contextEntity = BrooklynTaskTags.getTargetOrContextEntity(Tasks.current()); if (contextEntity instanceof EntityInternal) { return ((EntityInternal)contextEntity).getExecutionContext(); } else { log.debug("No entity context found, will use global execution context. Could lead to NPE on DSL resolving in location config."); return mgmt.getServerExecutionContext(); } } };
/** Finds a unique {@link MachineLocation} attached to the supplied entity * @throws IllegalStateException if there is not a unique such {@link SshMachineLocation} */ public static <T extends MachineLocation> T getMachine(Entity entity, Class<T> clazz) { try { return Machines.findUniqueMachineLocation(entity.getLocations(), clazz).get(); } catch (Exception e) { throw new IllegalStateException("Entity "+entity+" (in "+Tasks.current()+") requires a single " + clazz.getName() + ", but has "+entity.getLocations(), e); } }
public static CallInfo newInstance() { Exception e = new Exception("for stacktrace"); e.fillInStackTrace(); return new CallInfo(e.getStackTrace(), Tasks.current()); }
@Override public synchronized ProcessTaskWrapper<RET> newTask() { Entity entity = BrooklynTaskTags.getTargetOrContextEntity(Tasks.current()); if (machine==null) { if (log.isDebugEnabled()) log.debug("Using an ssh task not in an effector without any machine; will attempt to infer the machine: "+this); if (entity!=null) machine(EffectorTasks.getSshMachine(entity)); } applySshFlags(getConfig(), entity, getMachine()); return super.newTask(); }
@Override public String call() { log.info("main job - "+Tasks.current()); messages.add("main"); DynamicTasks.queue( sayTask("world") ); return "bye"; } };
@Override protected synchronized void onRequestFinished() { super.onRequestFinished(); requestingTasks.removeIfNotNull(Tasks.current()); Tasks.resetBlockingDetails(); Tasks.resetBlockingTask(); }
@Override public AttributeSensor apply(final String input) { Entity entity = BrooklynTaskTags.getContextEntity(Tasks.current()); if (entity!=null) { Sensor<?> result = entity.getEntityType().getSensor(input); if (result != null) return (AttributeSensor) result; } return Sensors.newSensor(Object.class, input); } });
@Override protected void postStop() { super.postStop(); if (isMachineStopped()) { // Don't unmanage in entity's task context as it will self-cancel the task. Wait for the stop effector to complete (and all parent entity tasks). // If this is not enough (still getting Caused by: java.util.concurrent.CancellationException: null) then // we could wait for BrooklynTaskTags.getTasksInEntityContext(ExecutionManager, this).isEmpty(); Task<?> stopEffectorTask = BrooklynTaskTags.getClosestEffectorTask(Tasks.current(), Startable.STOP); Task<?> topEntityTask = getTopEntityTask(stopEffectorTask); getManagementContext().getExecutionManager().submit(new UnmanageTask(topEntityTask, this)); } }