@Override public void customWriteBody(Task<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStringField("id", value.getId()); jgen.writeStringField("displayName", value.getDisplayName()); } @Override
@Override public boolean equals(Object obj) { if (obj instanceof Task) return ((Task<?>)obj).getId().equals(getId()); return false; }
/** As {@link #setHighlight(String, HighlightTuple)}, convenience for recording an item with the current time and given task. */ protected void highlight(String name, String description, @Nullable Task<?> t) { setHighlight(name, new HighlightTuple(description, System.currentTimeMillis(), t!=null ? t.getId() : null)); }
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; }
/** 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); } }
/** 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); }
/** normally (if not interrupted) called once for each call to {@link #internalBeforeSubmit(Map, Task)}, * and, for atomic tasks and scheduled-task submission iterations where * always called once if {@link #internalBeforeStart(Map, Task)} is invoked and in the same thread as that method */ protected void internalAfterEnd(Map<?,?> flags, Task<?> task, boolean startedInThisThread, boolean isEndingAllIterations) { if (log.isTraceEnabled()) log.trace(this+" afterEnd, task: "+task); if (startedInThisThread) { activeTaskCount.decrementAndGet(); } if (isEndingAllIterations) { incompleteTaskIds.remove(task.getId()); invokeCallback(flags.get("newTaskEndCallback"), task); ((TaskInternal<?>)task).setEndTimeUtc(System.currentTimeMillis()); } if (startedInThisThread) { PerThreadCurrentTaskHolder.perThreadCurrentTask.remove(); //clear thread _after_ endTime set, so we won't get a null thread when there is no end-time if (RENAME_THREADS && startedInThisThread) { Thread thread = task.getThread(); if (thread==null) { log.warn("BasicTask.afterEnd invoked without corresponding beforeStart"); } else { thread.setName(threadOriginalName.get()); threadOriginalName.remove(); } } ((TaskInternal<?>)task).setThread(null); } synchronized (task) { task.notifyAll(); } }
@Override protected String getDefaultUniqueTag() { List<String> summary = MutableList.of(); if (propagating!=null) { for (Map.Entry<? extends Sensor<?>, ? extends Sensor<?>> entry: propagating.entrySet()) { if (entry.getKey().getName().equals(entry.getValue().getName())) summary.add(entry.getKey().getName()); else summary.add(entry.getKey().getName()+"->"+entry.getValue().getName()); } } if (Boolean.TRUE.equals(propagatingAll)) summary.add("ALL"); if (propagatingAllBut!=null && !Iterables.isEmpty(propagatingAllBut)) { List<String> allBut = MutableList.of(); for (Sensor<?> s: propagatingAllBut) allBut.add(s.getName()); summary.add("ALL_BUT:"+com.google.common.base.Joiner.on(",").join(allBut)); } // TODO What to use as the entity id if using fromEntitySupplier? String fromId = (fromEntity != null) ? fromEntity.getId() : fromEntitySupplier.getId(); return "propagating["+fromId+":"+com.google.common.base.Joiner.on(",").join(summary)+"]"; } @Override
private LinkedHashMap<String, TaskSummary> getAllDescendantTasks(final Task<?> parentTask, int limit, int maxDepth) { final LinkedHashMap<String, TaskSummary> result = Maps.newLinkedHashMap(); if (!(parentTask instanceof HasTaskChildren)) { return result; } Set<Task<?>> nextLayer = MutableSet.copyOf( ((HasTaskChildren) parentTask).getChildren() ); outer: while (limit!=0 && !nextLayer.isEmpty() && maxDepth-- != 0) { Set<Task<?>> thisLayer = nextLayer; nextLayer = MutableSet.of(); for (final Task<?> childTask : thisLayer) { TaskSummary wasThere = result.put(childTask.getId(), TaskTransformer.fromTask(ui.getBaseUriBuilder()).apply(childTask)); if (wasThere==null) { if (--limit == 0) { break outer; } if (childTask instanceof HasTaskChildren) { Iterables.addAll(nextLayer, ((HasTaskChildren)childTask).getChildren()); } } } } return result; }
@Override public List<TaskSummary> listTasks(String applicationId, String entityId, int limit, Boolean recurse) { int sizeRemaining = limit; Entity entity = brooklyn().getEntity(applicationId, entityId); List<Task<?>> tasksToScan = MutableList.copyOf(BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), entity)); if (limit>0) { tasksToScan = MutableList.copyOf(Ordering.from(new InterestingTasksFirstComparator(entity)).leastOf(tasksToScan, limit)); } Map<String,Task<?>> tasksLoaded = MutableMap.of(); while (!tasksToScan.isEmpty()) { Task<?> t = tasksToScan.remove(0); if (tasksLoaded.put(t.getId(), t)==null) { if (--sizeRemaining==0) { break; } if (Boolean.TRUE.equals(recurse)) { if (t instanceof HasTaskChildren) { Iterables.addAll(tasksToScan, ((HasTaskChildren) t).getChildren() ); } } } } return new LinkedList<TaskSummary>(Collections2.transform(tasksLoaded.values(), TaskTransformer.fromTask(ui.getBaseUriBuilder()))); }
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(); }
public static LinkWithMetadata asLink(Task<?> t, UriBuilder ub) { 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 = BrooklynTaskTags.getContextEntity(t); if (entity!=null) { data.put("entityId", entity.getId()); if (entity.getDisplayName()!=null) data.put("entityDisplayName", entity.getDisplayName()); } URI taskUri = serviceUriBuilder(ub, ActivityApi.class, "get").build(t.getId()); return new LinkWithMetadata(taskUri.toString(), data); } }
protected int expireSubTasksWhoseSubmitterIsExpired() { // ideally we wouldn't have this; see comments on CHECK_SUBTASK_SUBMITTERS if (!brooklynProperties.getConfig(CHECK_SUBTASK_SUBMITTERS)) return 0; Collection<Task<?>> allTasks = executionManager.allTasksLive(); Collection<Task<?>> tasksToDelete = MutableList.of(); try { for (Task<?> task: allTasks) { if (!task.isDone()) continue; Task<?> submitter = task.getSubmittedByTask(); // if we've leaked, ie a subtask which is not a child task, // and the submitter is GC'd, then delete this also if (submitter!=null && submitter.isDone() && executionManager.getTask(submitter.getId())==null) { tasksToDelete.add(task); } } } catch (ConcurrentModificationException e) { // delete what we've found so far LOG.debug("Got CME inspecting aged tasks, with "+tasksToDelete.size()+" found for deletion: "+e); } for (Task<?> task: tasksToDelete) { executionManager.deleteTask(task); } return tasksToDelete.size(); }
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() }); return (Task<T>) submitNewScheduledTask(flags, (ScheduledTask)task); tasksById.put(task.getId(), task); totalTaskCount.incrementAndGet();
if (executionManager.getTask(parent.getId())==null) {
@Test public void testGetTaskById() throws Exception { Task<?> t = new BasicTask<Void>(newNoop()); em.submit(MutableMap.of("tag", "A"), t); assertEquals(em.getTask(t.getId()), t); }
public <T> Task<T> submitting(final Task<T> task) { return Tasks.<T>builder().displayName("submitting:"+task.getId()).body(new Callable<T>() { @Override public T call() throws Exception { ec.submit(task); return task.get(); } }).build(); }
@Test(groups = "Integration") public void testGetStream() throws Exception { String pathPrefix = "/v1/activities/"+subTask.getId()+"/stream/"; for (Map.Entry<String, String> entry : streams.entrySet()) { String streamId = entry.getKey(); String expectedStream = entry.getValue(); String path = pathPrefix+streamId; assert401(path); assertEquals(httpGet("myRoot", path), expectedStream); assertEquals(httpGet("myUser", path), expectedStream); assertEquals(httpGet("myReadonly", path), expectedStream); assertForbidden("myMinimal", path); assertForbidden("unrecognisedUser", path); StaticDelegatingEntitlementManager.setDelegate(new SeeSelectiveStreams(streamId)); assertEquals(httpGet("myCustom", path), expectedStream); StaticDelegatingEntitlementManager.setDelegate(new SeeSelectiveStreams("differentStreamId")); assertForbidden("myCustom", path); } }
@Test(groups = "Integration") public void testGetTask() throws Exception { String path = "/v1/activities/"+subTask.getId(); assert401(path); assertPermitted("myRoot", path); assertPermitted("myUser", path); assertPermitted("myReadonly", path); assertForbidden("myMinimal", path); assertForbidden("unrecognisedUser", path); }