MaintenanceTaskHealthCheck.java 3.96 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.hc.api.HealthCheck
 *  org.apache.sling.hc.api.Result
 *  org.apache.sling.hc.api.ResultLog
 *  org.apache.sling.hc.util.FormattingResultLog
 */
package com.adobe.granite.maintenance.impl;

import com.adobe.granite.maintenance.MaintenanceTaskInfo;
import com.adobe.granite.maintenance.MaintenanceTaskManager;
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;

public class MaintenanceTaskHealthCheck
implements HealthCheck {
    private final MaintenanceTaskManager mtManager;
    private final String taskName;
    private final MaintenanceTaskInfo.TaskSchedule schedule;

    public MaintenanceTaskHealthCheck(MaintenanceTaskManager manager, MaintenanceTaskInfo.TaskSchedule windowSchedule, String name) {
        this.mtManager = manager;
        this.taskName = name;
        this.schedule = windowSchedule;
    }

    public Result execute() {
        FormattingResultLog resultLog = new FormattingResultLog();
        MaintenanceTaskInfo info = this.mtManager.getMaintenanceTaskInfo(this.taskName);
        if (info == null) {
            resultLog.critical("Maintenance task with name '" + this.taskName + "' does not exist.", new Object[0]);
            resultLog.debug(this.taskName + "[Check {0} in the Maintenance section of the Operations Dashboard.](/libs/granite/operations/content/maintenance.html)", new Object[0]);
            return new Result((ResultLog)resultLog);
        }
        String hintMessage = info.getTitle() + "[Check {0} in the Maintenance section of the Operations Dashboard.](/libs/granite/operations/content/maintenance.html)";
        if (info.isRunning()) {
            resultLog.info("Maintenance task with name '" + this.taskName + "' is currently running.", new Object[0]);
            return new Result((ResultLog)resultLog);
        }
        switch (info.getLastRunState()) {
            case FAILED: {
                resultLog.critical("Maintenance task with name '" + this.taskName + "' failed in the last run.", new Object[0]);
                resultLog.debug(hintMessage, new Object[0]);
                return new Result((ResultLog)resultLog);
            }
            case SUCCEEDED: {
                if (info.getSuggestedSchedule() != null) {
                    if (this.schedule == null) {
                        resultLog.critical("Maintenance task with name '" + this.taskName + "' is not scheduled.", new Object[0]);
                        resultLog.debug(hintMessage, new Object[0]);
                        return new Result((ResultLog)resultLog);
                    }
                    int ord1 = info.getSuggestedSchedule().ordinal();
                    int ord2 = this.schedule.ordinal();
                    if (ord2 > ord1) {
                        resultLog.warn("Maintenance task with name '" + this.taskName + "' is scheduled " + this.schedule.name() + " but should be scheduled " + info.getSuggestedSchedule().name() + ".", new Object[0]);
                        resultLog.debug(hintMessage, new Object[0]);
                        return new Result((ResultLog)resultLog);
                    }
                }
                resultLog.info("Maintenance task with name '" + this.taskName + "' succeeded in the last run.", new Object[0]);
                return new Result((ResultLog)resultLog);
            }
            case STOPPED: {
                resultLog.critical("Maintenance task with name '" + this.taskName + "' has been stopped in the last run.", new Object[0]);
                resultLog.debug(hintMessage, new Object[0]);
                return new Result((ResultLog)resultLog);
            }
        }
        resultLog.critical("Maintenance task with name '" + this.taskName + "' has never run.", new Object[0]);
        resultLog.debug(hintMessage, new Object[0]);
        return new Result((ResultLog)resultLog);
    }

}