Skip to content

Instantly share code, notes, and snippets.

@ghenzler
Created September 12, 2018 15:05
Show Gist options
  • Save ghenzler/ea6cd808243a79eec320760e49475226 to your computer and use it in GitHub Desktop.
Save ghenzler/ea6cd808243a79eec320760e49475226 to your computer and use it in GitHub Desktop.
import org.apache.sling.hc.api.HealthCheck
//-------------------------------------
// Hostile Health Check
//-------------------------------------
// !!! Be careful, this creates a long-running health check blocking one
// thread of the HC thread pool. Unregister the hostile health check by
// creating a file "hc-unregister" in the current working dir (usually the
// dir next to crx-quickstart folder) and running the HC one more time
import org.apache.sling.hc.api.Result
import org.apache.sling.hc.util.FormattingResultLog
File currentWorkingDir = new File(System.getProperty('user.dir'))
dynamicHealthCheck = new HealthCheck() {
def serviceRegistration
def repository
Result execute() {
FormattingResultLog log = new FormattingResultLog()
log.info("Groovy Health Check")
for(int i=1; i<10;i++) {
def hcSession = null
try {
hcSession = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
hcSession.getNode("/").recurse { node ->
log.debug("node: "+node.getPath())
}
} finally {
if(hcSession!=null) {
hcSession.logout()
}
}
}
def markerFile = new File(currentWorkingDir, "hc-unregister")
if(markerFile.exists()) {
markerFile.renameTo(new File(currentWorkingDir, "hc-unregister-done"))
serviceRegistration.unregister()
}
return new Result(log)
}
}
resourceChangeListenerReg = bundleContext.registerService(
"org.apache.sling.hc.api.HealthCheck",
dynamicHealthCheck,
new Hashtable([(HealthCheck.NAME): "Hostile Health Check", (HealthCheck.TAGS): ["groovy","dashboard"]]))
dynamicHealthCheck.serviceRegistration = resourceChangeListenerReg
dynamicHealthCheck.repository = session.getRepository()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment