Search in sources :

Example 1 with DaemonContext

use of org.gradle.launcher.daemon.context.DaemonContext in project gradle by gradle.

the class DaemonServices method getDaemonLogFile.

public File getDaemonLogFile() {
    final DaemonContext daemonContext = get(DaemonContext.class);
    final Long pid = daemonContext.getPid();
    String fileName = "daemon-" + (pid == null ? UUID.randomUUID() : pid) + ".out.log";
    return new File(get(DaemonDir.class).getVersionedDir(), fileName);
}
Also used : DaemonContext(org.gradle.launcher.daemon.context.DaemonContext) File(java.io.File)

Example 2 with DaemonContext

use of org.gradle.launcher.daemon.context.DaemonContext in project gradle by gradle.

the class DaemonRegistryUnavailableExpirationStrategy method checkExpiration.

@Override
public DaemonExpirationResult checkExpiration() {
    try {
        final DaemonContext daemonContext = daemon.getDaemonContext();
        final File daemonRegistryDir = daemonContext.getDaemonRegistryDir();
        if (!new DaemonDir(daemonRegistryDir).getRegistry().canRead()) {
            LOG.warn("Daemon registry {} became unreadable. Expiring daemon.", daemonRegistryDir);
            return new DaemonExpirationResult(GRACEFUL_EXPIRE, REGISTRY_BECAME_UNREADABLE);
        } else {
            // Check that given daemon still exists in registry - a daemon registry could be removed and recreated between checks
            List<Long> allDaemonPids = Lists.transform(daemon.getDaemonRegistry().getAll(), new Function<DaemonInfo, Long>() {

                @Override
                public Long apply(DaemonInfo info) {
                    return info.getPid();
                }
            });
            if (!allDaemonPids.contains(daemonContext.getPid())) {
                return new DaemonExpirationResult(GRACEFUL_EXPIRE, REGISTRY_ENTRY_UNEXPECTEDLY_LOST);
            }
        }
    } catch (SecurityException se) {
        LOG.warn("Daemon registry became inaccessible. Expiring daemon. Error message is '{}'", se.getMessage());
        return new DaemonExpirationResult(GRACEFUL_EXPIRE, REGISTRY_BECAME_INACCESSIBLE);
    }
    return DaemonExpirationResult.NOT_TRIGGERED;
}
Also used : DaemonContext(org.gradle.launcher.daemon.context.DaemonContext) DaemonInfo(org.gradle.launcher.daemon.registry.DaemonInfo) File(java.io.File) DaemonDir(org.gradle.launcher.daemon.registry.DaemonDir) DaemonExpirationResult(org.gradle.launcher.daemon.server.expiry.DaemonExpirationResult)

Example 3 with DaemonContext

use of org.gradle.launcher.daemon.context.DaemonContext in project gradle by gradle.

the class DaemonMain method doAction.

@Override
protected void doAction(String[] args, ExecutionListener listener) {
    // The first argument is not really used but it is very useful in diagnosing, i.e. running 'jps -m'
    if (args.length != 1) {
        invalidArgs("Following arguments are required: <gradle-version>");
    }
    // Read configuration from stdin
    List<String> startupOpts;
    File gradleHomeDir;
    File daemonBaseDir;
    int idleTimeoutMs;
    int periodicCheckIntervalMs;
    boolean singleUse;
    String daemonUid;
    DaemonParameters.Priority priority;
    List<File> additionalClassPath;
    KryoBackedDecoder decoder = new KryoBackedDecoder(new EncodedStream.EncodedInput(System.in));
    try {
        gradleHomeDir = new File(decoder.readString());
        daemonBaseDir = new File(decoder.readString());
        idleTimeoutMs = decoder.readSmallInt();
        periodicCheckIntervalMs = decoder.readSmallInt();
        singleUse = decoder.readBoolean();
        daemonUid = decoder.readString();
        priority = DaemonParameters.Priority.values()[decoder.readSmallInt()];
        int argCount = decoder.readSmallInt();
        startupOpts = new ArrayList<String>(argCount);
        for (int i = 0; i < argCount; i++) {
            startupOpts.add(decoder.readString());
        }
        int additionalClassPathLength = decoder.readSmallInt();
        additionalClassPath = new ArrayList<File>(additionalClassPathLength);
        for (int i = 0; i < additionalClassPathLength; i++) {
            additionalClassPath.add(new File(decoder.readString()));
        }
    } catch (EOFException e) {
        throw new UncheckedIOException(e);
    }
    NativeServices.initializeOnDaemon(gradleHomeDir);
    DaemonServerConfiguration parameters = new DefaultDaemonServerConfiguration(daemonUid, daemonBaseDir, idleTimeoutMs, periodicCheckIntervalMs, singleUse, priority, startupOpts);
    LoggingServiceRegistry loggingRegistry = LoggingServiceRegistry.newCommandLineProcessLogging();
    LoggingManagerInternal loggingManager = loggingRegistry.newInstance(LoggingManagerInternal.class);
    DaemonServices daemonServices = new DaemonServices(parameters, loggingRegistry, loggingManager, DefaultClassPath.of(additionalClassPath));
    File daemonLog = daemonServices.getDaemonLogFile();
    // Any logging prior to this point will not end up in the daemon log file.
    initialiseLogging(loggingManager, daemonLog);
    // Detach the process from the parent terminal/console
    ProcessEnvironment processEnvironment = daemonServices.get(ProcessEnvironment.class);
    processEnvironment.maybeDetachProcess();
    LOGGER.debug("Assuming the daemon was started with following jvm opts: {}", startupOpts);
    Daemon daemon = daemonServices.get(Daemon.class);
    daemon.start();
    try {
        DaemonContext daemonContext = daemonServices.get(DaemonContext.class);
        Long pid = daemonContext.getPid();
        daemonStarted(pid, daemon.getUid(), daemon.getAddress(), daemonLog);
        DaemonExpirationStrategy expirationStrategy = daemonServices.get(MasterExpirationStrategy.class);
        daemon.stopOnExpiration(expirationStrategy, parameters.getPeriodicCheckIntervalMs());
    } finally {
        daemon.stop();
        // TODO: Stop all daemon services
        CompositeStoppable.stoppable(daemonServices.get(GradleUserHomeScopeServiceRegistry.class)).stop();
    }
}
Also used : UncheckedIOException(org.gradle.api.UncheckedIOException) ProcessEnvironment(org.gradle.internal.nativeintegration.ProcessEnvironment) DaemonParameters(org.gradle.launcher.daemon.configuration.DaemonParameters) DaemonContext(org.gradle.launcher.daemon.context.DaemonContext) EOFException(java.io.EOFException) DaemonExpirationStrategy(org.gradle.launcher.daemon.server.expiry.DaemonExpirationStrategy) LoggingManagerInternal(org.gradle.internal.logging.LoggingManagerInternal) DefaultDaemonServerConfiguration(org.gradle.launcher.daemon.configuration.DefaultDaemonServerConfiguration) EncodedStream(org.gradle.internal.stream.EncodedStream) EntryPoint(org.gradle.launcher.bootstrap.EntryPoint) LoggingServiceRegistry(org.gradle.internal.logging.services.LoggingServiceRegistry) KryoBackedDecoder(org.gradle.internal.serialize.kryo.KryoBackedDecoder) DaemonServerConfiguration(org.gradle.launcher.daemon.configuration.DaemonServerConfiguration) DefaultDaemonServerConfiguration(org.gradle.launcher.daemon.configuration.DefaultDaemonServerConfiguration) Daemon(org.gradle.launcher.daemon.server.Daemon) DaemonServices(org.gradle.launcher.daemon.server.DaemonServices) File(java.io.File)

Example 4 with DaemonContext

use of org.gradle.launcher.daemon.context.DaemonContext in project gradle by gradle.

the class PersistentDaemonRegistry method store.

@Override
public void store(final DaemonInfo info) {
    final Address address = info.getAddress();
    final DaemonContext daemonContext = info.getContext();
    final byte[] token = info.getToken();
    final State state = info.getState();
    lock.lock();
    LOGGER.debug("Storing daemon address: {}, context: {}", address, daemonContext);
    try {
        cache.update(new PersistentStateCache.UpdateAction<DaemonRegistryContent>() {

            @Override
            public DaemonRegistryContent update(DaemonRegistryContent oldValue) {
                if (oldValue == null) {
                    // it means the registry didn't exist yet
                    oldValue = new DaemonRegistryContent();
                }
                DaemonInfo daemonInfo = new DaemonInfo(address, daemonContext, token, state);
                oldValue.removeInfo(((InetEndpoint) address).getPort());
                oldValue.setStatus(address, daemonInfo);
                return oldValue;
            }
        });
    } finally {
        lock.unlock();
    }
}
Also used : DaemonContext(org.gradle.launcher.daemon.context.DaemonContext) PersistentStateCache(org.gradle.cache.PersistentStateCache) InetEndpoint(org.gradle.internal.remote.internal.inet.InetEndpoint) Address(org.gradle.internal.remote.Address) State(org.gradle.launcher.daemon.server.api.DaemonStateControl.State)

Example 5 with DaemonContext

use of org.gradle.launcher.daemon.context.DaemonContext 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

DaemonContext (org.gradle.launcher.daemon.context.DaemonContext)5 File (java.io.File)3 EOFException (java.io.EOFException)1 HashSet (java.util.HashSet)1 GradleException (org.gradle.api.GradleException)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 ExplainingSpec (org.gradle.api.internal.specs.ExplainingSpec)1 PersistentStateCache (org.gradle.cache.PersistentStateCache)1 LoggingManagerInternal (org.gradle.internal.logging.LoggingManagerInternal)1 LoggingServiceRegistry (org.gradle.internal.logging.services.LoggingServiceRegistry)1 ProcessEnvironment (org.gradle.internal.nativeintegration.ProcessEnvironment)1 Address (org.gradle.internal.remote.Address)1 InetEndpoint (org.gradle.internal.remote.internal.inet.InetEndpoint)1 KryoBackedDecoder (org.gradle.internal.serialize.kryo.KryoBackedDecoder)1 EncodedStream (org.gradle.internal.stream.EncodedStream)1 CountdownTimer (org.gradle.internal.time.CountdownTimer)1 EntryPoint (org.gradle.launcher.bootstrap.EntryPoint)1 DaemonParameters (org.gradle.launcher.daemon.configuration.DaemonParameters)1 DaemonServerConfiguration (org.gradle.launcher.daemon.configuration.DaemonServerConfiguration)1 DefaultDaemonServerConfiguration (org.gradle.launcher.daemon.configuration.DefaultDaemonServerConfiguration)1