JobExecuteResultHandler.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.primeton.datainsight.task;
  2. import com.primeton.datainsight.bo.WarnMessage;
  3. import com.primeton.datainsight.service.WarnService;
  4. import com.primeton.datainsight.utils.Constance;
  5. import com.primeton.datainsight.utils.TimeUtils;
  6. import org.apache.commons.exec.DefaultExecuteResultHandler;
  7. import org.apache.commons.exec.ExecuteException;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import java.util.Date;
  11. import java.util.concurrent.atomic.AtomicInteger;
  12. /**
  13. *
  14. * 任务超时告警监控
  15. *
  16. *
  17. * <pre>
  18. *
  19. * Created by zhaopx.
  20. * User: zhaopx
  21. * Date: 2019/4/11
  22. * Time: 11:04
  23. *
  24. * </pre>
  25. *
  26. * @author zhaopx
  27. */
  28. public class JobExecuteResultHandler extends DefaultExecuteResultHandler {
  29. private static Logger LOG = LoggerFactory.getLogger(JobExecuteResultHandler.class);
  30. /**
  31. * 开始时间
  32. */
  33. public final long startTime = System.currentTimeMillis();
  34. /**
  35. * 预警服务
  36. */
  37. final WarnService warnService;
  38. /**
  39. * 是否开启预警
  40. */
  41. final boolean enable;
  42. /**
  43. * 任务一小时预警
  44. */
  45. final long warnTimeout;
  46. /**
  47. * 1 小时候后每10分钟预警一次
  48. */
  49. final long warnTimeoutInterval;
  50. /**
  51. * 预警次数
  52. */
  53. final AtomicInteger warnCount = new AtomicInteger(0);
  54. /**
  55. * 任务名称,作为任务日志显示用
  56. */
  57. String taskName;
  58. public JobExecuteResultHandler(WarnService warnService) {
  59. this(warnService, warnService.isWarnEnable(), warnService.getWarnJobTimeout(), warnService.getWarnJobPeriod());
  60. }
  61. /**
  62. * 任务执行多长时间后未完成预警,如果预警后每隔多长时间继续预警
  63. * @param warnTimeout
  64. * @param warnTimeoutInterval
  65. */
  66. public JobExecuteResultHandler(WarnService warnService,
  67. boolean enable,
  68. long warnTimeout,
  69. long warnTimeoutInterval) {
  70. this.warnService = warnService;
  71. this.enable = enable;
  72. this.warnTimeout = warnTimeout > 0 ? warnTimeout : -1L;
  73. this.warnTimeoutInterval = warnTimeoutInterval > 0 ? warnTimeoutInterval : -1L;
  74. }
  75. @Override
  76. public void onProcessComplete(int exitValue) {
  77. // 退出码非 0 则认为任务不成功
  78. if(exitValue != 0){
  79. throw new IllegalStateException("进程非正常退出。exitValue: " + exitValue);
  80. }
  81. super.onProcessComplete(exitValue);
  82. }
  83. @Override
  84. public void onProcessFailed(ExecuteException e) {
  85. super.onProcessFailed(e);
  86. if(warnService.isWarnEnable()) {
  87. WarnMessage warnMessage = new WarnMessage();
  88. warnMessage.setWarnCatalog(Constance.WARN_CATALOG_TASK_EXE_FAILED);
  89. warnMessage.setWarnType("task");
  90. warnMessage.setWarnLevel("HIGH");
  91. warnMessage.setWarnStart(new Date());
  92. warnMessage.setWarnSubtext("任务执行失败");
  93. warnMessage.setWarnContent(taskName + "执行失败。REASON: " + e.getMessage());
  94. warnService.warn(warnMessage);
  95. }
  96. }
  97. @Override
  98. public void waitFor() throws InterruptedException {
  99. while (!hasResult()) {
  100. super.waitFor(1000);
  101. long l = System.currentTimeMillis();
  102. // 任务如果是开启的,就用任务的,否则使用全局的
  103. boolean enable = this.enable;
  104. // 如果没有开始告警,则不会查询一次 getTaskRunningTimeout(),提高性能
  105. long warnTimeout = enable && this.warnTimeout > 0 ? this.warnTimeout : 3600000;
  106. if(enable && (l - startTime) >= warnTimeout) {
  107. // 超时了,开始预警
  108. long warnInterval = this.warnTimeoutInterval > 0 ? this.warnTimeoutInterval : 600000;
  109. long timeout = warnCount.get() * warnInterval + warnTimeout;
  110. if(l - startTime >= timeout) {
  111. warnCount.incrementAndGet();
  112. LOG.warn("任务执行超时:" + TimeUtils.getTimeString(timeout));
  113. WarnMessage warnMessage = new WarnMessage();
  114. warnMessage.setWarnCatalog(Constance.WARN_CATALOG_TASK_TIMEOUT);
  115. warnMessage.setWarnType("task");
  116. warnMessage.setWarnLevel("HIGH");
  117. warnMessage.setWarnStart(new Date());
  118. warnMessage.setWarnSubtext("任务执行超时");
  119. warnMessage.setWarnContent(taskName + " 执行超时, 超时:" + TimeUtils.getTimeString(timeout));
  120. warnService.warn(warnMessage);
  121. }
  122. }
  123. }
  124. }
  125. public void setTaskName(String taskName) {
  126. this.taskName = taskName;
  127. }
  128. }