/** Invokes in parallel if multiple, but otherwise invokes the item directly. */ public static Task<?> invokeEffector(Entity callingEntity, Iterable<? extends Entity> entitiesToCall, final Effector<?> effector) { return invokeEffector(callingEntity, entitiesToCall, effector, Collections.<String,Object>emptyMap()); }
/** Invokes in parallel if multiple, but otherwise invokes the item directly. */ public static Task<?> invokeEffector(Entity callingEntity, Iterable<? extends Entity> entitiesToCall, final Effector<?> effector, final Map<String,?> parameters) { if (Iterables.size(entitiesToCall)==1) return invokeEffector(callingEntity, entitiesToCall.iterator().next(), effector, parameters); else return invokeEffectorList(callingEntity, entitiesToCall, effector, parameters); }
public static <T> Task<T> invokeEffector(Entity callingEntity, Entity entityToCall, final Effector<T> effector) { return invokeEffector(callingEntity, entityToCall, effector, Collections.<String,Object>emptyMap()); }
@SuppressWarnings("unchecked") public static <T> Task<T> invokeEffectorWithArgs(Entity callingEntity, Entity entityToCall, final Effector<T> effector, Object ...args) { return invokeEffector(callingEntity, entityToCall, effector, EffectorUtils.prepareArgsForEffectorAsMapFromArray(effector, args)); }
public Task<?> start(Application app, List<? extends Location> locations) { return Entities.invokeEffector(app, app, Startable.START, MutableMap.of("locations", locations)); }
public static final void stop(Entity caller, Entity target, Duration timeout) { if (target != null && Entities.isManaged(target)) { boolean stopped = Entities.invokeEffector(caller, target, Startable.STOP) .blockUntilEnded(timeout); if (!stopped) { LOG.debug("{} may not have stopped. Proceeding to stop {} anyway", target, caller); } } }
@Override public void stop() { Entities.invokeEffector(getEntity(), getOwnerEntity(), Startable.STOP); }
public static <T extends Application> CreationResult<T,Void> start(T app) { Task<Void> task = Entities.invokeEffector(app, app, Startable.START, // locations already set in the entities themselves; // TODO make it so that this arg does not have to be supplied to START ! MutableMap.of("locations", MutableList.of())); return CreationResult.of(app, task); }
@Test(groups="Integration") public void testSshDateEffector1() { Task<String> call = Entities.invokeEffector(app, app, GET_REMOTE_DATE_1); log.info("ssh date 1 gives: "+call.getUnchecked()); Assert.assertTrue(call.getUnchecked().indexOf("201") > 0); }
/** convenience for starting an entity, esp a new Startable instance which has been created dynamically * (after the application is started) */ public static void start(Entity e, Collection<? extends Location> locations) { if (!isManaged(e) && !manage(e)) { log.warn("Using deprecated discouraged mechanism to start management -- Entities.start(Application, Locations) -- caller should create and use the preferred management context"); startManagement(e); } if (e instanceof Startable) Entities.invokeEffector(e, e, Startable.START, MutableMap.of("locations", locations)).getUnchecked(); }
@Test(groups="Integration") public void testSshDateEffector2() { Task<String> call = Entities.invokeEffector(app, app, GET_REMOTE_DATE_2); log.info("ssh date 2 gives: "+call.getUnchecked()); Assert.assertTrue(call.getUnchecked().indexOf("201") == 0); }
@Override public String executeScript(String commands) { return Entities.invokeEffector(this, this, EXECUTE_SCRIPT, ConfigBag.newInstance().configure(ExecuteScriptEffectorBody.SCRIPT, commands).getAllConfig()).getUnchecked(); }
@Override public void resetPasswordOnChildren(String newPassword, Integer secretPin) throws Exception { for (TestEntityWithEffectors child : Iterables.filter(getChildren(), TestEntityWithEffectors.class)) { Entities.invokeEffector(this, child, RESET_PASSWORD, ImmutableMap.of("newPassword", newPassword, "secretPin", secretPin)).get(); child.resetPassword(newPassword, secretPin); } } }
@Override public void run() { Task<?> task = Entities.invokeEffector(app, entities, TestEntity.MY_EFFECTOR); try { task.get(); } catch (Exception e) { throw Exceptions.propagate(e); } }})); }
@Override public void removeRegion(String id) { Entity entity = getManagementContext().getEntityManager().getEntity(id); Preconditions.checkNotNull(entity, "No entity found for %s", id); Preconditions.checkArgument(this.equals(entity.getParent()), "Wrong parent (%s) for %s", entity.getParent(), entity); Collection<Location> childLocations = entity.getLocations(); if (entity instanceof Startable) { try { Entities.invokeEffector(this, entity, Startable.STOP).get(); } catch (Exception e) { Exceptions.propagateIfFatal(e); log.warn("Error stopping "+entity+" ("+e+"); proceeding to remove it anyway"); log.debug("Error stopping "+entity+" ("+e+"); proceeding to remove it anyway", e); } } removeChild(entity); removeLocations(childLocations); }
@Test(groups="Integration") public void testBadExitCodeCaughtAndStdErrAvailable() { final ProcessTaskWrapper<?>[] sshTasks = new ProcessTaskWrapper[1]; Task<Void> call = Entities.invokeEffector(app, app, Effectors.effector(Void.class, "badExitCode") .impl(new SshEffectorBody<Void>() { @Override public Void call(ConfigBag parameters) { sshTasks[0] = queue( ssh(COMMAND_THAT_DOES_NOT_EXIST).requiringExitCodeZero() ); return null; } }).build() ); call.blockUntilEnded(); Assert.assertTrue(call.isError()); log.info("stderr gives: "+new String(sshTasks[0].getStderr())); Assert.assertTrue(new String(sshTasks[0].getStderr()).indexOf(COMMAND_THAT_DOES_NOT_EXIST) >= 0); }
@Override public void start(Collection<? extends Location> locations) { if (isLegacyConstruction()) { // TODO should no longer be needed? init(); } locations = MutableList.copyOf(Locations.getLocationsCheckingAncestors(locations, this)); Iterables.getOnlyElement(locations); // Assert just one // set it; here we don't allow changing locations addLocations(locations); List<Entity> childrenToStart = MutableList.<Entity>of(getCluster()); // Set the KafkaZookeeper entity as child of cluster, if it does not already have a parent if (getZooKeeper().getParent() == null) { addChild(getZooKeeper()); } // And only start zookeeper if we are parent if (Objects.equal(this, getZooKeeper().getParent())) childrenToStart.add(getZooKeeper()); Entities.invokeEffector(this, childrenToStart, Startable.START, ImmutableMap.of("locations", locations)).getUnchecked(); }
@Override public void stop() { ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPING); try { Iterable<Entity> stoppableChildren = Iterables.filter(getChildren(), Predicates.instanceOf(Startable.class)); Task<?> invoke = Entities.invokeEffector(this, stoppableChildren, Startable.STOP); if (invoke != null) invoke.get(); ServiceStateLogic.setExpectedState(this, Lifecycle.STOPPED); sensors().set(SERVICE_UP, false); } catch (Exception e) { ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE); throw Exceptions.propagate(e); } }
@Test public void testInvokeEffectorDirectlyIncludesException() throws Exception { try { entity.myEffector(); } catch (Exception e) { assertExceptionContainsIsEntitledStack(e); } try { Entities.invokeEffector(app, entity, TestEntity.MY_EFFECTOR).get(); } catch (Exception e) { assertExceptionContainsIsEntitledStack(e); } }
@Override public StubContainerLocation obtain(Map<?, ?> flags) throws NoMachinesAvailableException { Entity callerContext = (Entity) flags.get(LocationConfigKeys.CALLER_CONTEXT.getName()); if (callerContext == null) callerContext = getOwner(); DynamicCluster cluster = dockerHost.getDockerContainerCluster(); Entity added = cluster.addNode(machine, flags); if (added == null) { throw new NoMachinesAvailableException(String.format("Failed to create container at %s", dockerHost)); } else { Entities.invokeEffector(callerContext, added, Startable.START, MutableMap.of("locations", ImmutableList.of(machine))).getUnchecked(); } StubContainer container = (StubContainer) added; return container.getDynamicLocation(); }