Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ public static Set<Task<?>> getTasksInEntityContext(ExecutionManager em, Entity e
return em.getTasksWithTag(tagForContextEntity(e));
}

public static Set<Task<?>> getTasksInAdjunctContext(ExecutionManager em, EntityAdjunct a) {
return em.getTasksWithTag(tagForContextAdjunct(a));
}

public static ManagementContext getManagementContext(Task<?> task) {
for (Object tag : getTagsFast(task))
if ((tag instanceof ManagementContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand All @@ -36,6 +37,7 @@
import org.apache.brooklyn.rest.domain.AdjunctSummary;
import org.apache.brooklyn.rest.domain.ConfigSummary;
import org.apache.brooklyn.rest.domain.Status;
import org.apache.brooklyn.rest.domain.TaskSummary;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
Expand Down Expand Up @@ -234,4 +236,21 @@ public Response setConfig(
@PathParam("config") String configKeyName,
@ApiParam(name = "value", value = "New value for the configuration", required = true)
Object value);


@GET
@Path("/{adjunct}/activities")
@ApiOperation(value = "Fetch list of tasks for this adjunct")
@ApiResponses(value = {
@ApiResponse(code = 404, message = "Could not find application, entity, or adjunct")
})
public List<TaskSummary> listTasks(
@ApiParam(value = "Application ID or name", required = true) @PathParam("application") String applicationId,
@ApiParam(value = "Entity ID or name", required = true) @PathParam("entity") String entityId,
@ApiParam(value = "Adjunct ID or name", required = true) @PathParam("adjunct") String adjunctToken,
@ApiParam(value = "Max number of tasks, or -1 for all (default 200)", required = false)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of -1 for all. But it's already used in EntityApi.listTasks so I'll live with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes for me it is simply "least bad"

@QueryParam("limit") @DefaultValue("200") int limit,
@ApiParam(value = "Whether to include subtasks recursively across different entities (default false)", required = false)
@QueryParam("recurse") @DefaultValue("false") Boolean recurse);

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigPredicates;
import org.apache.brooklyn.core.entity.EntityInternal;
import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
import org.apache.brooklyn.core.mgmt.entitlement.Entitlements;
import org.apache.brooklyn.rest.api.AdjunctApi;
import org.apache.brooklyn.rest.domain.AdjunctDetail;
import org.apache.brooklyn.rest.domain.AdjunctSummary;
import org.apache.brooklyn.rest.domain.ConfigSummary;
import org.apache.brooklyn.rest.domain.Status;
import org.apache.brooklyn.rest.domain.SummaryComparators;
import org.apache.brooklyn.rest.domain.TaskSummary;
import org.apache.brooklyn.rest.filter.HaHotStateRequired;
import org.apache.brooklyn.rest.transform.AdjunctTransformer;
import org.apache.brooklyn.rest.transform.ConfigTransformer;
import org.apache.brooklyn.rest.transform.EntityTransformer;
import org.apache.brooklyn.rest.transform.TaskTransformer;
import org.apache.brooklyn.rest.util.WebResourceUtils;
import org.apache.brooklyn.util.collections.MutableList;
import org.apache.brooklyn.util.core.ClassLoaderUtils;
import org.apache.brooklyn.util.core.flags.TypeCoercions;
import org.apache.brooklyn.util.exceptions.Exceptions;
Expand Down Expand Up @@ -270,4 +274,12 @@ public Response setConfig(String application, String entityToken, String adjunct
return Response.status(Response.Status.OK).build();
}

@Override
public List<TaskSummary> listTasks(String applicationId, String entityId, String adjunctToken, int limit, Boolean recurse) {
Entity entity = brooklyn().getEntity(applicationId, entityId);
EntityAdjunct adjunct = brooklyn().getAdjunct(entity, adjunctToken);
return TaskTransformer.fromTasks(MutableList.copyOf(BrooklynTaskTags.getTasksInAdjunctContext(mgmt().getExecutionManager(), adjunct)),
limit, recurse, entity, ui);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,11 @@ public Response addChildren(String applicationToken, String entityToken, Boolean

@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())));
return TaskTransformer.fromTasks(MutableList.copyOf(BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), entity)),
limit, recurse, entity, ui);
}

/** API does not guarantee order, but this is a the one we use (when there are lots of tasks):
* prefer top-level tasks and to recent tasks,
* balanced such that the following are equal:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

Expand All @@ -36,15 +37,23 @@
import org.apache.brooklyn.core.mgmt.BrooklynTaskTags.WrappedStream;
import org.apache.brooklyn.rest.domain.LinkWithMetadata;
import org.apache.brooklyn.rest.domain.TaskSummary;
import org.apache.brooklyn.rest.resources.EntityResource.InterestingTasksFirstComparator;
import org.apache.brooklyn.rest.util.WebResourceUtils;
import org.apache.brooklyn.util.collections.MutableList;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.core.task.TaskInternal;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.text.Strings;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;

import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.apache.brooklyn.rest.api.ActivityApi;
import org.apache.brooklyn.rest.api.EntityApi;
import static org.apache.brooklyn.rest.util.WebResourceUtils.serviceUriBuilder;
Expand Down Expand Up @@ -150,4 +159,28 @@ public static LinkWithMetadata asLink(Task<?> t, UriBuilder ub) {
URI taskUri = serviceUriBuilder(ub, ActivityApi.class, "get").build(t.getId());
return new LinkWithMetadata(taskUri.toString(), data);
}

public static List<TaskSummary> fromTasks(List<Task<?>> tasksToScan, int limit, Boolean recurse, Entity entity, UriInfo ui) {
int sizeRemaining = limit;
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())));
}
}