Search in sources :

Example 6 with HealthCheckMetadata

use of org.apache.sling.hc.util.HealthCheckMetadata in project sling by apache.

the class HealthCheckExecutorImpl method getHealthCheckMetadata.

/**
     * Create the health check meta data
     */
private List<HealthCheckMetadata> getHealthCheckMetadata(final ServiceReference... healthCheckReferences) {
    final List<HealthCheckMetadata> descriptors = new LinkedList<HealthCheckMetadata>();
    for (final ServiceReference serviceReference : healthCheckReferences) {
        final HealthCheckMetadata descriptor = getHealthCheckMetadata(serviceReference);
        descriptors.add(descriptor);
    }
    return descriptors;
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) LinkedList(java.util.LinkedList) ServiceReference(org.osgi.framework.ServiceReference)

Example 7 with HealthCheckMetadata

use of org.apache.sling.hc.util.HealthCheckMetadata in project sling by apache.

the class HealthCheckResultCache method useValidCacheResults.

/**
     * Get the valid cache results
     */
public void useValidCacheResults(final List<HealthCheckMetadata> metadatas, final Collection<HealthCheckExecutionResult> results, final long resultCacheTtlInMs) {
    final Set<HealthCheckExecutionResult> cachedResults = new TreeSet<HealthCheckExecutionResult>();
    final Iterator<HealthCheckMetadata> checksIt = metadatas.iterator();
    while (checksIt.hasNext()) {
        final HealthCheckMetadata md = checksIt.next();
        final HealthCheckExecutionResult result = getValidCacheResult(md, resultCacheTtlInMs);
        if (result != null) {
            cachedResults.add(result);
            checksIt.remove();
        }
    }
    logger.debug("Adding {} results from cache", cachedResults.size());
    results.addAll(cachedResults);
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) TreeSet(java.util.TreeSet) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 8 with HealthCheckMetadata

use of org.apache.sling.hc.util.HealthCheckMetadata in project sling by apache.

the class AsyncHealthCheckExecutor method activate.

@Activate
protected final void activate(final ComponentContext componentContext) {
    this.bundleContext = componentContext.getBundleContext();
    this.bundleContext.addServiceListener(this);
    int count = 0;
    HealthCheckFilter healthCheckFilter = new HealthCheckFilter(bundleContext);
    final ServiceReference[] healthCheckReferences = healthCheckFilter.getHealthCheckServiceReferences(HealthCheckSelector.empty());
    for (ServiceReference serviceReference : healthCheckReferences) {
        HealthCheckMetadata healthCheckMetadata = new HealthCheckMetadata(serviceReference);
        if (isAsync(healthCheckMetadata)) {
            if (scheduleHealthCheck(healthCheckMetadata)) {
                count++;
            }
        }
    }
    LOG.debug("Scheduled {} jobs for asynchronous health checks", count);
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) HealthCheckFilter(org.apache.sling.hc.util.HealthCheckFilter) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.apache.felix.scr.annotations.Activate)

Example 9 with HealthCheckMetadata

use of org.apache.sling.hc.util.HealthCheckMetadata in project sling by apache.

the class AsyncHealthCheckExecutor method serviceChanged.

@Override
public void serviceChanged(ServiceEvent event) {
    if (bundleContext == null) {
        // already deactivated?
        return;
    }
    ServiceReference serviceReference = event.getServiceReference();
    final boolean isHealthCheck = serviceReference.isAssignableTo(bundleContext.getBundle(), HealthCheck.class.getName());
    if (isHealthCheck) {
        HealthCheckMetadata healthCheckMetadata = new HealthCheckMetadata(serviceReference);
        int eventType = event.getType();
        LOG.debug("Received service event of type {} for health check {}", eventType, healthCheckMetadata);
        if (eventType == ServiceEvent.REGISTERED) {
            scheduleHealthCheck(healthCheckMetadata);
        } else if (eventType == ServiceEvent.UNREGISTERING) {
            unscheduleHealthCheck(healthCheckMetadata);
        } else if (eventType == ServiceEvent.MODIFIED) {
            unscheduleHealthCheck(healthCheckMetadata);
            scheduleHealthCheck(healthCheckMetadata);
        }
    }
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) HealthCheck(org.apache.sling.hc.api.HealthCheck) ServiceReference(org.osgi.framework.ServiceReference)

Example 10 with HealthCheckMetadata

use of org.apache.sling.hc.util.HealthCheckMetadata in project sling by apache.

the class HealthCheckExecutorImpl method execute.

/**
     * Execute a set of health checks
     */
private List<HealthCheckExecutionResult> execute(final ServiceReference[] healthCheckReferences, HealthCheckExecutionOptions options) {
    final StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    final List<HealthCheckExecutionResult> results = new ArrayList<HealthCheckExecutionResult>();
    final List<HealthCheckMetadata> healthCheckDescriptors = getHealthCheckMetadata(healthCheckReferences);
    createResultsForDescriptors(healthCheckDescriptors, results, options);
    stopWatch.stop();
    if (logger.isDebugEnabled()) {
        logger.debug("Time consumed for all checks: {}", msHumanReadable(stopWatch.getTime()));
    }
    // sort result
    Collections.sort(results, new Comparator<HealthCheckExecutionResult>() {

        @Override
        public int compare(final HealthCheckExecutionResult arg0, final HealthCheckExecutionResult arg1) {
            return ((ExecutionResult) arg0).compareTo((ExecutionResult) arg1);
        }
    });
    return results;
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) ArrayList(java.util.ArrayList) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) StopWatch(org.apache.commons.lang.time.StopWatch)

Aggregations

HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)14 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)10 Result (org.apache.sling.hc.api.Result)8 ServiceReference (org.osgi.framework.ServiceReference)5 Date (java.util.Date)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)2 ProductInfo (com.adobe.granite.license.ProductInfo)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 List (java.util.List)1 ObjectName (javax.management.ObjectName)1 StopWatch (org.apache.commons.lang.time.StopWatch)1 Activate (org.apache.felix.scr.annotations.Activate)1 Deactivate (org.apache.felix.scr.annotations.Deactivate)1 HealthCheck (org.apache.sling.hc.api.HealthCheck)1