Search in sources :

Example 1 with CountdownTimer

use of org.gradle.internal.time.CountdownTimer in project gradle by gradle.

the class WaitingReader method readLine.

String readLine() throws IOException {
    CountdownTimer timer = Timers.startTimer(timeoutMs);
    reader.mark(READAHEAD_BUFFER_SIZE);
    int character = EOF;
    while (character != NEW_LINE && character != CARRIAGE_RETURN) {
        character = reader.read();
        if (character == EOF) {
            if (timer.hasExpired()) {
                break;
            }
            try {
                Thread.sleep(clockTick);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            retriedCount++;
        }
    }
    reader.reset();
    String line = reader.readLine();
    return line;
}
Also used : CountdownTimer(org.gradle.internal.time.CountdownTimer)

Example 2 with CountdownTimer

use of org.gradle.internal.time.CountdownTimer in project gradle by gradle.

the class DaemonStopClient method stop.

/**
     * Stops all daemons, blocking until all have completed.
     */
public void stop() {
    CountdownTimer timer = Timers.startTimer(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)

Example 3 with CountdownTimer

use of org.gradle.internal.time.CountdownTimer in project gradle by gradle.

the class DefaultDaemonConnector method startDaemon.

public DaemonClientConnection startDaemon(ExplainingSpec<DaemonContext> constraint) {
    ProgressLogger progressLogger = progressLoggerFactory.newOperation(DefaultDaemonConnector.class).start("Starting Gradle Daemon", "Starting Daemon");
    final DaemonStartupInfo startupInfo = daemonStarter.startDaemon();
    LOGGER.debug("Started Gradle daemon {}", startupInfo);
    CountdownTimer timer = Timers.startTimer(connectTimeout);
    try {
        do {
            DaemonClientConnection daemonConnection = connectToDaemonWithId(startupInfo, constraint);
            if (daemonConnection != null) {
                startListener.daemonStarted(daemonConnection.getDaemon());
                return daemonConnection;
            }
            try {
                sleep(200L);
            } catch (InterruptedException e) {
                throw UncheckedException.throwAsUncheckedException(e);
            }
        } while (!timer.hasExpired());
    } finally {
        progressLogger.completed();
    }
    throw new DaemonConnectionException("Timeout waiting to connect to the Gradle daemon.\n" + startupInfo.describe());
}
Also used : CountdownTimer(org.gradle.internal.time.CountdownTimer) DaemonStartupInfo(org.gradle.launcher.daemon.diagnostics.DaemonStartupInfo) ProgressLogger(org.gradle.internal.logging.progress.ProgressLogger)

Example 4 with CountdownTimer

use of org.gradle.internal.time.CountdownTimer in project gradle by gradle.

the class DefaultDaemonConnector method connectToCanceledDaemon.

private DaemonClientConnection connectToCanceledDaemon(Collection<DaemonInfo> busyDaemons, ExplainingSpec<DaemonContext> constraint) {
    DaemonClientConnection connection = null;
    final Pair<Collection<DaemonInfo>, Collection<DaemonInfo>> canceledBusy = partitionByState(busyDaemons, Canceled);
    final Collection<DaemonInfo> compatibleCanceledDaemons = getCompatibleDaemons(canceledBusy.getLeft(), constraint);
    if (!compatibleCanceledDaemons.isEmpty()) {
        LOGGER.info(DaemonMessages.WAITING_ON_CANCELED);
        CountdownTimer timer = Timers.startTimer(CANCELED_WAIT_TIMEOUT);
        while (connection == null && !timer.hasExpired()) {
            try {
                sleep(200);
                connection = connectToIdleDaemon(daemonRegistry.getIdle(), constraint);
            } catch (InterruptedException e) {
                throw UncheckedException.throwAsUncheckedException(e);
            }
        }
    }
    return connection;
}
Also used : CountdownTimer(org.gradle.internal.time.CountdownTimer) Collection(java.util.Collection) DaemonInfo(org.gradle.launcher.daemon.registry.DaemonInfo)

Example 5 with CountdownTimer

use of org.gradle.internal.time.CountdownTimer in project gradle by gradle.

the class DaemonStateCoordinator method cancelNow.

private void cancelNow() {
    CountdownTimer timer = Timers.startTimer(cancelTimeoutMs);
    LOGGER.debug("Cancel requested: will wait for daemon to become idle.");
    try {
        cancellationToken.cancel();
    } catch (Exception ex) {
        LOGGER.error("Cancel processing failed. Will continue.", ex);
    }
    lock.lock();
    try {
        while (!timer.hasExpired()) {
            try {
                switch(state) {
                    case Idle:
                        LOGGER.debug("Cancel: daemon is idle now.");
                        return;
                    case Busy:
                    case Canceled:
                    case StopRequested:
                        LOGGER.debug("Cancel: daemon is busy, sleeping until state changes.");
                        condition.awaitUntil(timer.getExpiryTime());
                        break;
                    case Broken:
                        throw new IllegalStateException("This daemon is in a broken state.");
                    case Stopped:
                        LOGGER.debug("Cancel: daemon has stopped.");
                        return;
                }
            } catch (InterruptedException e) {
                throw UncheckedException.throwAsUncheckedException(e);
            }
        }
        LOGGER.debug("Cancel: daemon is still busy after grace period. Will force stop.");
        stopNow("cancel requested but timed out");
    } finally {
        lock.unlock();
    }
}
Also used : CountdownTimer(org.gradle.internal.time.CountdownTimer) UncheckedException(org.gradle.internal.UncheckedException) DaemonStoppedException(org.gradle.launcher.daemon.server.api.DaemonStoppedException) DaemonUnavailableException(org.gradle.launcher.daemon.server.api.DaemonUnavailableException)

Aggregations

CountdownTimer (org.gradle.internal.time.CountdownTimer)5 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 GradleException (org.gradle.api.GradleException)1 ExplainingSpec (org.gradle.api.internal.specs.ExplainingSpec)1 UncheckedException (org.gradle.internal.UncheckedException)1 ProgressLogger (org.gradle.internal.logging.progress.ProgressLogger)1 DaemonContext (org.gradle.launcher.daemon.context.DaemonContext)1 DaemonStartupInfo (org.gradle.launcher.daemon.diagnostics.DaemonStartupInfo)1 Stop (org.gradle.launcher.daemon.protocol.Stop)1 DaemonInfo (org.gradle.launcher.daemon.registry.DaemonInfo)1 DaemonStoppedException (org.gradle.launcher.daemon.server.api.DaemonStoppedException)1 DaemonUnavailableException (org.gradle.launcher.daemon.server.api.DaemonUnavailableException)1