JobsHealthCheck.java 4.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.PropertyUnbounded
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.event.jobs.JobManager
 *  org.apache.sling.event.jobs.Statistics
 *  org.apache.sling.hc.api.HealthCheck
 *  org.apache.sling.hc.api.Result
 *  org.apache.sling.hc.api.ResultLog
 *  org.apache.sling.hc.util.FormattingResultLog
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.granite.bundles.hc.impl;

import java.util.Dictionary;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.Statistics;
import org.apache.sling.hc.api.HealthCheck;
import org.apache.sling.hc.api.Result;
import org.apache.sling.hc.api.ResultLog;
import org.apache.sling.hc.util.FormattingResultLog;
import org.osgi.service.component.ComponentContext;

@Component(immediate=1, metatype=1, label="Apache Sling Jobs Health Check")
@Properties(value={@Property(name="hc.name", value={"Sling Jobs"}, description="Health Check name", label="Name"), @Property(name="hc.tags", unbounded=PropertyUnbounded.ARRAY, description="Health Check tags", label="Tags", value={"sling", "jobs"}), @Property(name="hc.mbean.name", value={"slingJobs"}, description="Health Check MBean name", label="MBean name")})
@Service(value={HealthCheck.class})
public class JobsHealthCheck
implements HealthCheck {
    private static final int MAX_ACTIVATED_JOB_AGE = 3600000;
    private static final int MAX_QUEUED_JOB_AGE = 3600000;
    private static final int MAX_NUM_QUEUED_JOBS = 1000;
    @Property(label="Maximum Number of Queued Jobs", description="Threshold on the number of queued jobs that could exist at a certain moment", intValue={1000})
    private static final String PROP_MAX_NUM_QUEUED_JOBS = "max.queued.jobs";
    @Reference
    private JobManager jobManager;
    private int maxQueuedJobs;

    @Activate
    protected void activate(ComponentContext cc) {
        this.maxQueuedJobs = PropertiesUtil.toInteger(cc.getProperties().get("max.queued.jobs"), (int)1000);
    }

    public Result execute() {
        long lastActivatedJobTime;
        long diff;
        long lastFinishedJobTime;
        FormattingResultLog resultLog = new FormattingResultLog();
        JobManager jm = this.jobManager;
        if (jm == null) {
            resultLog.info("No JobManager available at the moment.", new Object[0]);
            return new Result((ResultLog)resultLog);
        }
        Statistics stats = jm.getStatistics();
        long numberOfQueuedJobs = stats.getNumberOfQueuedJobs();
        resultLog.info("Found {} jobs queued.", new Object[]{numberOfQueuedJobs});
        if (numberOfQueuedJobs > (long)this.maxQueuedJobs) {
            resultLog.critical("More than {} jobs queued: {}", new Object[]{this.maxQueuedJobs, numberOfQueuedJobs});
        }
        long now = System.currentTimeMillis();
        long activeJobs = stats.getNumberOfActiveJobs();
        long queuedJobs = stats.getNumberOfQueuedJobs();
        if (activeJobs > 0 && (diff = now - (lastActivatedJobTime = stats.getLastActivatedJobTime())) > 3600000) {
            resultLog.critical("There are active jobs but the last job activated was over {}sec ago ({}sec) and is not yet finished", new Object[]{3600, diff / 1000});
        }
        if (queuedJobs > 0 && (diff = now - (lastFinishedJobTime = stats.getLastFinishedJobTime())) > 3600000) {
            resultLog.critical("There are queued jobs but the last job finished was over {}sec ago: {}sec", new Object[]{3600, diff / 1000});
        }
        return new Result((ResultLog)resultLog);
    }

    protected void bindJobManager(JobManager jobManager) {
        this.jobManager = jobManager;
    }

    protected void unbindJobManager(JobManager jobManager) {
        if (this.jobManager == jobManager) {
            this.jobManager = null;
        }
    }
}