Search in sources :

Example 16 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project acs-aem-commons by Adobe-Consulting-Services.

the class HealthCheckStatusEmailer method run.

@Override
public final void run() {
    log.trace("Executing ACS Commons Health Check E-mailer scheduled service");
    final List<HealthCheckExecutionResult> success = new ArrayList<>();
    final List<HealthCheckExecutionResult> failure = new ArrayList<>();
    final long start = System.currentTimeMillis();
    final HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
    options.setForceInstantExecution(true);
    options.setCombineTagsWithOr(healthCheckTagsOptionsOr);
    if (healthCheckTimeoutOverride > 0) {
        options.setOverrideGlobalTimeout(healthCheckTimeoutOverride);
    }
    final List<HealthCheckExecutionResult> results = healthCheckExecutor.execute(options, healthCheckTags);
    log.debug("Obtained [ {} ] results for Health Check tags [ {} ]", results.size(), StringUtils.join(healthCheckTags, ", "));
    for (HealthCheckExecutionResult result : results) {
        if (result.getHealthCheckResult().isOk()) {
            success.add(result);
        } else {
            failure.add(result);
        }
    }
    final long timeTaken = System.currentTimeMillis() - start;
    log.info("Executed ACS Commons Health Check E-mailer scheduled service in [ {} ms ]", timeTaken);
    if (!sendEmailOnlyOnFailure || failure.size() > 0) {
        if (nextEmailTime == null || Calendar.getInstance().after(nextEmailTime)) {
            sendEmail(success, failure, timeTaken);
            synchronized (LOCK) {
                nextEmailTime = Calendar.getInstance();
                nextEmailTime.add(Calendar.MINUTE, throttleInMins);
            }
        } else {
            log.info("Did not send e-mail as it did not meet the e-mail throttle configured time of a [ {} ] minute quiet period. Next valid time to e-mail is [ {} ]", throttleInMins, nextEmailTime.getTime());
        }
    } else {
        log.debug("Declining to send e-mail notification of 100% successful Health Check execution due to configuration.");
    }
}
Also used : ArrayList(java.util.ArrayList) HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 17 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class HealthCheckWebconsolePlugin method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    if (getStaticResource(req, resp)) {
        return;
    }
    final String tags = getParam(req, PARAM_TAGS, null);
    final boolean debug = Boolean.valueOf(getParam(req, PARAM_DEBUG, "false"));
    final boolean quiet = Boolean.valueOf(getParam(req, PARAM_QUIET, "false"));
    final boolean combineTagsWithOr = Boolean.valueOf(getParam(req, PARAM_COMBINE_TAGS_WITH_OR, "false"));
    final boolean forceInstantExecution = Boolean.valueOf(getParam(req, PARAM_FORCE_INSTANT_EXECUTION, "false"));
    final String overrideGlobalTimeoutStr = getParam(req, PARAM_OVERRIDE_GLOBAL_TIMEOUT, "");
    final PrintWriter pw = resp.getWriter();
    doForm(pw, tags, debug, quiet, combineTagsWithOr, forceInstantExecution, overrideGlobalTimeoutStr);
    // Execute health checks only if tags are specified (even if empty)
    if (tags != null) {
        HealthCheckExecutionOptions options = new HealthCheckExecutionOptions();
        options.setCombineTagsWithOr(combineTagsWithOr);
        options.setForceInstantExecution(forceInstantExecution);
        try {
            options.setOverrideGlobalTimeout(Integer.valueOf(overrideGlobalTimeoutStr));
        } catch (NumberFormatException nfe) {
        // override not set in UI
        }
        Collection<HealthCheckExecutionResult> results = healthCheckExecutor.execute(options, tags.split(","));
        pw.println("<table class='content healthcheck' cellpadding='0' cellspacing='0' width='100%'>");
        int total = 0;
        int failed = 0;
        for (final HealthCheckExecutionResult exR : results) {
            final Result r = exR.getHealthCheckResult();
            total++;
            if (!r.isOk()) {
                failed++;
            }
            if (!quiet || !r.isOk()) {
                renderResult(pw, exR, debug);
            }
        }
        final WebConsoleHelper c = new WebConsoleHelper(resp.getWriter());
        c.titleHtml("Summary", total + " HealthCheck executed, " + failed + " failures");
        pw.println("</table>");
        pw.println("<a href='configMgr/org.apache.sling.hc.core.impl.executor.HealthCheckExecutorImpl'>Configure executor</a><br/><br/>");
    }
}
Also used : HealthCheckExecutionOptions(org.apache.sling.hc.api.execution.HealthCheckExecutionOptions) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) PrintWriter(java.io.PrintWriter) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 18 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class HealthCheckWebconsolePlugin method renderResult.

private void renderResult(final PrintWriter pw, final HealthCheckExecutionResult exResult, final boolean debug) throws IOException {
    final Result result = exResult.getHealthCheckResult();
    final WebConsoleHelper c = new WebConsoleHelper(pw);
    final StringBuilder status = new StringBuilder();
    status.append("Tags: ").append(exResult.getHealthCheckMetadata().getTags());
    status.append(" Finished: ").append(new SimpleDateFormat("yyyy-MM-dd mm:ss").format(exResult.getFinishedAt()) + " after " + msHumanReadable(exResult.getElapsedTimeInMs()));
    c.titleHtml(exResult.getHealthCheckMetadata().getTitle(), null);
    c.tr();
    c.tdContent();
    c.writer().print(ResponseUtil.escapeXml(status.toString()));
    c.writer().print("<br/>Result: <span class='resultOk");
    c.writer().print(result.isOk());
    c.writer().print("'>");
    c.writer().print(result.getStatus().toString());
    c.writer().print("</span>");
    c.closeTd();
    c.closeTr();
    c.tr();
    c.tdContent();
    for (final ResultLog.Entry e : result) {
        if (!debug && e.getStatus().equals(Result.Status.DEBUG)) {
            continue;
        }
        c.writer().print("<div class='log");
        c.writer().print(e.getStatus().toString());
        c.writer().print("'>");
        c.writer().print(e.getStatus().toString());
        c.writer().print(' ');
        c.writer().print(ResponseUtil.escapeXml(e.getMessage()));
        if (e.getException() != null) {
            c.writer().print(" ");
            c.writer().print(ResponseUtil.escapeXml(e.getException().toString()));
        }
        c.writer().println("</div>");
    }
    c.closeTd();
}
Also used : ResultLog(org.apache.sling.hc.api.ResultLog) SimpleDateFormat(java.text.SimpleDateFormat) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Example 19 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult 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)

Example 20 with HealthCheckExecutionResult

use of org.apache.sling.hc.api.execution.HealthCheckExecutionResult in project sling by apache.

the class HealthCheckExecutorImpl method createResultsForDescriptor.

private HealthCheckExecutionResult createResultsForDescriptor(final HealthCheckMetadata metadata) {
    // create result for a single descriptor
    // reuse cached results where possible
    HealthCheckExecutionResult result;
    result = healthCheckResultCache.getValidCacheResult(metadata, resultCacheTtlInMs);
    if (result == null) {
        final HealthCheckFuture future;
        synchronized (this.stillRunningFutures) {
            future = createOrReuseFuture(metadata);
        }
        // wait for futures at most until timeout (but will return earlier if all futures are finished)
        waitForFuturesRespectingTimeout(Collections.singletonList(future), null);
        result = collectResultFromFuture(future);
    }
    return result;
}
Also used : HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult)

Aggregations

HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)34 Result (org.apache.sling.hc.api.Result)18 Test (org.junit.Test)16 Date (java.util.Date)9 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)9 HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)8 ExecutionResult (org.apache.sling.hc.core.impl.executor.ExecutionResult)7 ArrayList (java.util.ArrayList)6 TreeSet (java.util.TreeSet)6 LinkedList (java.util.LinkedList)5 Matchers.anyString (org.mockito.Matchers.anyString)5 SimpleDateFormat (java.text.SimpleDateFormat)4 DateFormat (java.text.DateFormat)3 HealthCheckSelector (org.apache.sling.hc.api.execution.HealthCheckSelector)3 ServiceReference (org.osgi.framework.ServiceReference)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 HashSet (java.util.HashSet)2 ResultLog (org.apache.sling.hc.api.ResultLog)2 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)2