public static ManagementContext getManagementContext(Task<?> task) { for (Object tag : task.getTags()) if ((tag instanceof ManagementContext)) return (ManagementContext) tag; return null; }
@Override public boolean apply(Task<?> input) { return (input != null) && input.getTags().contains(tag); } @Override
/** returns the tag for the indicated stream, or null */ public static WrappedStream stream(Task<?> task, String streamType) { if (task==null) return null; for (Object tag: task.getTags()) if ((tag instanceof WrappedStream) && ((WrappedStream)tag).streamType.equals(streamType)) return (WrappedStream)tag; return null; }
public static boolean hasTag(Task<?> task, Object tag) { if (task==null) return false; return task.getTags().contains(tag); }
/** returns the set of tags indicating the streams available on a task */ public static Set<WrappedStream> streams(Task<?> task) { Set<WrappedStream> result = new LinkedHashSet<BrooklynTaskTags.WrappedStream>(); for (Object tag: task.getTags()) { if (tag instanceof WrappedStream) { result.add((WrappedStream)tag); } } return ImmutableSet.copyOf(result); }
public static WrappedEntity getWrappedEntityTagOfType(Task<?> t, String wrappingType) { if (t==null) return null; return getWrappedEntityTagOfType(t.getTags(), wrappingType); } public static WrappedEntity getWrappedEntityTagOfType(Collection<?> tags, String wrappingType) {
/** finds the first {@link EffectorCallTag} tag on this tag, or optionally on submitters, or null */ public static EffectorCallTag getEffectorCallTag(Task<?> task, boolean recurse) { Task<?> t = task; while (t!=null) { for (Object tag: t.getTags()) { if (tag instanceof EffectorCallTag) return (EffectorCallTag)tag; } if (!recurse) return null; t = t.getSubmittedByTask(); } return null; }
public static EntitlementContext getEntitlement(Task<?> task) { if (task==null) return null; return getEntitlement(task.getTags()); }
/** 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); }
protected boolean deleteTaskNonRecursive(Task<?> task) { Set<?> tags = checkNotNull(task, "task").getTags(); for (Object tag : tags) { synchronized (tasksByTag) { Set<Task<?>> tasks = tasksWithTagLiveOrNull(tag); if (tasks != null) { tasks.remove(task); if (tasks.isEmpty()) { tasksByTag.remove(tag); } } } } Task<?> removed = tasksById.remove(task.getId()); incompleteTaskIds.remove(task.getId()); if (removed!=null && removed.isSubmitted() && !removed.isDone()) { log.warn("Deleting submitted task before completion: "+removed+"; this task will continue to run in the background outwith "+this+", but perhaps it should have been cancelled?"); } return removed != null; }
/** * finds the task up the {@code child} hierarchy handling the {@code effector} call, * returns null if one doesn't exist. */ @Beta public static Task<?> getClosestEffectorTask(Task<?> child, Effector<?> effector) { Task<?> t = child; while (t != null) { Set<Object> tags = t.getTags(); if (tags.contains(EFFECTOR_TAG)) { for (Object tag: tags) { if (tag instanceof EffectorCallTag) { EffectorCallTag et = (EffectorCallTag) tag; if (effector != null && !et.getEffectorName().equals(effector.getName())) continue; return t; } } } t = t.getSubmittedByTask(); } return null; }
@Override public void run() { Collection<Entity> context = new ArrayList<>(); for (Object tag : Tasks.current().getTags()) { if (tag instanceof WrappedEntity) { WrappedEntity wrapped = (WrappedEntity)tag; if (BrooklynTaskTags.CONTEXT_ENTITY.equals(wrapped.wrappingType)) { context.add(wrapped.entity); } } } assertEquals(context, expectedContext, "Found " + context + ", expected " + expectedContext); }
@Test public void testInvokeEffectorTaskHasTag() { Task<Void> starting = app.invoke(Startable.START, MutableMap.of("locations", locs)); // log.info("TAGS: "+starting.getTags()); Assert.assertTrue(starting.getTags().contains(ManagementContextInternal.EFFECTOR_TAG)); }
/** * checks if the given task is part of the given effector call on the given entity; * @param task the task to check (false if null) * @param entity the entity where this effector task should be running, or any entity if null * @param effector the effector (matching name) where this task should be running, or any effector if null * @param allowNestedEffectorCalls whether to match ancestor effector calls, e.g. if eff1 calls eff2, * and we are checking eff2, whether to match eff1 * @return whether the given task is part of the given effector */ public static boolean isInEffectorTask(Task<?> task, @Nullable Entity entity, @Nullable Effector<?> effector, boolean allowNestedEffectorCalls) { Task<?> t = task; while (t!=null) { Set<Object> tags = t.getTags(); if (tags.contains(EFFECTOR_TAG)) { for (Object tag: tags) { if (tag instanceof EffectorCallTag) { EffectorCallTag et = (EffectorCallTag)tag; if (entity!=null && !et.getEntityId().equals(entity.getId())) continue; if (effector!=null && !et.getEffectorName().equals(effector.getName())) continue; return true; } } if (!allowNestedEffectorCalls) return false; } t = t.getSubmittedByTask(); } return false; }
@Override public Void call() throws Exception { log.info("beginning spawned child response "+Tasks.current()+", with tags "+Tasks.current().getTags()); Tasks.setBlockingDetails("spawned child blocking details"); nowWaitingLatch.countDown(); if (!continueFromWaitingLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) { fail("took too long to be told to continue"); } return null; }}); }
private String taskToVerboseString(Task<?> t) { return MoreObjects.toStringHelper(t) .add("id", t.getId()) .add("displayName", t.getDisplayName()) .add("submitTime", t.getSubmitTimeUtc()) .add("startTime", t.getStartTimeUtc()) .add("endTime", t.getEndTimeUtc()) .add("status", t.getStatusSummary()) .add("tags", t.getTags()) .toString(); }
if (log.isTraceEnabled()) { log.trace("Submitting task {} ({}), with flags {}, and tags {}, job {}; caller {}", new Object[] {task.getId(), task, Sanitizer.sanitize(flags), task.getTags(), (task instanceof TaskInternal ? ((TaskInternal<T>)task).getJob() : "<unavailable>"), Tasks.current() }); for (Object tago: task.getTags()) { TaskScheduler scheduler = getTaskSchedulerForTag(tago); if (scheduler!=null) {
if (!task.isDone()) return false; Set<Object> tags = task.getTags(); if (tags.contains(ManagementContextInternal.TRANSIENT_TASK_TAG)) return true;
private void checkTags(Task<Integer> t, Entity entity, Effector<?> eff, boolean shouldHaveChild) { Assert.assertEquals(BrooklynTaskTags.getContextEntity(t), app); Assert.assertTrue(t.getTags().contains(BrooklynTaskTags.EFFECTOR_TAG), "missing effector tag; had: "+t.getTags()); Assert.assertTrue(t.getDescription().contains(eff.getName()), "description missing effector name: "+t.getDescription()); Assert.assertTrue(BrooklynTaskTags.isInEffectorTask(t, entity, eff, false)); Assert.assertTrue(BrooklynTaskTags.isInEffectorTask(t, null, null, false)); Assert.assertFalse(BrooklynTaskTags.isInEffectorTask(t, entity, Startable.START, false)); if (shouldHaveChild) { Task<?> subtask = ((HasTaskChildren)t).getChildren().iterator().next(); Assert.assertTrue(BrooklynTaskTags.isInEffectorTask(subtask, entity, eff, false)); Assert.assertTrue(BrooklynTaskTags.isInEffectorTask(subtask, null, null, false)); } }
public void testUnmanagedEntityCanBeGcedEvenIfPreviouslyTagged() throws Exception { TestEntity e = app.createAndManageChild(EntitySpec.create(TestEntity.class)); String eId = e.getId(); e.invoke(TestEntity.MY_EFFECTOR, ImmutableMap.<String,Object>of()).get(); Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(app.getManagementContext().getExecutionManager(), e); Task<?> task = Iterables.get(tasks, 0); assertTrue(task.getTags().contains(BrooklynTaskTags.tagForContextEntity(e))); Set<Object> tags = app.getManagementContext().getExecutionManager().getTaskTags(); assertTrue(tags.contains(BrooklynTaskTags.tagForContextEntity(e)), "tags="+tags); Entities.destroy(e); forceGc(); Set<Object> tags2 = app.getManagementContext().getExecutionManager().getTaskTags(); for (Object tag : tags2) { if (tag instanceof Entity && ((Entity)tag).getId().equals(eId)) { fail("tags contains unmanaged entity "+tag); } if ((tag instanceof WrappedEntity) && ((WrappedEntity)tag).entity.getId().equals(eId) && ((WrappedEntity)tag).wrappingType.equals(BrooklynTaskTags.CONTEXT_ENTITY)) { fail("tags contains unmanaged entity (wrapped) "+tag); } } return; }