@Override public void onItemAdded(final Item item) { executor.submit(MutableMap.of("tag", delegate), new Runnable() { @Override public void run() { try { delegate.onItemAdded(item); } catch (Throwable t) { LOG.warn("Error notifying listener of itemAdded("+item+")", t); Exceptions.propagate(t); } } }); }
@Override public void onItemRemoved(final Item item) { executor.submit(MutableMap.of("tag", delegate), new Runnable() { @Override public void run() { try { delegate.onItemRemoved(item); } catch (Throwable t) { LOG.warn("Error notifying listener of itemAdded("+item+")", t); Exceptions.propagate(t); } } }); }
public Task<?> destroy(final Application application) { return mgmt.getExecutionManager().submit( MutableMap.of("displayName", "destroying "+application, "description", "REST call to destroy application "+application.getDisplayName()+" ("+application+")"), new Runnable() { @Override public void run() { ((EntityInternal)application).destroy(); mgmt.getEntityManager().unmanage(application); } }); }
@Test(groups="Integration") // takes 0.5s public void testGetConfigWithExecutedTaskWaitsForResult() throws Exception { LatchingCallable<String> work = new LatchingCallable<String>("abc"); Task<String> task = executionManager.submit(work); final MyOtherEntity entity = app.addChild(EntitySpec.create(MyOtherEntity.class) .configure(MyOtherEntity.STRING_KEY, task)); Future<String> getConfigFuture = executor.submit(new Callable<String>() { @Override public String call() { return entity.getConfig(MyOtherEntity.STRING_KEY); }}); assertTrue(work.latchCalled.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); assertFalse(getConfigFuture.isDone()); work.latchContinued.countDown(); assertEquals(getConfigFuture.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "abc"); assertEquals(work.callCount.get(), 1); }
protected SshPutTaskWrapper submit(final SshPutTaskFactory tf) { SshPutTaskWrapper t = tf.newTask(); mgmt.getExecutionManager().submit(t); return t; }
protected <T> ProcessTaskWrapper<T> submit(final ProcessTaskFactory<T> tf) { ProcessTaskWrapper<T> t = tf.newTask(); mgmt.getExecutionManager().submit(t); return t; }
Task<Void> readTask = ctx.getExecutionManager().submit(new Callable<Void>() { @Override public Void call() throws Exception {
@Test public void testDisplayNameMatches() throws Exception { Task<Object> task = execManager.submit(TaskBuilder.builder() .body(Callables.<Object>returning("val")) .displayName("myname") .build()); assertTrue(TaskPredicates.displayNameSatisfies(Predicates.equalTo("myname")).apply(task)); assertFalse(TaskPredicates.displayNameSatisfies(Predicates.equalTo("wrong")).apply(task)); }
@Test public void testIsEffector() throws Exception { Task<?> task = app.invoke(TestApplication.START, ImmutableMap.of("locations", ImmutableList.<Location>of())); Task<?> otherTask = execManager.submit(TaskBuilder.<Object>builder() .body(Callables.<Object>returning("val")) .build()); assertTrue(TaskPredicates.isEffector().apply(task)); assertFalse(TaskPredicates.isEffector().apply(otherTask)); }
@Test(groups="Integration") public void testSshFetch() throws IOException { String fn = Urls.mergePaths(tempDir.getPath(), "f2"); FileUtils.write(new File(fn), "hello fetched world"); SshFetchTaskFactory tf = SshTasks.newSshFetchTaskFactory(host, fn); SshFetchTaskWrapper t = tf.newTask(); mgmt.getExecutionManager().submit(t); t.block(); Assert.assertTrue(t.isDone()); Assert.assertEquals(t.get(), "hello fetched world"); Assert.assertEquals(t.getBytes(), "hello fetched world".getBytes()); }
@Test public void testDisplayNameSatisfies() throws Exception { Task<Object> task = execManager.submit(TaskBuilder.builder() .body(Callables.<Object>returning("val")) .displayName("myname") .build()); assertTrue(TaskPredicates.displayNameSatisfies(Predicates.equalTo("myname")).apply(task)); assertFalse(TaskPredicates.displayNameSatisfies(Predicates.equalTo("wrong")).apply(task)); }
@Test public void testIsTransient() throws Exception { Task<?> task = execManager.submit(TaskBuilder.<Object>builder() .body(Callables.<Object>returning("val")) .build()); assertFalse(TaskPredicates.isTransient().apply(task)); BrooklynTaskTags.setTransient(task); assertTrue(TaskPredicates.isTransient().apply(task)); }
@Test public void testIsInessential() throws Exception { Task<?> task = execManager.submit(TaskBuilder.<Object>builder() .body(Callables.<Object>returning("val")) .build()); assertFalse(TaskPredicates.isInessential().apply(task)); BrooklynTaskTags.setInessential(task); assertTrue(TaskPredicates.isInessential().apply(task)); } }
@Test public void testHasTag() throws Exception { Task<?> task = execManager.submit(TaskBuilder.<Object>builder() .body(Callables.<Object>returning("val")) .tag("mytag") .build()); assertTrue(TaskPredicates.hasTag("mytag").apply(task)); assertFalse(TaskPredicates.hasTag("wrongtag").apply(task)); }
@Test public void testDisplayNameEqualTo() throws Exception { Task<Object> task = execManager.submit(TaskBuilder.builder() .body(Callables.<Object>returning("val")) .displayName("myname") .build()); assertTrue(TaskPredicates.displayNameEqualTo("myname").apply(task)); assertFalse(TaskPredicates.displayNameEqualTo("wrong").apply(task)); }
protected <T> ProcessTaskWrapper<T> submit(final ProcessTaskFactory<T> tf) { tf.machine(host); ProcessTaskWrapper<T> t = tf.newTask(); mgmt.getExecutionManager().submit(t); return t; }
@Override protected void postStop() { super.postStop(); if (isMachineStopped()) { // Don't unmanage in entity's task context as it will self-cancel the task. Wait for the stop effector to complete (and all parent entity tasks). // If this is not enough (still getting Caused by: java.util.concurrent.CancellationException: null) then // we could wait for BrooklynTaskTags.getTasksInEntityContext(ExecutionManager, this).isEmpty(); Task<?> stopEffectorTask = BrooklynTaskTags.getClosestEffectorTask(Tasks.current(), Startable.STOP); Task<?> topEntityTask = getTopEntityTask(stopEffectorTask); getManagementContext().getExecutionManager().submit(new UnmanageTask(topEntityTask, this)); } }
protected void checkIsRunningAndStops(Entity mysql, SshMachineLocation lh) { Integer pid = getPid(mysql); Assert.assertNotNull(pid, "PID should be set as an attribute (or getPid() overridden to supply)"); Entities.submit(app, SshEffectorTasks.requirePidRunning(pid).machine(lh).newTask() ).get(); app.stop(); // let the kill -1 take effect Time.sleep(Duration.ONE_SECOND); // and assert it has died log.info("mysql in pid "+pid+" should be dead now"); // (app has stopped, so submit on mgmt context) ProcessTaskWrapper<Integer> t = SshEffectorTasks.codePidRunning(pid).machine(lh).newTask(); mgmt.getExecutionManager().submit(t); Assert.assertNotEquals(t.block().getExitCode(), 0); } }
public Task<?> expunge(final Entity entity, final boolean release) { if (mgmt.getEntitlementManager().isEntitled(Entitlements.getEntitlementContext(), Entitlements.INVOKE_EFFECTOR, Entitlements.EntityAndItem.of(entity, StringAndArgument.of("expunge", MutableMap.of("release", release))))) { Map<String, Object> flags = MutableMap.<String, Object>of("displayName", "expunging " + entity, "description", "REST call to expunge entity " + entity.getDisplayName() + " (" + entity + ")"); if (Entitlements.getEntitlementContext() != null) { flags.put("tags", MutableSet.of(BrooklynTaskTags.tagForEntitlement(Entitlements.getEntitlementContext()))); } return mgmt.getExecutionManager().submit( flags, new Runnable() { @Override public void run() { if (release) Entities.destroyCatching(entity); else mgmt.getEntityManager().unmanage(entity); } }); } throw WebResourceUtils.forbidden("User '%s' is not authorized to expunge entity %s", Entitlements.getEntitlementContext().user(), entity); }