/** 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); }
/** @return The value of the most recently run task */ @Override public Object get() throws InterruptedException, ExecutionException { blockUntilStarted(); blockUntilFirstScheduleStarted(); return (groovyTruth(recentRun)) ? recentRun.get() : internalFuture.get(); }
public CreationResult<T,U> blockUntilComplete(Duration timeout) { if (task!=null) task.blockUntilEnded(timeout); return this; } public CreationResult<T,U> blockUntilComplete() { if (task!=null) task.blockUntilEnded(); return this; }
public boolean isRunning() { boolean hasActiveTasks = false; for (Task<?> task: tasks) { if (task.isBegun() && !task.isDone()) { hasActiveTasks = true; break; } } if (!started && hasActiveTasks) { log.warn("Poller should not be running, but has active tasks, tasks: "+tasks); } return started && hasActiveTasks; }
@Override public void customWriteBody(Task<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStringField("id", value.getId()); jgen.writeStringField("displayName", value.getDisplayName()); } @Override
@Override public int compare(Task<?> o1, Task<?> o2) { if (!Objects.equal(o1.isSubmitted(), o2.isSubmitted())) { return o1.isSubmitted() ? -1 : 1; Task<?> o1s = o1.getSubmittedByTask(); Task<?> o2s = o2.getSubmittedByTask(); if ("start".equals(o1.getDisplayName()) ||"start".equals(o2.getDisplayName())) { weight = 0; if (!Objects.equal(o1.isBegun() && !o1.isDone(), o2.isBegun() && !o2.isDone())) weight += 60 * (o1.isBegun() && !o1.isDone() ? -1 : 1); if (!Objects.equal(o1.isBegun(), o2.isBegun())) weight += 10 * (!o1.isBegun() ? -1 : 1); long t1 = o1.isDone() ? o1.getEndTimeUtc() : o1.isBegun() ? o1.getStartTimeUtc() : o1.getSubmitTimeUtc(); long t2 = o2.isDone() ? o2.getEndTimeUtc() : o2.isBegun() ? o2.getStartTimeUtc() : o2.getSubmitTimeUtc(); long u1 = now - t1; long u2 = now - t2; return o1.getId().compareTo(o2.getId());
if (task.isDone()) { result = WebResourceUtils.getValueForDisplay(task.get(), true, false); } else { result = null; return new TaskSummary(task.getId(), task.getDisplayName(), task.getDescription(), entityId, entityDisplayName, task.getTags(), ifPositive(task.getSubmitTimeUtc()), ifPositive(task.getStartTimeUtc()), ifPositive(task.getEndTimeUtc()), task.getStatusSummary(), result, task.isError(), task.isCancelled(), children, asLink(task.getSubmittedByTask(), ub), task.isDone() ? null : task instanceof TaskInternal ? asLink(((TaskInternal<?>)task).getBlockingTask(), ub) : null, task.isDone() ? null : task instanceof TaskInternal ? ((TaskInternal<?>)task).getBlockingDetails() : null, task.getStatusDetail(true), streams, links);
@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); }
t.cancel(true); Assert.assertTrue(t.isDone()); Assert.assertEquals(ci.next().get(), "1"); Assert.assertTrue(task2.isBegun()); Assert.assertTrue(task2.isDone()); Assert.assertTrue(task2.isCancelled()); Assert.assertTrue(task3.isDone()); Assert.assertTrue(task3.isCancelled()); Assert.assertFalse(task3.isBegun());
private void assertTaskSucceeds(Task<Void> task) { task.getUnchecked(); Assert.assertFalse(task.isError()); }
t1.asTask().blockUntilEnded(Duration.TEN_SECONDS); if (!t1.asTask().isError()) { Assert.fail("Should have thrown error during "+t1+" because no more machines available at "+loc); TaskAdaptable<Void> t2 = Entities.submit(entity, Effectors.invocation(entity, Startable.RESTART, ConfigBag.newInstance().configure(RestartSoftwareParameters.RESTART_MACHINE_TYPED, RestartMachineMode.TRUE))); t2.asTask().get();
if (task.isDone() && !task.isError()) { result = task.getUnchecked(); message += "; persisting result "+result; } else {
protected void assertSubTaskFailures(SoftwareProcess entity, Map<String, Predicate<CharSequence>> taskErrs) throws Exception { Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), entity); for (Map.Entry<String, Predicate<CharSequence>> entry : taskErrs.entrySet()) { String taskNameRegex = entry.getKey(); Predicate<? super String> errChecker = entry.getValue(); Task<?> subTask = findTaskOrSubTask(tasks, TaskPredicates.displayNameSatisfies(StringPredicates.matchesRegex(taskNameRegex))).get(); String msg = "regex="+taskNameRegex+"; task="+subTask; assertNotNull(subTask, msg); assertTrue(subTask.isDone(), msg); assertTrue(subTask.isError(), msg); try { subTask.get(); fail(); } catch (Exception e) { if (!errChecker.apply(e.toString())) { throw e; } } } }
@Override public void onTaskDone(Task<?> task) { Assert.assertTrue(task.isDone()); Object result = task.getUnchecked(); if (result != null && result.equals("foo")) { synchronized (completedTasks) { completedTasks.put(task, Duration.sinceUtc(task.getEndTimeUtc())); } latch.countDown(); } } });
@Override public void run() { if (task.isDone()) throw new IllegalStateException("Task finished early with: "+task.getUnchecked()); } });
public void doTestLatchBlocks(ConfigKey<Boolean> latch, List<String> preLatchEvents, Object latchValue, Function<? super MyService, Void> customAssertFn) throws Exception { final AttributeSensor<Object> latchSensor = Sensors.newSensor(Object.class, "latch"); final MyService entity = app.createAndManageChild(EntitySpec.create(MyService.class) .configure(ConfigKeys.newConfigKey(Object.class, latch.getName()), (Object)DependentConfiguration.attributeWhenReady(app, latchSensor))); final Task<Void> task; final Task<Void> startTask = Entities.invokeEffector(app, app, MyService.START, ImmutableMap.of("locations", ImmutableList.of(loc))); if (latch != SoftwareProcess.STOP_LATCH) { task = startTask; } else { startTask.get(Duration.THIRTY_SECONDS); task = Entities.invokeEffector(app, app, MyService.STOP); } assertEffectorBlockingDetailsEventually(entity, task.getDisplayName(), "Waiting for config " + latch.getName()); assertDriverEventsEquals(entity, preLatchEvents); assertFalse(task.isDone()); app.sensors().set(latchSensor, latchValue); customAssertFn.apply(entity); task.get(Duration.THIRTY_SECONDS); assertDriverEventsEquals(entity, getLatchPostTasks(latch)); }
private boolean waitAppShutdown(CountdownTimer shutdownTimeoutTimer, Task<?> t) throws TimeoutException { Duration waitInterval = null; //wait indefinitely if no shutdownTimeoutTimer (shutdownTimeout == 0) if (shutdownTimeoutTimer != null) { waitInterval = Duration.of(SHUTDOWN_TIMEOUT_CHECK_INTERVAL, TimeUnit.MILLISECONDS); } // waitInterval == null - blocks indefinitely while(!t.blockUntilEnded(waitInterval)) { if (shutdownTimeoutTimer.isExpired()) { log.warn("Timeout while waiting for applications to stop at "+t+".\n"+t.getStatusDetail(true)); throw new TimeoutException(); } } if (t.isError()) { log.warn("Error stopping application "+t+" during shutdown (ignoring)\n"+t.getStatusDetail(true)); return false; } else { return true; } } }.start();
@Override public Integer call() { Preconditions.checkState(x.isSubmitted()); return x.getUnchecked()+y; } }).build();
private String taskToVerboseString(Task<?> t) { return MoreObjects.toStringHelper(t) .add("id", t.getId()) .add("displayName", t.getDisplayName()) .add("submitTime", t.getSubmitTimeUtc()) .add("startTime", t.getStartTimeUtc()) .add("endTime", t.getEndTimeUtc()) .add("status", t.getStatusSummary()) .add("tags", t.getTags()) .toString(); }