Search in sources :

Example 1 with ExplainingSpec

use of org.gradle.api.internal.specs.ExplainingSpec in project gradle by gradle.

the class DaemonStopClient method stop.

/**
 * Stops all daemons, blocking until all have completed.
 */
public void stop() {
    CountdownTimer timer = Time.startCountdownTimer(STOP_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    final Set<String> seen = new HashSet<String>();
    ExplainingSpec<DaemonContext> spec = new ExplainingSpec<DaemonContext>() {

        @Override
        public String whyUnsatisfied(DaemonContext element) {
            return "already seen";
        }

        @Override
        public boolean isSatisfiedBy(DaemonContext element) {
            return !seen.contains(element.getUid());
        }
    };
    DaemonClientConnection connection = connector.maybeConnect(spec);
    if (connection == null) {
        LOGGER.lifecycle(DaemonMessages.NO_DAEMONS_RUNNING);
        return;
    }
    LOGGER.lifecycle("Stopping Daemon(s)");
    // iterate and stop all daemons
    int numStopped = 0;
    while (connection != null && !timer.hasExpired()) {
        try {
            seen.add(connection.getDaemon().getUid());
            LOGGER.debug("Requesting daemon {} stop now", connection.getDaemon());
            boolean stopped = stopDispatcher.dispatch(connection, new Stop(idGenerator.generateId(), connection.getDaemon().getToken()));
            if (stopped) {
                numStopped++;
            }
        } finally {
            connection.stop();
        }
        connection = connector.maybeConnect(spec);
    }
    if (numStopped > 0) {
        LOGGER.lifecycle(numStopped + " Daemon" + ((numStopped > 1) ? "s" : "") + " stopped");
    }
    if (connection != null) {
        throw new GradleException(String.format("Timeout waiting for all daemons to stop. Waited %s.", timer.getElapsed()));
    }
}
Also used : DaemonContext(org.gradle.launcher.daemon.context.DaemonContext) CountdownTimer(org.gradle.internal.time.CountdownTimer) ExplainingSpec(org.gradle.api.internal.specs.ExplainingSpec) Stop(org.gradle.launcher.daemon.protocol.Stop) GradleException(org.gradle.api.GradleException) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 GradleException (org.gradle.api.GradleException)1 ExplainingSpec (org.gradle.api.internal.specs.ExplainingSpec)1 CountdownTimer (org.gradle.internal.time.CountdownTimer)1 DaemonContext (org.gradle.launcher.daemon.context.DaemonContext)1 Stop (org.gradle.launcher.daemon.protocol.Stop)1