use of org.gradle.launcher.daemon.server.expiry.DaemonExpirationListener in project gradle by gradle.
the class DefaultDaemonScanInfo method notifyOnUnhealthy.
@Override
public void notifyOnUnhealthy(final Action<? super String> listener) {
/*
The semantics of this method are that the given action should be notified if the
Daemon is going to be terminated at the end of this build.
It is not a generic outlet for “expiry events”.
Ideally, the value given would describe the problem and not be phrased in terms of why we are shutting down,
but this is a practical compromise born out of piggy backing on the expiration listener mechanism to implement it.
*/
final AtomicReference<DaemonExpirationListener> daemonExpirationListenerReference = new AtomicReference<DaemonExpirationListener>();
final DaemonExpirationListener daemonExpirationListener = new DaemonExpirationListener() {
@Override
public void onExpirationEvent(DaemonExpirationResult result) {
if (result.getStatus() == DaemonExpirationStatus.GRACEFUL_EXPIRE) {
try {
listener.execute(result.getReason());
} finally {
// set the reference to null, which says that we're taking care of removing the listener
if (daemonExpirationListenerReference.getAndSet(null) != null) {
listenerManager.removeListener(this);
}
}
}
}
};
daemonExpirationListenerReference.set(daemonExpirationListener);
listenerManager.addListener(daemonExpirationListener);
final BuildAdapter buildListener = new BuildAdapter() {
@Override
public void buildFinished(BuildResult result) {
DaemonExpirationListener daemonExpirationListener = daemonExpirationListenerReference.getAndSet(null);
if (daemonExpirationListener != null) {
listenerManager.removeListener(daemonExpirationListener);
}
listenerManager.removeListener(this);
}
};
listenerManager.addListener(buildListener);
}
Aggregations