@Test(groups = "Integration") @Override public void testGetMachineDetails() throws Exception { BasicExecutionManager execManager = new BasicExecutionManager("mycontextid"); BasicExecutionContext execContext = new BasicExecutionContext(execManager); try { MachineDetails details = execContext.submit(new Callable<MachineDetails>() { @Override public MachineDetails call() { return host.getMachineDetails(); }}).get(); LOG.info("machineDetails="+details); assertNotNull(details); } finally { execManager.shutdownNow(); } }
protected void submitIfNecessary(TaskAdaptable<?> task) { if (!task.asTask().isSubmitted()) { if (BasicExecutionContext.getCurrentExecutionContext() == null) { throw new IllegalStateException("Compound task ("+task+") launched from "+this+" missing required execution context"); } else { BasicExecutionContext.getCurrentExecutionContext().submit(task); } } }
@Override public boolean isShutdown() { return getExecutionManager().isShutdown(); }
@Override public T call() throws Exception { ec.submit(task); return task.get(); } }).build();
@Override public ExecutionContext getServerExecutionContext() { // BEC is a thin wrapper around EM so fine to create a new one here ImmutableSet<Object> tags = ImmutableSet.<Object>of( this, BrooklynTaskTags.BROOKLYN_SERVER_TASK_TAG ); return new BasicExecutionContext(MutableMap.of("tags", tags), getExecutionManager()); }
protected <T> Task<T> submitInternal(Map<?,?> propertiesQ, final Object task) { if (task instanceof TaskAdaptable<?> && !(task instanceof Task<?>)) return submitInternal(propertiesQ, ((TaskAdaptable<?>)task).asTask()); Entity target = BrooklynTaskTags.getWrappedEntityOfType(taskTags, BrooklynTaskTags.TARGET_ENTITY); checkUserSuppliedContext(task, taskTags); return submit(Tasks.<T>builder().displayName("Cross-context execution: "+t.getDescription()).dynamic(true).body(new Callable<T>() { @Override public T call() { submit(Tasks.<T>builder().displayName("Cross-context execution").dynamic(true).body(() -> { if (task instanceof Callable) { return DynamicTasks.queue( Tasks.<T>builder().dynamic(false).body((Callable<T>)task).build() ).getUnchecked();
/** * Resolves key in the * {@link BasicExecutionContext#getCurrentExecutionContext current execution context}. * @see #resolve(ConfigBag, ConfigKey, ExecutionContext) */ public static <T> T resolve(ConfigBag configBag, ConfigKey<T> key) { return resolve(configBag, key, BasicExecutionContext.getCurrentExecutionContext()); }
BasicExecutionContext oldExecutionContext = getCurrentExecutionContext(); registerPerThreadExecutionContext();
@Test(groups="Integration") public void testWaitForFileContentsWhenContentsAppearAfterStart() throws Exception { String fileContent = "mycontents"; String cmd = BashCommands.waitForFileContents(destFile.getAbsolutePath(), fileContent, Duration.THIRTY_SECONDS, false); ProcessTaskWrapper<String> t = execRequiringZeroAndReturningStdout(loc, cmd); exec.submit(t); // sleep for long enough to ensure the ssh command is definitely executing Thread.sleep(5*1000); assertFalse(t.isDone()); Files.write(fileContent, destFile, Charsets.UTF_8); String output = t.get(); assertFalse(output.contains("Couldn't find"), "output="+output); }
@BeforeClass public void setup() { em = new BasicExecutionManager("mycontext"); ec = new BasicExecutionContext(em); }
@Override protected <T> Task<T> runAtEntity(final Entity entity, final Effector<T> eff, @SuppressWarnings("rawtypes") final Map parameters) { manageIfNecessary(entity, eff); // prefer to submit this from the current execution context so it sets up correct cross-context chaining ExecutionContext ec = BasicExecutionContext.getCurrentExecutionContext(); if (ec == null) { log.debug("Top-level effector invocation: {} on {}", eff, entity); ec = getExecutionContext(entity); } return runAtEntity(entity, Effectors.invocation(entity, eff, parameters)); }
@Test(groups="Integration") public void testDownloadToStdout() throws Exception { ProcessTaskWrapper<String> t = SshTasks.newSshExecTaskFactory(loc, "cd "+destFile.getParentFile().getAbsolutePath(), BashCommands.downloadToStdout(Arrays.asList(sourceFileUrl1))+" | sed s/my/your/") .requiringZeroAndReturningStdout().newTask(); String result = exec.submit(t).get(); assertTrue(result.trim().equals("yoursource1"), "Wrong contents of stdout download: "+result); }
@Test public void testGetMachineDetails() throws Exception { String response = Joiner.on("\n").join( "name:Test OS Y", "version:1.2.3", "architecture:x86_64", "ram:1234", "cpus:3"); RecordingSshTool.setCustomResponse(".*uname.*", new CustomResponse(0, response, "")); BasicExecutionManager execManager = new BasicExecutionManager("mycontextid"); BasicExecutionContext execContext = new BasicExecutionContext(execManager); try { MachineDetails details = execContext.submit(new Callable<MachineDetails>() { @Override public MachineDetails call() { return host.getMachineDetails(); }}).get(); LOG.info("machineDetails="+details); assertNotNull(details); assertEquals(details.getOsDetails().getName(), "Test OS Y", "details="+details); assertEquals(details.getOsDetails().getVersion(), "1.2.3", "details="+details); assertEquals(details.getOsDetails().getArch(), "x86_64", "details="+details); assertEquals(details.getHardwareDetails().getCpuCount(), Integer.valueOf(3), "details="+details); assertEquals(details.getHardwareDetails().getRam(), Integer.valueOf(1234), "details="+details); } finally { execManager.shutdownNow(); } }
@Override public T call() throws Exception { if (!task.asTask().isSubmitted()) { BasicExecutionContext.getCurrentExecutionContext().submit(task); } return transformer.apply(task.asTask().get()); }}); }
@BeforeMethod(alwaysRun=true) public void setUp() { em = new BasicExecutionManager("mycontext"); ec = new BasicExecutionContext(em); ex = Executors.newCachedThreadPool(); data = Collections.synchronizedMap(new LinkedHashMap<Object,Object>()); started = new Semaphore(0); waitInTask = new Semaphore(0); cancelledWhileSleeping = new Semaphore(0); }
private boolean orSubmitInternal() { if (!wasQueued()) { if (isQueuedOrSubmitted()) { log.warn("Redundant call to execute "+getTask()+"; skipping"); return false; } else { ExecutionContext ec = execContext; if (ec==null) ec = BasicExecutionContext.getCurrentExecutionContext(); if (ec==null) throw new IllegalStateException("Cannot execute "+getTask()+" without an execution context; ensure caller is in an ExecutionContext"); ec.submit(getTask()); return true; } } else { return false; } } /** causes the task to be submitted (asynchronously) if it hasn't already been,
@Test(groups="Integration") public void testAlternatives() throws Exception { ProcessTaskWrapper<Integer> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.alternatives(Arrays.asList("asdfj_no_such_command_1", "exit 88"))) .newTask(); Integer returnCode = exec.submit(t).get(); log.info("alternatives for bad commands gave: "+returnCode+"; err="+new String(t.getStderr())+"; out="+new String(t.getStdout())); assertEquals(returnCode, (Integer)88); }
BasicExecutionContext execContext = new BasicExecutionContext(execManager); try { MachineDetails details = execContext.submit(new Callable<MachineDetails>() { @Override public MachineDetails call() {
@Override public void spawnchild() throws Exception { // spawn a child, then wait BasicExecutionContext.getCurrentExecutionContext().submit( MutableMap.of("displayName", "SpawnedChildName"), new Callable<Void>() { @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 public ExecutionContext getExecutionContext(Entity e) { // BEC is a thin wrapper around EM so fine to create a new one here; but make sure it gets the real entity if (e instanceof AbstractEntity) { ImmutableSet<Object> tags = ImmutableSet.<Object>of( BrooklynTaskTags.tagForContextEntity(e), this ); return new BasicExecutionContext(MutableMap.of("tags", tags), getExecutionManager()); } else { return ((EntityInternal)e).getExecutionContext(); } }