/** causes the task to be submitted *synchronously* if it hasn't already been submitted; * useful in contexts such as libraries where callers may be either on a legacy call path * (which assumes all commands complete immediately); * requiring an entity execution context (will try to find a default if not set) */ public TaskQueueingResult<T> orSubmitAndBlock() { if (orSubmitInternal()) task.getUnchecked(); return this; } /** convenience for setting {@link #executionContext(ExecutionContext)} then submitting blocking */
/** Common implementation for restart in parent nodes; just invokes restart on all children of the entity */ public static void restart(Entity e) { log.debug("Restarting entity "+e); DynamicTasks.queueIfPossible(restartingChildren(e)).orSubmitAsync(e).getTask().getUnchecked(); if (log.isDebugEnabled()) log.debug("Restarted entity "+e); }
/** Common implementation for stop in parent nodes; just invokes stop on all children of the entity */ public static void stop(Entity e) { log.debug("Stopping entity "+e); DynamicTasks.queueIfPossible(stoppingChildren(e)).orSubmitAsync(e).getTask().getUnchecked(); if (log.isDebugEnabled()) log.debug("Stopped entity "+e); }
/** Returns the result of the last task queued in this context, coerced to the given type */ protected <V> V last(Class<V> type) { Task<?> last = waitForLast(); if (last==null) throw new IllegalStateException("No last task available (in "+DynamicTasks.getTaskQueuingContext()+")"); if (!Tasks.isQueuedOrSubmitted(last)) throw new IllegalStateException("Last task "+last+" has not been queued or submitted; will not block on its result"); return TypeCoercions.coerce(last.getUnchecked(), type); } }
@Override public T call(ConfigBag parameters) { TaskAdaptable<T> t = DynamicTasks.queue(effectorTaskFactory.newTask(entity, effector, parameters)); return t.asTask().getUnchecked(); } }).newTask(entity, effector, parameters);
public int execute() { if (DynamicTasks.getTaskQueuingContext()!=null) { return queue().getUnchecked(); } else { return executeInternal(); } }
@Override public Integer call() { Preconditions.checkState(x.isSubmitted()); return x.getUnchecked()+y; } }).build();
private Object invokeEffectorNamed(Entity target, String effectorName, ConfigBag params) { LOG.debug("{} forwarding effector invocation on {} to entity={}, effector={}, parameters={}", new Object[]{this, entity(), target, effectorName, params}); Effector<?> effector = EffectorUtils.findEffectorDeclared(target, effectorName).get(); return target.invoke(effector, params.getAllConfig()).getUnchecked(); } }
protected void stopAndRemoveNode(Entity member) { removeMember(member); try { if (member instanceof Startable) { Task<?> task = newThrottledEffectorTask(member, Startable.STOP, Collections.<String, Object>emptyMap()); DynamicTasks.queueIfPossible(task).orSubmitAsync(); task.getUnchecked(); } } finally { Entities.unmanage(member); } }
@Test(groups="Integration") public void testRequirePidFromFileOnSuccess() throws IOException { File f = File.createTempFile("testBrooklynPid", ".pid"); Files.write( (""+getMyPid()).getBytes(), f ); ProcessTaskWrapper<?> t = submit(SshEffectorTasks.requirePidFromFileRunning(f.getPath())); t.getTask().getUnchecked(); }
private Object invokeEffectorNamed(String effectorName, ConfigBag params) { LOG.info("{} invoking effector on {}, effector={}, parameters={}", new Object[]{this, entity(), effectorName, params}); Maybe<Effector<?>> effector = entity().getEntityType().getEffectorByName(effectorName); if (effector.isAbsent()) { throw new IllegalStateException("Cannot find effector " + effectorName); } return entity().invoke(effector.get(), params.getAllConfig()).getUnchecked(); }
@Test(groups="Integration") public void testSshEchoHello() { ProcessTaskWrapper<Integer> t = submit(SshEffectorTasks.ssh("sleep 1 ; echo hello world")); Assert.assertFalse(t.isDone()); Assert.assertEquals(t.get(), (Integer)0); Assert.assertEquals(t.getTask().getUnchecked(), (Integer)0); Assert.assertEquals(t.getStdout().trim(), "hello world"); }
@Test(groups="Integration") public void testSshEchoHello() { ProcessTaskWrapper<Integer> t = submit(SshTasks.newSshExecTaskFactory(host, "sleep 1 ; echo hello world")); Assert.assertFalse(t.isDone()); Assert.assertEquals(t.get(), (Integer)0); Assert.assertEquals(t.getTask().getUnchecked(), (Integer)0); Assert.assertEquals(t.getStdout().trim(), "hello world"); }
@Test(groups="Integration") public void testExecEchoHello() { ProcessTaskWrapper<Integer> t = submit(SystemTasks.exec("sleep 1 ; echo hello world")); Assert.assertFalse(t.isDone()); Assert.assertEquals(t.get(), (Integer)0); Assert.assertEquals(t.getTask().getUnchecked(), (Integer)0); Assert.assertEquals(t.getStdout().trim(), "hello world"); }
@Test public void testSingleExecutionContextEntityWithRunnable() { // Should cause an exception to be thrown in future releases. For now will log a warning. // Until then make sure the task is tagged only with the context of the executor. final TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Runnable task = new AssertContextRunnable(ImmutableList.of(app)); ImmutableMap<String,?> flags = ImmutableMap.of( "tags", ImmutableList.of(BrooklynTaskTags.tagForContextEntity(entity))); app.getExecutionContext().submit(flags, task).getUnchecked(); }
@Test public void testSingleExecutionContextEntityWithTaskAndExternalFlags() { // Should cause an exception to be thrown in future releases. For now will log a warning. // Until then make sure the task is tagged only with the context of the executor. final TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Task<Void> task = Tasks.<Void>builder() .body(new AssertContextRunnable(ImmutableList.of(app))).build(); ImmutableMap<String,?> flags = ImmutableMap.of( "tags", ImmutableList.of(BrooklynTaskTags.tagForContextEntity(entity))); app.getExecutionContext().submit(flags, task).getUnchecked(); }
@Override public Void call(ConfigBag parameters) { Duration timeout = parameters.get(TIMEOUT); ConfigBag stopParameters = ConfigBag.newInstanceCopying(parameters); stopParameters.put(ShutdownEffector.STOP_APPS_FIRST, Boolean.FALSE); stopParameters.putIfAbsent(ShutdownEffector.SHUTDOWN_TIMEOUT, timeout); stopParameters.putIfAbsent(ShutdownEffector.REQUEST_TIMEOUT, timeout); DynamicTasks.queue(Effectors.invocation(entity(), STOP, stopParameters)).asTask().getUnchecked(); return null; } }
protected static Task<?> runEmptyTaskWithNameAndTags(Entity target, String name, Object ...tags) { TaskBuilder<Object> tb = newEmptyTask(name); for (Object tag: tags) tb.tag(tag); Task<?> task = ((EntityInternal)target).getExecutionContext().submit(tb.build()); task.getUnchecked(); return task; }
@Test public void testSingleExecutionContextEntityWithTask() { // Should cause an exception to be thrown in future releases. For now will log a warning. // Until then make sure the task is tagged only with the context of the executor. final TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Task<Void> task = Tasks.<Void>builder() .tag(BrooklynTaskTags.tagForContextEntity(entity)) .body(new AssertContextRunnable(ImmutableList.of(app))).build(); app.getExecutionContext().submit(task).getUnchecked(); }
public void testCompletedTaskReturnsResultImmediately() { Task<String> t = newSleepTask(Duration.ZERO, "foo"); app.getExecutionContext().submit(t).getUnchecked(); // Below, we call ValueResolver.getMaybe() from this thread, which has no execution context. // However, the task has already been submitted and we have waited for it to complete. // Therefore the ValueResolver can simply check for task.isDone() and return its result immediately. Maybe<String> result = Tasks.resolving(t).as(String.class).timeout(Duration.ZERO).getMaybe(); Assert.assertEquals(result.get(), "foo"); }