ReplicationQueueHealthCheck.java 5.92 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.Agent
 *  com.day.cq.replication.AgentManager
 *  com.day.cq.replication.ReplicationQueue
 *  com.day.cq.replication.ReplicationQueue$Entry
 *  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.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  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.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.replication.hc.impl;

import com.day.cq.replication.Agent;
import com.day.cq.replication.AgentManager;
import com.day.cq.replication.ReplicationQueue;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(metatype=1, label="Adobe Granite Replication Queue Health Check", description="This health check checks the replication queue.")
@Properties(value={@Property(name="hc.name", value={"Replication Queue"}, label="Name", description="Name of the health check."), @Property(name="hc.tags", unbounded=PropertyUnbounded.ARRAY, label="Tags", description="Tags for the health check."), @Property(name="hc.mbean.name", value={"replicationQueue"}, label="MBean Name", description="Name of the JMX mbean to register for this check.")})
@Service(value={HealthCheck.class})
public class ReplicationQueueHealthCheck
implements HealthCheck {
    private static final Logger log = LoggerFactory.getLogger(ReplicationQueueHealthCheck.class);
    private static final int DEFAULT_NUMBER_OF_RETRIES_ALLOWED = 3;
    private int numberOfRetriesAllowed;
    @Property(intValue={3}, label="Number of Allowed Retries", description="This is the number of allowed retries for an entry.")
    private static final String NUMBER_OF_RETRIES_ALLOWED = "numberOfRetriesAllowed";
    @Reference(policy=ReferencePolicy.DYNAMIC)
    private volatile AgentManager agentManager;

    @Activate
    public void activate(Map<String, Object> properties) {
        this.numberOfRetriesAllowed = PropertiesUtil.toInteger((Object)properties.get("numberOfRetriesAllowed"), (int)3);
        log.info("Activated, numberOfRetriesAllowed={}", (Object)this.numberOfRetriesAllowed);
    }

    public Result execute() {
        FormattingResultLog resultLog = new FormattingResultLog();
        int failures = 0;
        Map agents = this.agentManager.getAgents();
        if (agents != null && agents.size() > 0) {
            for (Map.Entry s : agents.entrySet()) {
                String name = (String)s.getKey();
                Agent agent = (Agent)s.getValue();
                try {
                    if (agent.isEnabled()) {
                        ReplicationQueue q = agent.getQueue();
                        List entries = q.entries();
                        if (entries != null && entries.size() > 0) {
                            ReplicationQueue.Entry top = (ReplicationQueue.Entry)entries.get(0);
                            if (top.getNumProcessed() <= this.numberOfRetriesAllowed) {
                                resultLog.debug("Agent: [{}], first item: [{}], number of retries: {}", new Object[]{name, top.getId(), top.getNumProcessed()});
                                continue;
                            }
                            resultLog.warn("Agent: [{}], first item: [{}], number of retries: {}, expected number of retries <= {}", new Object[]{name, top.getId(), top.getNumProcessed(), this.numberOfRetriesAllowed});
                            ++failures;
                            continue;
                        }
                        resultLog.debug("No items in queue for agent [{}]", new Object[]{name});
                        continue;
                    }
                    resultLog.debug("Agent is disabled [{}]", new Object[]{name});
                }
                catch (Exception e) {
                    resultLog.warn("Exception while inspecting replication agent [{}]: {}", new Object[]{name, e});
                }
            }
        } else {
            resultLog.debug("No replication agents configured", new Object[0]);
        }
        resultLog.info("[Click here to inspect the replication agent configurations and queues.](/etc/replication.html)", new Object[0]);
        if (failures > 0) {
            resultLog.info("[Go to the 'Log Messages' section of the Diagnosis page and check for replication specific log entries.](/libs/granite/operations/content/diagnosis/tool.html/_granite_logmessages)", new Object[0]);
        }
        return new Result((ResultLog)resultLog);
    }

    protected void bindAgentManager(AgentManager agentManager) {
        this.agentManager = agentManager;
    }

    protected void unbindAgentManager(AgentManager agentManager) {
        if (this.agentManager == agentManager) {
            this.agentManager = null;
        }
    }
}