AlertManager.java 9.1 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.escheduler.server.utils;


import cn.escheduler.common.enums.AlertType;
import cn.escheduler.common.enums.CommandType;
import cn.escheduler.common.enums.ShowType;
import cn.escheduler.common.enums.WarningType;
import cn.escheduler.common.utils.DateUtils;
import cn.escheduler.common.utils.JSONUtils;
import cn.escheduler.dao.AlertDao;
import cn.escheduler.dao.DaoFactory;
import cn.escheduler.dao.model.Alert;
B
baoliang 已提交
29
import cn.escheduler.dao.model.ProcessDefinition;
L
ligang 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import cn.escheduler.dao.model.ProcessInstance;
import cn.escheduler.dao.model.TaskInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * alert manager
 */
public class AlertManager {

    private static final Logger logger = LoggerFactory.getLogger(AlertManager.class);

    private AlertDao alertDao = DaoFactory.getDaoInstance(AlertDao.class);


    /**
     *  command type convert chinese
     * @param commandType
     * @return
     */
    private String getCommandCnName(CommandType commandType) {
        switch (commandType) {
            case RECOVER_TOLERANCE_FAULT_PROCESS:
B
baoliang 已提交
58
                return "recover tolerance fault process";
L
ligang 已提交
59
            case RECOVER_SUSPENDED_PROCESS:
B
baoliang 已提交
60
                return "recover suspended process";
L
ligang 已提交
61
            case START_CURRENT_TASK_PROCESS:
B
baoliang 已提交
62
                return "start current task process";
L
ligang 已提交
63
            case START_FAILURE_TASK_PROCESS:
B
baoliang 已提交
64
                return "start failure task process";
L
ligang 已提交
65
            case START_PROCESS:
B
baoliang 已提交
66
                return "start process";
L
ligang 已提交
67
            case REPEAT_RUNNING:
B
baoliang 已提交
68
                return "repeat running";
L
ligang 已提交
69
            case SCHEDULER:
B
baoliang 已提交
70
                return "scheduler";
L
ligang 已提交
71
            case COMPLEMENT_DATA:
B
baoliang 已提交
72
                return "complement data";
L
ligang 已提交
73
            case PAUSE:
B
baoliang 已提交
74
                return "pause";
L
ligang 已提交
75
            case STOP:
B
baoliang 已提交
76
                return "stop";
L
ligang 已提交
77
            default:
B
baoliang 已提交
78
                return "unknown type";
L
ligang 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        }
    }

    /**
     *  process instance format
     */
    private static final String PROCESS_INSTANCE_FORMAT =
            "\"Id:%d\"," +
            "\"Name:%s\"," +
            "\"Job type: %s\"," +
            "\"State: %s\"," +
            "\"Recovery:%s\"," +
            "\"Run time: %d\"," +
            "\"Start time: %s\"," +
            "\"End time: %s\"," +
            "\"Host: %s\"" ;

    /**
     *  get process instance content
     * @param processInstance
     * @return
     */
    public String getContentProcessInstance(ProcessInstance processInstance,
                                            List<TaskInstance> taskInstances){

        String res = "";
        if(processInstance.getState().typeIsSuccess()){
            res = String.format(PROCESS_INSTANCE_FORMAT,
                    processInstance.getId(),
                    processInstance.getName(),
                    getCommandCnName(processInstance.getCommandType()),
                    processInstance.getState().toString(),
                    processInstance.getRecovery().toString(),
                    processInstance.getRunTimes(),
                    DateUtils.dateToString(processInstance.getStartTime()),
                    DateUtils.dateToString(processInstance.getEndTime()),
                    processInstance.getHost()

            );
            res = "[" + res + "]";
        }else if(processInstance.getState().typeIsFailure()){

            List<LinkedHashMap> failedTaskList = new ArrayList<>();

            for(TaskInstance task : taskInstances){
                if(task.getState().typeIsSuccess()){
                    continue;
                }
                LinkedHashMap<String, String> failedTaskMap = new LinkedHashMap();
B
baoliang 已提交
128 129 130 131 132 133
                failedTaskMap.put("task id", String.valueOf(task.getId()));
                failedTaskMap.put("task name", task.getName());
                failedTaskMap.put("task type", task.getTaskType());
                failedTaskMap.put("task state", task.getState().toString());
                failedTaskMap.put("task start time", DateUtils.dateToString(task.getStartTime()));
                failedTaskMap.put("task end time", DateUtils.dateToString(task.getEndTime()));
L
ligang 已提交
134
                failedTaskMap.put("host", task.getHost());
B
baoliang 已提交
135
                failedTaskMap.put("log path", task.getLogPath());
L
ligang 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                failedTaskList.add(failedTaskMap);
            }
            res = JSONUtils.toJson(failedTaskList);
        }

        return res;
    }

    /**
     *  getting worker fault tolerant content
     * @param processInstance
     * @param toleranceTaskList
     * @return
     */
    private String getWorkerToleranceContent(ProcessInstance processInstance, List<TaskInstance> toleranceTaskList){

        List<LinkedHashMap<String, String>> toleranceTaskInstanceList =  new ArrayList<>();

        for(TaskInstance taskInstance: toleranceTaskList){
            LinkedHashMap<String, String> toleranceWorkerContentMap = new LinkedHashMap();
B
baoliang 已提交
156 157 158 159
            toleranceWorkerContentMap.put("process name", processInstance.getName());
            toleranceWorkerContentMap.put("task name", taskInstance.getName());
            toleranceWorkerContentMap.put("host", taskInstance.getHost());
            toleranceWorkerContentMap.put("task retry times", String.valueOf(taskInstance.getRetryTimes()));
L
ligang 已提交
160 161 162 163 164 165 166 167 168 169
            toleranceTaskInstanceList.add(toleranceWorkerContentMap);
        }
        return JSONUtils.toJson(toleranceTaskInstanceList);
    }

    /**
     * send worker alert fault tolerance
     * @param processInstance
     * @param toleranceTaskList
     */
B
baoliang 已提交
170
    public void sendAlertWorkerToleranceFault(ProcessInstance processInstance, List<TaskInstance> toleranceTaskList){
L
ligang 已提交
171
        Alert alert = new Alert();
B
baoliang 已提交
172
        alert.setTitle("worker fault tolerance");
L
ligang 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        alert.setShowType(ShowType.TABLE);
        String content = getWorkerToleranceContent(processInstance, toleranceTaskList);
        alert.setContent(content);
        alert.setAlertType(AlertType.EMAIL);
        alert.setCreateTime(new Date());
        alert.setAlertGroupId(processInstance.getWarningGroupId());
        alert.setReceivers(processInstance.getProcessDefinition().getReceivers());
        alert.setReceiversCc(processInstance.getProcessDefinition().getReceiversCc());

        alertDao.addAlert(alert);
        logger.info("add alert to db , alert : {}", alert.toString());

    }

    /**
     * send process instance alert
     * @param processInstance
     */
B
baoliang 已提交
191 192
    public void sendAlertProcessInstance(ProcessInstance processInstance,
                                         List<TaskInstance> taskInstances){
L
ligang 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

        boolean sendWarnning = false;
        WarningType warningType = processInstance.getWarningType();
        switch (warningType){
            case ALL:
                if(processInstance.getState().typeIsFinished()){
                    sendWarnning = true;
                }
                break;
            case SUCCESS:
                if(processInstance.getState().typeIsSuccess()){
                    sendWarnning = true;
                }
                break;
            case FAILURE:
                if(processInstance.getState().typeIsFailure()){
                    sendWarnning = true;
                }
                break;
                default:
        }
        if(!sendWarnning){
            return;
        }
        Alert alert = new Alert();


        String cmdName = getCommandCnName(processInstance.getCommandType());
B
baoliang 已提交
221
        String success = processInstance.getState().typeIsSuccess() ? "success" :"failed";
L
ligang 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        alert.setTitle(cmdName + success);
        ShowType showType = processInstance.getState().typeIsSuccess() ? ShowType.TEXT : ShowType.TABLE;
        alert.setShowType(showType);
        String content = getContentProcessInstance(processInstance, taskInstances);
        alert.setContent(content);
        alert.setAlertType(AlertType.EMAIL);
        alert.setAlertGroupId(processInstance.getWarningGroupId());
        alert.setCreateTime(new Date());
        alert.setReceivers(processInstance.getProcessDefinition().getReceivers());
        alert.setReceiversCc(processInstance.getProcessDefinition().getReceiversCc());

        alertDao.addAlert(alert);
        logger.info("add alert to db , alert: {}", alert.toString());
    }

B
baoliang 已提交
237 238 239
    public void sendProcessTimeoutAlert(ProcessInstance processInstance, ProcessDefinition processDefinition) {
        alertDao.sendProcessTimeoutAlert(processInstance, processDefinition);
    }
L
ligang 已提交
240
}