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);
}
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;
}
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();
}
}
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();
}
}
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()));
}
}
Aggregations