修改JobThread捕获Error错误不更新JobLog

This commit is contained in:
anthow 2017-11-10 11:43:36 +08:00
parent be926f81aa
commit abb4474fbb
1 changed files with 84 additions and 76 deletions

View File

@ -91,86 +91,94 @@ public class JobThread extends Thread{
@Override @Override
public void run() { public void run() {
while(!toStop){
running = false;
idleTimes++;
try {
// to check toStop signal, we need cycle, so wo cannot use queue.take(), instand of poll(timeout)
TriggerParam triggerParam = triggerQueue.poll(3L, TimeUnit.SECONDS);
if (triggerParam!=null) {
running = true;
idleTimes = 0;
triggerLogIdSet.remove(triggerParam.getLogId());
// parse param while(!toStop){
String[] handlerParams = (triggerParam.getExecutorParams()!=null && triggerParam.getExecutorParams().trim().length()>0) running = false;
? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(",")).toArray()) : null; idleTimes++;
// handle job
ReturnT<String> executeResult = null;
TriggerParam triggerParam = null;
try {
// to check toStop signal, we need cycle, so wo cannot use queue.take(), instand of poll(timeout)
triggerParam = triggerQueue.poll(3L, TimeUnit.SECONDS);
if (triggerParam!=null) {
running = true;
idleTimes = 0;
triggerLogIdSet.remove(triggerParam.getLogId());
// handle job // parse param
ReturnT<String> executeResult = null; String[] handlerParams = (triggerParam.getExecutorParams()!=null && triggerParam.getExecutorParams().trim().length()>0)
try { ? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(" ")).toArray()) : null;
// log filename: yyyy-MM-dd/9999.log
String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
XxlJobFileAppender.contextHolder.set(logFileName);
ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal()));
XxlJobLogger.log("<br>----------- xxl-job job execute start -----------<br>----------- Params:" + Arrays.toString(handlerParams));
executeResult = handler.execute(handlerParams); try {
if (executeResult == null) { // log filename: yyyy-MM-dd/9999.log
executeResult = ReturnT.FAIL; String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
}
XxlJobLogger.log("<br>----------- xxl-job job execute end(finish) -----------<br>----------- ReturnT:" + executeResult); XxlJobFileAppender.contextHolder.set(logFileName);
} catch (Exception e) { ShardingUtil.setShardingVo(new ShardingUtil.ShardingVO(triggerParam.getBroadcastIndex(), triggerParam.getBroadcastTotal()));
if (toStop) { XxlJobLogger.log("<br>----------- xxl-job job execute start -----------<br>----------- Params:" + Arrays.toString(handlerParams));
XxlJobLogger.log("<br>----------- JobThread toStop, stopReason:" + stopReason);
}
StringWriter stringWriter = new StringWriter(); executeResult = handler.execute(handlerParams);
e.printStackTrace(new PrintWriter(stringWriter)); if (executeResult == null) {
String errorMsg = stringWriter.toString(); executeResult = ReturnT.FAIL;
executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, errorMsg); }
XxlJobLogger.log("<br>----------- JobThread Exception:" + errorMsg + "<br>----------- xxl-job job execute end(error) -----------"); XxlJobLogger.log("<br>----------- xxl-job job execute end(finish) -----------<br>----------- ReturnT:" + executeResult);
} } catch (Exception e) {
if (toStop) {
XxlJobLogger.log("<br>----------- JobThread toStop, stopReason:" + stopReason);
}
// callback handler info StringWriter stringWriter = new StringWriter();
if (!toStop) { e.printStackTrace(new PrintWriter(stringWriter));
// commonm String errorMsg = stringWriter.toString();
TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), executeResult)); executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, errorMsg);
} else {
// is killed
ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [业务运行中,被强制终止]");
TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), stopResult));
}
} else {
if (idleTimes > 30) {
XxlJobExecutor.removeJobThread(jobId, "excutor idel times over limit.");
}
}
} catch (Throwable e) {
if (toStop) {
XxlJobLogger.log("<br>----------- xxl-job toStop, stopReason:" + stopReason);
}
StringWriter stringWriter = new StringWriter(); XxlJobLogger.log("<br>----------- JobThread Exception:" + errorMsg + "<br>----------- xxl-job job execute end(error) -----------");
e.printStackTrace(new PrintWriter(stringWriter)); }
String errorMsg = stringWriter.toString();
XxlJobLogger.log("----------- xxl-job JobThread Exception:" + errorMsg);
}
}
// callback trigger request in queue } else {
while(triggerQueue !=null && triggerQueue.size()>0){ if (idleTimes > 30) {
TriggerParam triggerParam = triggerQueue.poll(); XxlJobExecutor.removeJobThread(jobId, "excutor idel times over limit.");
if (triggerParam!=null) { }
// is killed }
ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [任务尚未执行,在调度队列中被终止]"); } catch (Throwable e) {
TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), stopResult)); if (toStop) {
} XxlJobLogger.log("<br>----------- xxl-job toStop, stopReason:" + stopReason);
} }
logger.info(">>>>>>>>>>> xxl-job JobThread stoped, hashCode:{}", Thread.currentThread()); StringWriter stringWriter = new StringWriter();
} e.printStackTrace(new PrintWriter(stringWriter));
String errorMsg = stringWriter.toString();
executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, errorMsg);
XxlJobLogger.log("----------- xxl-job JobThread Exception:" + errorMsg);
} finally {
if(triggerParam != null) {
// callback handler info
if (!toStop) {
// commonm
TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), executeResult));
} else {
// is killed
ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [业务运行中,被强制终止]");
TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), stopResult));
}
}
}
}
// callback trigger request in queue
while(triggerQueue !=null && triggerQueue.size()>0){
TriggerParam triggerParam = triggerQueue.poll();
if (triggerParam!=null) {
// is killed
ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [任务尚未执行,在调度队列中被终止]");
TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), stopResult));
}
}
logger.info(">>>>>>>>>>>> xxl-job JobThread stoped, hashCode:{}", Thread.currentThread());
}
} }