Authored by qinchao

定时任务优化:handtype_1或者2,如果判断没有出现的情况,则在连接influxdb异常时,不发送报警短信

... ... @@ -43,12 +43,11 @@ public class SingleTask implements Runnable {
}
}
//获取数据
if("app".equals(taskModel.getInfluxdbs())&&taskModel.getDatabase().equals("app_collect_sample")){
logger.error("beginqqqqq "+taskModel.getInfluxSql());
}
Map<String, List<List<Map<String, Object>>>> result = getResult(taskModel);
if("app".equals(taskModel.getInfluxdbs())&&taskModel.getDatabase().equals("app_collect_sample")){
logger.error("afterquery"+result);
List<Map> resultList= getResultWithSql(taskModel,taskModel.getInfluxSql());
Map<String, List<List<Map<String, Object>>>> result = resultList.get(0);
Map<String,Boolean> exceptionMap= resultList.get(1);
if(exceptionMap!=null&&exceptionMap.size()>0){
logger.error("定时任务执行时,查询influx出现异常:{}",taskModel);
}
taskSheduleMapper.updateLastTime(id);//更新时间
//按照告警类型进行处理
... ... @@ -62,8 +61,17 @@ public class SingleTask implements Runnable {
alarmTypeHandler.handle(taskModel,result);
}
}else{
AlarmTypeHandler alarmTypeHandler = getHandler(taskModel.getAlarmType());
alarmTypeHandler.handle(taskModel,result);
if(1==taskModel.getAlarmType()||2==taskModel.getAlarmType()){
///// 未出现想要则告警,有可能存在特殊的情况,比如influx数据库连接异常,造成的数据为空
if(exceptionMap==null||exceptionMap.size()<=0){
AlarmTypeHandler alarmTypeHandler = getHandler(taskModel.getAlarmType());
alarmTypeHandler.handle(taskModel,result);
}
}else{
AlarmTypeHandler alarmTypeHandler = getHandler(taskModel.getAlarmType());
alarmTypeHandler.handle(taskModel,result);
}
}
} catch (Exception e) {
... ... @@ -76,14 +84,6 @@ public class SingleTask implements Runnable {
return (AlarmTypeHandler) ApplicationUtil.getApplicationContext().getBean("handlerType_" + alarmType);
}
/**
* 获取数据
* @param taskModel
* @return
*/
public Map<String, List<List<Map<String, Object>>>> getResult(TaskModel taskModel) {
return getResultWithSql(taskModel,taskModel.getInfluxSql());
}
/**
* 获取数据:上个周期
... ... @@ -95,11 +95,12 @@ public class SingleTask implements Runnable {
logger.error("SingleTask getResultLastPeriod sql_second is null ");
return null;
}
return getResultWithSql(taskModel,taskModel.getInfluxSqlSnd());
return getResultWithSql(taskModel,taskModel.getInfluxSqlSnd()).get(0);
}
private Map<String, List<List<Map<String, Object>>>> getResultWithSql(TaskModel taskModel,String sql) {
private List<Map> getResultWithSql(TaskModel taskModel,String sql) {
String[] influxdbs = taskModel.getInfluxdbs().split(",");
String[] fields = taskModel.getFields().split(",");
String[] tags = null;
... ... @@ -107,9 +108,12 @@ public class SingleTask implements Runnable {
tags = taskModel.getTags().split(",");
}
Map<String, List<List<Map<String, Object>>>> resultMap = new HashMap<>();
Map<String,Boolean> exceptionMap=new HashMap<String,Boolean>();
for (String influxdb : influxdbs) {
QueryResult queryResult = ((CommonQuery)ApplicationUtil.getBean(CommonQuery.class)).query(influxdb, sql, taskModel.getDatabase());
if(queryResult==null){
exceptionMap.put(influxdb,true); // result为null,说明查询的时候发生了异常,比如数据库连接异常,sql语句异常
}
List<QueryResult.Series> seriesList = QueryResultUtil.getSeries(queryResult);
if (seriesList == null) {
continue;
... ... @@ -134,7 +138,10 @@ public class SingleTask implements Runnable {
}
resultMap.put(influxdb, seriesResultList);
}
return resultMap;
List<Map> rtn=new ArrayList<Map>();
rtn.add(resultMap);
rtn.add(exceptionMap);
return rtn;
}
... ...