作业子任务执行流程

作业子任务执行流程.jpg

代码

JobWorkInfoController,JobWorkInfoService,JobWorkInfoMapper,AdminBizImpl,
JobWorkLogController,JobWorkLogService,JobConstant

源码改动

AdminBizImpl
重写

  1. private JobWrapper<String> callback(HandleCallbackParam handleCallbackParam) {
  2. // valid log item
  3. //当前执行任务的日志id
  4. JobLog log = xxlJobLogDao.load(handleCallbackParam.getLogId());
  5. //当前执行成功的任务
  6. JobInfo xxlJobInfo = xxlJobInfoDao.loadById(log.getJobId());
  7. int logId = log.getId();
  8. if (log == null) {
  9. return new JobWrapper<String>(JobWrapper.FAIL_CODE, "log item not found.");
  10. }
  11. if (log.getHandleCode() != 1) {
  12. return new JobWrapper<String>(JobWrapper.FAIL_CODE, "log repeate callback."); // avoid repeat callback, trigger child job etc
  13. }
  14. // trigger success, to trigger child job
  15. String callbackMsg = null;
  16. //执行成功的处理
  17. if (IJobHandler.SUCCESS.getCode() == handleCallbackParam.getExecuteResult().getCode()) {
  18. if (xxlJobInfo!=null) {
  19. //如果是作业任务
  20. if (xxlJobInfo.getClassify().equals(3)){
  21. //更新根任务日志运行状态为“运行中”
  22. log.setHandleCode(JobConstant.JOB_ROOT_LOG_RUNNING);
  23. xxlJobLogDao.updateHandleInfo(log);
  24. //直接触发第一个子任务
  25. String[] ids = xxlJobInfo.getChildJobId().split(",");
  26. Integer childJobId = Integer.parseInt(ids[0]);
  27. String executorParam = null;
  28. //获取子任务执行所需的参数
  29. executorParam = getChildExecutorParam(childJobId,logId);
  30. JobTriggerPoolHelper.trigger(childJobId,logId, TriggerTypeEnum.PARENT, -1, null, executorParam);
  31. // JobWrapper<String> triggerChildResult = JobWrapper.SUCCESS;
  32. }else if (log.getRootLogId()!=null){
  33. //如果是作业子任务,根据rootLogId找到所处任务链,得知下一步触发的任务id
  34. Integer childJobId = getNextJobId(log);
  35. //下一步触发的任务不为空才触发
  36. if (childJobId!=null){
  37. String executorParam = null;
  38. //获取子任务执行所需的参数
  39. executorParam = getChildExecutorParam(childJobId,log.getRootLogId());
  40. JobTriggerPoolHelper.trigger(childJobId,log.getRootLogId(), TriggerTypeEnum.PARENT, -1, null, executorParam);
  41. }else{
  42. //进行所有日志的总结
  43. JobLog rootJobLog = xxlJobLogDao.load(log.getRootLogId());
  44. rootJobLog.setHandleCode(JobConstant.JOB_ROOT_LOG_SUCCESS);
  45. xxlJobLogDao.updateHandleInfo(rootJobLog);
  46. }
  47. // JobWrapper<String> triggerChildResult = JobWrapper.SUCCESS;
  48. }
  49. callbackMsg = "<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>"+ I18nUtil.getString("jobconf_trigger_child_run") +"<<<<<<<<<<< </span><br>";
  50. }
  51. //失败/超时的处理
  52. }else if (IJobHandler.FAIL.getCode() == handleCallbackParam.getExecuteResult().getCode()
  53. ||IJobHandler.FAIL_TIMEOUT.getCode() == handleCallbackParam.getExecuteResult().getCode()
  54. ){
  55. //当前执行的任务
  56. if (xxlJobInfo!=null){
  57. if (log.getRootLogId()!=null){
  58. //进行所有日志的总结
  59. JobLog rootJobLog = xxlJobLogDao.load(log.getRootLogId());
  60. Integer childJobId = getNextJobId(log);
  61. if (childJobId==null){
  62. rootJobLog.setHandleCode(JobConstant.JOB_ROOT_LOG_FAIL);
  63. }else {
  64. rootJobLog.setHandleCode(JobConstant.JOB_ROOT_LOG_INTERRUPT);
  65. //插入后续子任务的日志
  66. buildFollowChildLogs(log);
  67. }
  68. xxlJobLogDao.updateHandleInfo(rootJobLog);
  69. }
  70. }
  71. callbackMsg = "<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>"+ I18nUtil.getString("jobconf_trigger_child_run") +"<<<<<<<<<<< </span><br>";
  72. }
  73. // handle msg
  74. StringBuffer handleMsg = new StringBuffer();
  75. if (log.getHandleMsg()!=null) {
  76. handleMsg.append(log.getHandleMsg()).append("<br>");
  77. }
  78. if (handleCallbackParam.getExecuteResult().getMsg() != null) {
  79. handleMsg.append(handleCallbackParam.getExecuteResult().getMsg());
  80. }
  81. if (callbackMsg != null) {
  82. handleMsg.append(callbackMsg);
  83. }
  84. // success, save log
  85. log.setHandleTime(new Date());
  86. if(xxlJobInfo!=null && !xxlJobInfo.getClassify().equals(3)){
  87. log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
  88. }
  89. log.setHandleMsg(handleMsg.toString());
  90. xxlJobLogDao.updateHandleInfo(log);
  91. return JobWrapper.SUCCESS;
  92. }

新写

//获取子任务的执行参数    
private String getChildExecutorParam(Integer jobId,int logId) {
        String executorParam = null;
        //查到根日志
        JobLog rootlog = xxlJobLogDao.load(logId);
        String executorParams = rootlog.getExecutorParam();
        try{
            if (StringUtils.isNotEmpty(executorParams)){
                List<JobScriptParamDto> jobScriptParamDtos = JsonUtil.json2List(executorParams, JobScriptParamDto.class);
                for (JobScriptParamDto jobScriptParamDto : jobScriptParamDtos) {
                    if (jobScriptParamDto.getJobId().equals(jobId) &&
                            jobScriptParamDto.getScriptParamObj()!=null){
                        executorParam = JsonUtil.object2Json(jobScriptParamDto.getScriptParamObj());
                        return executorParam;
                    }
                }
            }

        }catch (Exception e){
//            e.printStackTrace();
        }
        return executorParam;
    }

    //获取下一个子任务
    private Integer getNextJobId(JobLog log) {
        //当前任务id
        Integer jobId = log.getJobId();
        //根据当前日志找到根日志id;
        Integer rootLogId = log.getRootLogId();
        //查到根日志
        JobLog rootlog = xxlJobLogDao.load(rootLogId);
        //找到根任务(作业任务)
        JobInfo jobInfo = xxlJobInfoDao.loadById(rootlog.getJobId());
        String[] ids = jobInfo.getChildJobId().split(",");
        Integer nextJobId = null;
        for (int i=0;i<ids.length;i++){
            if (i==ids.length-1){
                break;
            }else if (jobId.equals(Integer.parseInt(ids[i]))){
                nextJobId = Integer.parseInt(ids[i+1]);
                break;
            }
        }
        return nextJobId;
    }

    //创建子任务执行日志
    private void buildFollowChildLogs(JobLog log) {
        //当前任务id
        Integer jobId = log.getJobId();
        //根据当前日志找到根日志id;
        Integer rootLogId = log.getRootLogId();
        //查到根日志
        JobLog rootlog = xxlJobLogDao.load(rootLogId);
        //找到根任务(作业任务)
        JobInfo jobInfo = xxlJobInfoDao.loadById(rootlog.getJobId());
        String[] ids = jobInfo.getChildJobId().split(",");
        int index = -1;
        for (int i=0;i<ids.length;i++){
            if (jobId.equals(Integer.parseInt(ids[i].toString()))){
                index = i;
                //插入日志
//                JobLog jobLog = new JobLog();
//                jobLog.setRootLogId(rootLogId);
//                jobLog.setJobGroup(-1);
//                jobLog.setJobId(Integer.parseInt(ids[i].toString()));
//                jobLog.setExecutorFailRetryCount(0);
//                jobLog.setTriggerCode(-2);
//                jobLog.setHandleCode(-2);
//                xxlJobLogDao.save(jobLog);
            }else if (index!=-1 && i>index){
                //插入日志
                JobLog jobLog = new JobLog();
                jobLog.setRootLogId(rootLogId);
                jobLog.setJobGroup(-1);
                jobLog.setJobId(Integer.parseInt(ids[i].toString()));
                jobLog.setExecutorFailRetryCount(0);
                jobLog.setTriggerCode(-2);
                jobLog.setHandleCode(-2);
                xxlJobLogDao.save(jobLog);
            }
        }
    }

表结构

CREATE TABLE `h_qrtz_trigger_work_info` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL COMMENT '作业名称',
    `type` varchar(50) NOT NULL COMMENT '作业分类',
    `work_cron` varchar(50) DEFAULT NULL COMMENT 'cron',
    `job_id` int(11) DEFAULT NULL COMMENT '作业任务id',
    `job_code` varchar(50) DEFAULT NULL COMMENT 'job_code',
    `job_ids` varchar(255) DEFAULT NULL COMMENT '任务ids',
    `job_num` int(11) DEFAULT NULL COMMENT '关联任务数',
    `description` varchar(255) DEFAULT NULL COMMENT '描述',
    `create_time` datetime DEFAULT NULL COMMENT '创建时间',
    `is_task` tinyint(10) NOT NULL DEFAULT '0' COMMENT '是否定时任务(0否:1是)',
    `state` tinyint(10) NOT NULL DEFAULT '0' COMMENT '是否启动(0否:1是)',
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=369 DEFAULT CHARSET=utf8 COMMENT='作业表';

h_qrtz_trigger_log

新增

`root_log_id` int(11) DEFAULT NULL COMMENT '根任务日志id',//作业中心用的

其他

每次生成子任务执行日志的时候,存入root_log_id,使得子任务日志关联上作业任务日志,作业任务日志中放着子任务的配置情况和索引子任务的执行参数。