public <T extends TaskAdaptable<?>> T submit(final TaskFactory<T> taskFactory) { return Entities.submit(app, taskFactory); }
/** Breaks the parent-child relation between Tasks.current() and the task passed, * making the new task a top-level one at the target entity. * To make it visible in the UI, also tag the task with: * .tag(BrooklynTaskTags.tagForContextEntity(entity)) * .tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG) */ public static <T> Task<T> submitTopLevelTask(TaskAdaptable<T> task, Entity entity) { Task<?> currentTask = BasicExecutionManager.getPerThreadCurrentTask().get(); BasicExecutionManager.getPerThreadCurrentTask().set(null); try { return Entities.submit(entity, task).asTask(); } finally { BasicExecutionManager.getPerThreadCurrentTask().set(currentTask); } }
public String getNimbusHostname() { String result = entity.getConfig(Storm.NIMBUS_HOSTNAME); if (result != null) return result; Entity nimbus = entity.getConfig(Storm.NIMBUS_ENTITY); if (nimbus == null) { log.warn("No nimbus hostname available; using 'localhost'"); return "localhost"; } return Entities.submit(entity, DependentConfiguration.attributeWhenReady(nimbus, Attributes.HOSTNAME)).getUnchecked(); }
public void updateNeeded() { synchronized (serverPoolAddresses) { if (updateNeeded) return; updateNeeded = true; LOG.debug("queueing an update-needed task for "+this+"; update will occur shortly"); Entities.submit(this, Tasks.builder().displayName("update-needed").body(new Runnable() { @Override public void run() { if (updateNeeded) AbstractControllerImpl.this.update(); } }).build()); } }
public void updateNeeded() { synchronized (mutex) { if (updateNeeded) return; updateNeeded = true; LOG.debug("queueing an update-needed task for "+this+"; update will occur shortly"); Entities.submit(this, Tasks.builder().displayName("update-needed").body(new Runnable() { @Override public void run() { if (updateNeeded) AbstractNonProvisionedControllerImpl.this.update(); } }).build()); } }
@Override protected Integer getPid(Entity mysql) { ProcessTaskWrapper<Integer> t = Entities.submit(mysql, SshEffectorTasks.ssh("sudo cat "+ChefSoloDriverToyMySqlEntity.PID_FILE)); return Integer.parseInt(t.block().getStdout().trim()); }
@Override protected Integer getPid(Entity mysql) { ProcessTaskWrapper<Integer> t = Entities.submit(mysql, SshEffectorTasks.ssh("sudo cat "+ChefSoloDriverToyMySqlEntity.PID_FILE)); return Integer.parseInt(t.block().getStdout().trim()); }
@Override public final synchronized Entity get() { try { if (log.isDebugEnabled()) log.debug("Queuing task to resolve child "+tag); Entity result = Entities.submit(entity(), newTask()).get(); if (log.isDebugEnabled()) log.debug("Resolved "+result+" from child "+tag); return result; } catch (Exception e) { throw Exceptions.propagate(e); } }
@Test(groups="Integration") public void testKnifeWithoutConfig() { // without config it shouldn't pass // (assumes that knife global config is *not* installed on your machine) ProcessTaskWrapper<Boolean> t = Entities.submit(app, ChefServerTasks.isKnifeInstalled()); log.info("isKnifeInstalled without config returned: "+t.get()+" ("+t.getExitCode()+")\n"+t.getStdout()+"\nERR:\n"+t.getStderr()); Assert.assertFalse(t.get()); }
@Test(groups="Integration") public void testKnifeWithoutConfig() { // without config it shouldn't pass // (assumes that knife global config is *not* installed on your machine) ProcessTaskWrapper<Boolean> t = Entities.submit(app, ChefServerTasks.isKnifeInstalled()); log.info("isKnifeInstalled without config returned: "+t.get()+" ("+t.getExitCode()+")\n"+t.getStdout()+"\nERR:\n"+t.getStderr()); Assert.assertFalse(t.get()); }
/** * Set the database {@link DatastoreCommon#DATASTORE_URL urls} as attributes when they become available on the entities. */ @Override protected void preStart() { super.preStart(); DatastoreCommon leftNode = getConfig(LEFT_DATABASE); if (leftNode != null) { sensors().set(LEFT_DATASTORE_URL, Entities.submit(this, DependentConfiguration.attributeWhenReady(leftNode, DatastoreCommon.DATASTORE_URL)).getUnchecked(getDatabaseStartupDelay())); } DatastoreCommon rightNode = getConfig(RIGHT_DATABASE); if (rightNode != null) { sensors().set(RIGHT_DATASTORE_URL, Entities.submit(this, DependentConfiguration.attributeWhenReady(rightNode, DatastoreCommon.DATASTORE_URL)).getUnchecked(getDatabaseStartupDelay())); } }
@Test public void testBasicSoftwareProcessStopEverythingExplicitly() throws Exception { MyService entity = app.createAndManageChild(EntitySpec.create(MyService.class)); entity.start(ImmutableList.of(loc)); SimulatedDriver d = (SimulatedDriver) entity.getDriver(); Location machine = Iterables.getOnlyElement(entity.getLocations()); d.events.clear(); TaskAdaptable<Void> t1 = Entities.submit(entity, Effectors.invocation(entity, Startable.STOP, ConfigBag.newInstance().configure(StopSoftwareParameters.STOP_MACHINE_MODE, StopSoftwareParameters.StopMode.IF_NOT_STOPPED))); t1.asTask().get(); assertEquals(d.events, ImmutableList.of("stop")); assertEquals(entity.getLocations().size(), 0); assertTrue(loc.getAvailable().contains(machine)); }
@Test(groups="Integration") public void testKnifeWithConfig() { // requires that knife is installed on the path of login shells // (creates the config in a temp space) ChefLiveTestSupport.installBrooklynChefHostedConfig(app); ProcessTaskWrapper<Boolean> t = Entities.submit(app, ChefServerTasks.isKnifeInstalled()); log.info("isKnifeInstalled *with* config returned: "+t.get()+" ("+t.getExitCode()+")\n"+t.getStdout()+"\nERR:\n"+t.getStderr()); Assert.assertTrue(t.get()); }
@Test(groups="Integration") public void testKnifeWithConfig() { // requires that knife is installed on the path of login shells // (creates the config in a temp space) ChefLiveTestSupport.installBrooklynChefHostedConfig(app); ProcessTaskWrapper<Boolean> t = Entities.submit(app, ChefServerTasks.isKnifeInstalled()); log.info("isKnifeInstalled *with* config returned: "+t.get()+" ("+t.getExitCode()+")\n"+t.getStdout()+"\nERR:\n"+t.getStderr()); Assert.assertTrue(t.get()); }
/** * Stops the entity and its children. * <p/> * Subclasses should override {@link #doStop} to customise behaviour. */ @Override public final void stop() { if (DynamicTasks.getTaskQueuingContext() != null) { doStop(); } else { Task<?> task = Tasks.builder().displayName("stop").body(new Runnable() { @Override public void run() { doStop(); } }).build(); Entities.submit(this, task).getUnchecked(); } }
protected static <T> T getConfigInTask(final Entity entity, final ConfigKey<T> key) { return Entities.submit(entity, Tasks.<T>builder().body(new Callable<T>() { @Override public T call() throws Exception { return entity.getConfig(key); } }).build()).getUnchecked(); }
private RuntimeException fail(final String msg) { if (getConfig(FAIL_IN_SUB_TASK)) { Task<?> task = Tasks.builder() .displayName(msg) .body(new Runnable() { @Override public void run() { throw newException(msg); } }).build(); Entities.submit(this, task).getUnchecked(); Assert.fail("Should have thrown exception on task.getUnchecked"); throw new IllegalStateException("unreachable code"); } else { throw newException(msg); } }
@Override public Map<String, Object> batchConfigRead(String application, String entityToken, Boolean raw) { // TODO: add test Entity entity = brooklyn().getEntity(application, entityToken); if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_ENTITY, entity)) { throw WebResourceUtils.forbidden("User '%s' is not authorized to see entity '%s'", Entitlements.getEntitlementContext().user(), entity); } // wrap in a task for better runtime view return Entities.submit(entity, Tasks.<Map<String,Object>>builder().displayName("REST API batch config read").body(new BatchConfigRead(mgmt(), this, entity, raw)).build()).getUnchecked(); }
private boolean fileExists(MySqlNode node, String binLogName) { String dataDir = Strings.nullToEmpty(node.getConfig(MySqlNode.DATA_DIR)); String path = Os.mergePathsUnix(dataDir, binLogName); String cmd = BashCommands.chain( "cd $RUN_DIR", BashCommands.requireTest(String.format("-f \"%s\"", path), "File " + path + " doesn't exist.")); String summary = "Check if file " + path + " exists"; SshMachineLocation machine = EffectorTasks.getSshMachine(node); return Entities.submit(node, SshTasks.newSshExecTaskFactory(machine, cmd) .allowingNonZeroExitCode() .environmentVariable("RUN_DIR", node.getAttribute(SoftwareProcess.RUN_DIR)) .summary(summary) .allowingNonZeroExitCode()).asTask().getUnchecked() == 0; } private void cleanData() {
private void deleteSnapshot(MySqlCluster cluster) { ReplicationSnapshot replicationSnapshot = cluster.getAttribute(MySqlCluster.REPLICATION_LAST_SLAVE_SNAPSHOT); Entity snapshotEntity = mgmt.getEntityManager().getEntity(replicationSnapshot.getEntityId()); SshMachineLocation machine = EffectorTasks.getSshMachine(snapshotEntity); Entities.submit(snapshotEntity, SshEffectorTasks.ssh( "cd $RUN_DIR", "rm " + replicationSnapshot.getSnapshotPath()) .summary("clear snapshot") .machine(machine) .environmentVariable("RUN_DIR", snapshotEntity.getAttribute(MySqlNode.RUN_DIR)) .requiringExitCodeZero()) .asTask() .getUnchecked(); }