private WinRmToolResponse executePsScript(final WinRmMachineLocation machine, final String psScript) { TaskBuilder<WinRmToolResponse> tb = Tasks.<WinRmToolResponse>builder().displayName("winrm-exec").body( new Callable<WinRmToolResponse>() { @Override public WinRmToolResponse call() throws Exception { // FIXME Do I need COMPUTER_NAME in flags? return machine.executePsScript(psScript); } }); return execute(tb, psScript); }
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()); } }
@Override public Object call() throws Exception { try { Tasks.setBlockingDetails("Retrieving "+ds); return ds.get(); } finally { Tasks.resetBlockingDetails(); } } }; String description = getDescription();
public static Task<List<?>> sequential(String name, List<? extends TaskAdaptable<?>> tasks) { return sequential(name, asTasks(Iterables.toArray(tasks, TaskAdaptable.class))); } private static Task<List<?>> sequentialInternal(String name, Task<?>[] tasks) {
@SuppressWarnings("unchecked") @Override public void apply(final EntityLocal entity) { super.apply(entity); class ResolveValue implements Callable<Maybe<T>> { @Override public Maybe<T> call() throws Exception { return Tasks.resolving(value).as((Class<T>)sensor.getType()).timeout(timeout).getMaybe(); } } final Task<Maybe<T>> resolveValue = Tasks.<Maybe<T>>builder().displayName("resolving " + value).body(new ResolveValue()).build(); class SetValue implements Callable<T> { @Override public T call() throws Exception { Maybe<T> v = resolveValue.get(); if (!v.isPresent()) { log.debug(this+" not setting sensor "+sensor+" on "+entity+"; cannot resolve "+value+" after timeout " + timeout); return null; } log.debug(this+" setting sensor "+sensor+" to "+v.get()+" on "+entity); return entity.sensors().set(sensor, v.get()); } } Task<T> setValue = Tasks.<T>builder().displayName("Setting " + sensor + " on " + entity).body(new SetValue()).build(); Entities.submit(entity, Tasks.sequential("Resolving and setting " + sensor + " on " + entity, resolveValue, setValue)); } }
/** * Utility method that will execute the given function on the given nodes. The executions will be done in a parallel. * * @param nodes the nodes to execute the function on. * @param fn the function to execute. * @param <T> the type of node. * @return a new pool of tasks. */ // TODO: Pass the task's name and description as parameter protected <T> Task<List<?>> parallelListenerTask(final Iterable<T> nodes, final Function<T, ?> fn) { List<Task<?>> tasks = Lists.newArrayList(); for (final T node : nodes) { Task<?> t = Tasks.builder() .name(node.toString()) .description("Invocation on " + node.toString()) .body(new FunctionRunningCallable<T>(node, fn)) .tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG) .build(); tasks.add(t); } return Tasks.parallel("Parallel invocation of " + fn + " on ambari nodes", tasks); }
private Maybe<Entity> resolveTarget() { return Tasks.resolving(target, Entity.class) .context(entity()) .getMaybe(); }
stoppingProcess = Tasks.parallel("stopping", Tasks.create("stopping (process)", new StopProcessesAtMachineTask()), Tasks.create("stopping (feeds)", new StopFeedsAtMachineTask())); DynamicTasks.queue(stoppingProcess); stopMachineFlags.put("tags", MutableSet.of(BrooklynTaskTags.tagForEntitlement(Entitlements.getEntitlementContext()))); Task<StopMachineDetails<Integer>> stopMachineTask = Tasks.<StopMachineDetails<Integer>>builder() .displayName("stopping (machine)") .body(stopTask).flags(stopMachineFlags)
@Override protected synchronized void onRequesting() { if (!owningTasks.isEmpty() || !requestingTasks.isEmpty()) { Tasks.setBlockingTask( !requestingTasks.isEmpty() ? Iterables.getLast(requestingTasks) : Iterables.getFirst(owningTasks, null) ); Tasks.setBlockingDetails("Waiting on semaphore "+getName()+" ("+getDescription()+"); " + "queued at "+Time.makeDateString()+" when "+getRequestingThreads().size()+" ahead in queue"); } requestingTasks.addIfNotNull(Tasks.current()); super.onRequesting(); }
/** blocks until the given task completes, submitting if necessary, returning the result of that task; * optional contextMessage is available in status if this is running in a task */ @SuppressWarnings("unchecked") public static <T> T waitForTask(Task<T> t, Entity context, String contextMessage) throws InterruptedException { try { return (T) Tasks.resolveValue(t, Object.class, ((EntityInternal)context).getExecutionContext(), contextMessage); } catch (ExecutionException e) { throw Throwables.propagate(e); } }
@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; }}); }
@Override protected synchronized void onRequestFinished() { super.onRequestFinished(); requestingTasks.removeIfNotNull(Tasks.current()); Tasks.resetBlockingDetails(); Tasks.resetBlockingTask(); }
@Override public void run() { String oldDetails = Tasks.setBlockingDetails(description); LOG.debug("{} acquiring permit from {}", this, permit); try { permit.acquire(); hasObtainedPermit.set(true); } catch (InterruptedException e) { throw Exceptions.propagate(e); } finally { Tasks.setBlockingDetails(oldDetails); } } }
/** @return a {@link TaskBuilder} which tests whether the repeater terminates with success in its configured timeframe, * returning true or false depending on whether repeater succeed */ public static TaskBuilder<Boolean> testing(Repeater repeater) { return Tasks.<Boolean>builder().body(new WaitForRepeaterCallable(repeater, false)) .displayName("waiting for condition") .description("Testing whether " + getTimeoutString(repeater) + ": "+repeater.getDescription()); }
protected void copyResources(String destinationParentDir, Map<String, String> files, Map<String, String> templates) { if (files == null) files = Collections.emptyMap(); if (templates == null) templates = Collections.emptyMap(); final List<TaskAdaptable<?>> tasks = new ArrayList<>(files.size() + templates.size()); applyFnToResourcesAppendToList(files, newCopyResourceFunction(), destinationParentDir, tasks); applyFnToResourcesAppendToList(templates, newCopyTemplateFunction(), destinationParentDir, tasks); if (!tasks.isEmpty()) { String oldBlockingDetails = Tasks.setBlockingDetails("Copying resources"); try { DynamicTasks.queue(Tasks.sequential(tasks)).getUnchecked(); } finally { Tasks.setBlockingDetails(oldBlockingDetails); } } }
/** * @see #resolveDeepValue(Object, Class, ExecutionContext, String) */ public static Object resolveDeepValue(Object v, Class<?> type, ExecutionContext exec) throws ExecutionException, InterruptedException { return resolveDeepValue(v, type, exec, null); }
protected Object resolveValue(Object v, ExecutionContext exec) throws ExecutionException, InterruptedException { if (v instanceof Collection || v instanceof Map) { return Tasks.resolveDeepValue(v, Object.class, exec, "config "+name); } else { return Tasks.resolveValue(v, getType(), exec, "config "+name); } }
/** returns an unsubmitted task which will invoke the given effector on the given entities in parallel; * return type is Task<List<T>> (but haven't put in the blood sweat toil and tears to make the generics work) */ public static TaskAdaptable<List<?>> invocationParallel(Effector<?> eff, Map<?,?> params, Iterable<? extends Entity> entities) { List<TaskAdaptable<?>> tasks = new ArrayList<TaskAdaptable<?>>(); for (Entity e: entities) tasks.add(invocation(e, eff, params)); return Tasks.parallel( "invoking " + eff + " on " + tasks.size() + " node" + (Strings.s(tasks.size())), tasks.toArray(new TaskAdaptable[tasks.size()])); }
@Test public void testComplex() throws Exception { Task<List<?>> t = Tasks.sequential( sayTask("1"), sayTask("2"), Tasks.parallel(sayTask("4"), sayTask("3")), sayTask("5") ); ec.submit(t); Assert.assertEquals(t.get().size(), 4); Asserts.assertEqualsIgnoringOrder((List<?>)t.get().get(2), ImmutableSet.of("3", "4")); Assert.assertTrue(messages.equals(Arrays.asList("1", "2", "3", "4", "5")) || messages.equals(Arrays.asList("1", "2", "4", "3", "5")), "messages="+messages); }