定时任务优化:handtype_1或者2,如果判断没有出现的情况,则在连接influxdb异常时,不发送报警短信
Showing
1 changed file
with
27 additions
and
20 deletions
@@ -43,12 +43,11 @@ public class SingleTask implements Runnable { | @@ -43,12 +43,11 @@ public class SingleTask implements Runnable { | ||
43 | } | 43 | } |
44 | } | 44 | } |
45 | //获取数据 | 45 | //获取数据 |
46 | - if("app".equals(taskModel.getInfluxdbs())&&taskModel.getDatabase().equals("app_collect_sample")){ | ||
47 | - logger.error("beginqqqqq "+taskModel.getInfluxSql()); | ||
48 | - } | ||
49 | - Map<String, List<List<Map<String, Object>>>> result = getResult(taskModel); | ||
50 | - if("app".equals(taskModel.getInfluxdbs())&&taskModel.getDatabase().equals("app_collect_sample")){ | ||
51 | - logger.error("afterquery"+result); | 46 | + List<Map> resultList= getResultWithSql(taskModel,taskModel.getInfluxSql()); |
47 | + Map<String, List<List<Map<String, Object>>>> result = resultList.get(0); | ||
48 | + Map<String,Boolean> exceptionMap= resultList.get(1); | ||
49 | + if(exceptionMap!=null&&exceptionMap.size()>0){ | ||
50 | + logger.error("定时任务执行时,查询influx出现异常:{}",taskModel); | ||
52 | } | 51 | } |
53 | taskSheduleMapper.updateLastTime(id);//更新时间 | 52 | taskSheduleMapper.updateLastTime(id);//更新时间 |
54 | //按照告警类型进行处理 | 53 | //按照告警类型进行处理 |
@@ -62,8 +61,17 @@ public class SingleTask implements Runnable { | @@ -62,8 +61,17 @@ public class SingleTask implements Runnable { | ||
62 | alarmTypeHandler.handle(taskModel,result); | 61 | alarmTypeHandler.handle(taskModel,result); |
63 | } | 62 | } |
64 | }else{ | 63 | }else{ |
65 | - AlarmTypeHandler alarmTypeHandler = getHandler(taskModel.getAlarmType()); | ||
66 | - alarmTypeHandler.handle(taskModel,result); | 64 | + if(1==taskModel.getAlarmType()||2==taskModel.getAlarmType()){ |
65 | + ///// 未出现想要则告警,有可能存在特殊的情况,比如influx数据库连接异常,造成的数据为空 | ||
66 | + if(exceptionMap==null||exceptionMap.size()<=0){ | ||
67 | + AlarmTypeHandler alarmTypeHandler = getHandler(taskModel.getAlarmType()); | ||
68 | + alarmTypeHandler.handle(taskModel,result); | ||
69 | + } | ||
70 | + }else{ | ||
71 | + AlarmTypeHandler alarmTypeHandler = getHandler(taskModel.getAlarmType()); | ||
72 | + alarmTypeHandler.handle(taskModel,result); | ||
73 | + } | ||
74 | + | ||
67 | } | 75 | } |
68 | 76 | ||
69 | } catch (Exception e) { | 77 | } catch (Exception e) { |
@@ -76,14 +84,6 @@ public class SingleTask implements Runnable { | @@ -76,14 +84,6 @@ public class SingleTask implements Runnable { | ||
76 | return (AlarmTypeHandler) ApplicationUtil.getApplicationContext().getBean("handlerType_" + alarmType); | 84 | return (AlarmTypeHandler) ApplicationUtil.getApplicationContext().getBean("handlerType_" + alarmType); |
77 | } | 85 | } |
78 | 86 | ||
79 | - /** | ||
80 | - * 获取数据 | ||
81 | - * @param taskModel | ||
82 | - * @return | ||
83 | - */ | ||
84 | - public Map<String, List<List<Map<String, Object>>>> getResult(TaskModel taskModel) { | ||
85 | - return getResultWithSql(taskModel,taskModel.getInfluxSql()); | ||
86 | - } | ||
87 | 87 | ||
88 | /** | 88 | /** |
89 | * 获取数据:上个周期 | 89 | * 获取数据:上个周期 |
@@ -95,11 +95,12 @@ public class SingleTask implements Runnable { | @@ -95,11 +95,12 @@ public class SingleTask implements Runnable { | ||
95 | logger.error("SingleTask getResultLastPeriod sql_second is null "); | 95 | logger.error("SingleTask getResultLastPeriod sql_second is null "); |
96 | return null; | 96 | return null; |
97 | } | 97 | } |
98 | - return getResultWithSql(taskModel,taskModel.getInfluxSqlSnd()); | 98 | + |
99 | + return getResultWithSql(taskModel,taskModel.getInfluxSqlSnd()).get(0); | ||
99 | } | 100 | } |
100 | 101 | ||
101 | 102 | ||
102 | - private Map<String, List<List<Map<String, Object>>>> getResultWithSql(TaskModel taskModel,String sql) { | 103 | + private List<Map> getResultWithSql(TaskModel taskModel,String sql) { |
103 | String[] influxdbs = taskModel.getInfluxdbs().split(","); | 104 | String[] influxdbs = taskModel.getInfluxdbs().split(","); |
104 | String[] fields = taskModel.getFields().split(","); | 105 | String[] fields = taskModel.getFields().split(","); |
105 | String[] tags = null; | 106 | String[] tags = null; |
@@ -107,9 +108,12 @@ public class SingleTask implements Runnable { | @@ -107,9 +108,12 @@ public class SingleTask implements Runnable { | ||
107 | tags = taskModel.getTags().split(","); | 108 | tags = taskModel.getTags().split(","); |
108 | } | 109 | } |
109 | Map<String, List<List<Map<String, Object>>>> resultMap = new HashMap<>(); | 110 | Map<String, List<List<Map<String, Object>>>> resultMap = new HashMap<>(); |
110 | - | 111 | + Map<String,Boolean> exceptionMap=new HashMap<String,Boolean>(); |
111 | for (String influxdb : influxdbs) { | 112 | for (String influxdb : influxdbs) { |
112 | QueryResult queryResult = ((CommonQuery)ApplicationUtil.getBean(CommonQuery.class)).query(influxdb, sql, taskModel.getDatabase()); | 113 | QueryResult queryResult = ((CommonQuery)ApplicationUtil.getBean(CommonQuery.class)).query(influxdb, sql, taskModel.getDatabase()); |
114 | + if(queryResult==null){ | ||
115 | + exceptionMap.put(influxdb,true); // result为null,说明查询的时候发生了异常,比如数据库连接异常,sql语句异常 | ||
116 | + } | ||
113 | List<QueryResult.Series> seriesList = QueryResultUtil.getSeries(queryResult); | 117 | List<QueryResult.Series> seriesList = QueryResultUtil.getSeries(queryResult); |
114 | if (seriesList == null) { | 118 | if (seriesList == null) { |
115 | continue; | 119 | continue; |
@@ -134,7 +138,10 @@ public class SingleTask implements Runnable { | @@ -134,7 +138,10 @@ public class SingleTask implements Runnable { | ||
134 | } | 138 | } |
135 | resultMap.put(influxdb, seriesResultList); | 139 | resultMap.put(influxdb, seriesResultList); |
136 | } | 140 | } |
137 | - return resultMap; | 141 | + List<Map> rtn=new ArrayList<Map>(); |
142 | + rtn.add(resultMap); | ||
143 | + rtn.add(exceptionMap); | ||
144 | + return rtn; | ||
138 | } | 145 | } |
139 | 146 | ||
140 | 147 |
-
Please register or login to post a comment