Search in sources :

Example 1 with XxlJobGroup

use of com.xxl.job.admin.core.model.XxlJobGroup in project xxl-job by xuxueli.

the class JobRegistryMonitorHelper method start.

public void start() {
    registryThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!toStop) {
                try {
                    // auto registry group
                    List<XxlJobGroup> groupList = XxlJobDynamicScheduler.xxlJobGroupDao.findByAddressType(0);
                    if (CollectionUtils.isNotEmpty(groupList)) {
                        // remove dead address (admin/executor)
                        XxlJobDynamicScheduler.xxlJobRegistryDao.removeDead(RegistryConfig.DEAD_TIMEOUT);
                        // fresh online address (admin/executor)
                        HashMap<String, List<String>> appAddressMap = new HashMap<String, List<String>>();
                        List<XxlJobRegistry> list = XxlJobDynamicScheduler.xxlJobRegistryDao.findAll(RegistryConfig.DEAD_TIMEOUT);
                        if (list != null) {
                            for (XxlJobRegistry item : list) {
                                if (RegistryConfig.RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) {
                                    String appName = item.getRegistryKey();
                                    List<String> registryList = appAddressMap.get(appName);
                                    if (registryList == null) {
                                        registryList = new ArrayList<String>();
                                    }
                                    if (!registryList.contains(item.getRegistryValue())) {
                                        registryList.add(item.getRegistryValue());
                                    }
                                    appAddressMap.put(appName, registryList);
                                }
                            }
                        }
                        // fresh group address
                        for (XxlJobGroup group : groupList) {
                            List<String> registryList = appAddressMap.get(group.getAppName());
                            String addressListStr = null;
                            if (CollectionUtils.isNotEmpty(registryList)) {
                                Collections.sort(registryList);
                                addressListStr = StringUtils.join(registryList, ",");
                            }
                            group.setAddressList(addressListStr);
                            XxlJobDynamicScheduler.xxlJobGroupDao.update(group);
                        }
                    }
                } catch (Exception e) {
                    logger.error("job registry instance error:{}", e);
                }
                try {
                    TimeUnit.SECONDS.sleep(RegistryConfig.BEAT_TIMEOUT);
                } catch (InterruptedException e) {
                    logger.error("job registry instance error:{}", e);
                }
            }
        }
    });
    registryThread.setDaemon(true);
    registryThread.start();
}
Also used : XxlJobGroup(com.xxl.job.admin.core.model.XxlJobGroup) HashMap(java.util.HashMap) XxlJobRegistry(com.xxl.job.admin.core.model.XxlJobRegistry) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with XxlJobGroup

use of com.xxl.job.admin.core.model.XxlJobGroup in project xxl-job by xuxueli.

the class JobFailMonitorHelper method failAlarm.

/**
 * fail alarm
 *
 * @param jobLog
 */
private void failAlarm(XxlJobLog jobLog) {
    // send monitor email
    XxlJobInfo info = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(jobLog.getJobId());
    if (info != null && info.getAlarmEmail() != null && info.getAlarmEmail().trim().length() > 0) {
        Set<String> emailSet = new HashSet<String>(Arrays.asList(info.getAlarmEmail().split(",")));
        for (String email : emailSet) {
            XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(Integer.valueOf(info.getJobGroup()));
            String title = I18nUtil.getString("jobconf_monitor");
            String content = MessageFormat.format(mailBodyTemplate, group != null ? group.getTitle() : "null", info.getId(), info.getJobDesc());
            MailUtil.sendMail(email, title, content);
        }
    }
// TODO, custom alarm strategy, such as sms
}
Also used : XxlJobGroup(com.xxl.job.admin.core.model.XxlJobGroup) XxlJobInfo(com.xxl.job.admin.core.model.XxlJobInfo)

Example 3 with XxlJobGroup

use of com.xxl.job.admin.core.model.XxlJobGroup in project xxl-job by xuxueli.

the class XxlJobTrigger method trigger.

/**
 * trigger job
 *
 * @param jobId
 */
public static void trigger(int jobId) {
    // load data
    // job info
    XxlJobInfo jobInfo = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(jobId);
    if (jobInfo == null) {
        logger.warn(">>>>>>>>>>>> trigger fail, jobId invalid,jobId={}", jobId);
        return;
    }
    // group info
    XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(jobInfo.getJobGroup());
    // block strategy
    ExecutorBlockStrategyEnum blockStrategy = ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), ExecutorBlockStrategyEnum.SERIAL_EXECUTION);
    // fail strategy
    ExecutorFailStrategyEnum failStrategy = ExecutorFailStrategyEnum.match(jobInfo.getExecutorFailStrategy(), ExecutorFailStrategyEnum.FAIL_ALARM);
    // route strategy
    ExecutorRouteStrategyEnum executorRouteStrategyEnum = ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null);
    ArrayList<String> addressList = (ArrayList<String>) group.getRegistryList();
    // broadcast
    if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST == executorRouteStrategyEnum && CollectionUtils.isNotEmpty(addressList)) {
        for (int i = 0; i < addressList.size(); i++) {
            String address = addressList.get(i);
            // 1、save log-id
            XxlJobLog jobLog = new XxlJobLog();
            jobLog.setJobGroup(jobInfo.getJobGroup());
            jobLog.setJobId(jobInfo.getId());
            XxlJobDynamicScheduler.xxlJobLogDao.save(jobLog);
            logger.debug(">>>>>>>>>>> xxl-job trigger start, jobId:{}", jobLog.getId());
            // 2、prepare trigger-info
            // jobLog.setExecutorAddress(executorAddress);
            jobLog.setGlueType(jobInfo.getGlueType());
            jobLog.setExecutorHandler(jobInfo.getExecutorHandler());
            jobLog.setExecutorParam(jobInfo.getExecutorParam());
            jobLog.setTriggerTime(new Date());
            ReturnT<String> triggerResult = new ReturnT<String>(null);
            StringBuffer triggerMsgSb = new StringBuffer();
            triggerMsgSb.append(I18nUtil.getString("jobconf_trigger_admin_adress")).append(":").append(IpUtil.getIp());
            triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regtype")).append(":").append((group.getAddressType() == 0) ? I18nUtil.getString("jobgroup_field_addressType_0") : I18nUtil.getString("jobgroup_field_addressType_1"));
            triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regaddress")).append(":").append(group.getRegistryList());
            // update01
            triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorRouteStrategy")).append(":").append(executorRouteStrategyEnum.getTitle()).append("(" + i + "/" + addressList.size() + ")");
            triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorBlockStrategy")).append(":").append(blockStrategy.getTitle());
            triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorFailStrategy")).append(":").append(failStrategy.getTitle());
            // 3、trigger-valid
            if (triggerResult.getCode() == ReturnT.SUCCESS_CODE && CollectionUtils.isEmpty(addressList)) {
                triggerResult.setCode(ReturnT.FAIL_CODE);
                triggerMsgSb.append("<br>----------------------<br>").append(I18nUtil.getString("jobconf_trigger_address_empty"));
            }
            if (triggerResult.getCode() == ReturnT.SUCCESS_CODE) {
                // 4.1、trigger-param
                TriggerParam triggerParam = new TriggerParam();
                triggerParam.setJobId(jobInfo.getId());
                triggerParam.setExecutorHandler(jobInfo.getExecutorHandler());
                triggerParam.setExecutorParams(jobInfo.getExecutorParam());
                triggerParam.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
                triggerParam.setLogId(jobLog.getId());
                triggerParam.setLogDateTim(jobLog.getTriggerTime().getTime());
                triggerParam.setGlueType(jobInfo.getGlueType());
                triggerParam.setGlueSource(jobInfo.getGlueSource());
                triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
                triggerParam.setBroadcastIndex(i);
                // update02
                triggerParam.setBroadcastTotal(addressList.size());
                // 4.2、trigger-run (route run / trigger remote executor)
                // update03
                triggerResult = runExecutor(triggerParam, address);
                triggerMsgSb.append("<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>" + I18nUtil.getString("jobconf_trigger_run") + "<<<<<<<<<<< </span><br>").append(triggerResult.getMsg());
                // 4.3、trigger (fail retry)
                if (triggerResult.getCode() != ReturnT.SUCCESS_CODE && failStrategy == ExecutorFailStrategyEnum.FAIL_RETRY) {
                    // update04
                    triggerResult = runExecutor(triggerParam, address);
                    triggerMsgSb.append("<br><br><span style=\"color:#F39C12;\" > >>>>>>>>>>>" + I18nUtil.getString("jobconf_trigger_fail_retry") + "<<<<<<<<<<< </span><br>").append(triggerResult.getMsg());
                }
            }
            // 5、save trigger-info
            jobLog.setExecutorAddress(triggerResult.getContent());
            jobLog.setTriggerCode(triggerResult.getCode());
            jobLog.setTriggerMsg(triggerMsgSb.toString());
            XxlJobDynamicScheduler.xxlJobLogDao.updateTriggerInfo(jobLog);
            // 6、monitor trigger
            JobFailMonitorHelper.monitor(jobLog.getId());
            logger.debug(">>>>>>>>>>> xxl-job trigger end, jobId:{}", jobLog.getId());
        }
    } else {
        // 1、save log-id
        XxlJobLog jobLog = new XxlJobLog();
        jobLog.setJobGroup(jobInfo.getJobGroup());
        jobLog.setJobId(jobInfo.getId());
        XxlJobDynamicScheduler.xxlJobLogDao.save(jobLog);
        logger.debug(">>>>>>>>>>> xxl-job trigger start, jobId:{}", jobLog.getId());
        // 2、prepare trigger-info
        // jobLog.setExecutorAddress(executorAddress);
        jobLog.setGlueType(jobInfo.getGlueType());
        jobLog.setExecutorHandler(jobInfo.getExecutorHandler());
        jobLog.setExecutorParam(jobInfo.getExecutorParam());
        jobLog.setTriggerTime(new Date());
        ReturnT<String> triggerResult = new ReturnT<String>(null);
        StringBuffer triggerMsgSb = new StringBuffer();
        triggerMsgSb.append(I18nUtil.getString("jobconf_trigger_admin_adress")).append(":").append(IpUtil.getIp());
        triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regtype")).append(":").append((group.getAddressType() == 0) ? I18nUtil.getString("jobgroup_field_addressType_0") : I18nUtil.getString("jobgroup_field_addressType_1"));
        triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regaddress")).append(":").append(group.getRegistryList());
        triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorRouteStrategy")).append(":").append(executorRouteStrategyEnum.getTitle());
        triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorBlockStrategy")).append(":").append(blockStrategy.getTitle());
        triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorFailStrategy")).append(":").append(failStrategy.getTitle());
        // 3、trigger-valid
        if (triggerResult.getCode() == ReturnT.SUCCESS_CODE && CollectionUtils.isEmpty(addressList)) {
            triggerResult.setCode(ReturnT.FAIL_CODE);
            triggerMsgSb.append("<br>----------------------<br>").append(I18nUtil.getString("jobconf_trigger_address_empty"));
        }
        if (triggerResult.getCode() == ReturnT.SUCCESS_CODE) {
            // 4.1、trigger-param
            TriggerParam triggerParam = new TriggerParam();
            triggerParam.setJobId(jobInfo.getId());
            triggerParam.setExecutorHandler(jobInfo.getExecutorHandler());
            triggerParam.setExecutorParams(jobInfo.getExecutorParam());
            triggerParam.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
            triggerParam.setLogId(jobLog.getId());
            triggerParam.setLogDateTim(jobLog.getTriggerTime().getTime());
            triggerParam.setGlueType(jobInfo.getGlueType());
            triggerParam.setGlueSource(jobInfo.getGlueSource());
            triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
            triggerParam.setBroadcastIndex(0);
            triggerParam.setBroadcastTotal(1);
            // 4.2、trigger-run (route run / trigger remote executor)
            triggerResult = executorRouteStrategyEnum.getRouter().routeRun(triggerParam, addressList);
            triggerMsgSb.append("<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>" + I18nUtil.getString("jobconf_trigger_run") + "<<<<<<<<<<< </span><br>").append(triggerResult.getMsg());
            // 4.3、trigger (fail retry)
            if (triggerResult.getCode() != ReturnT.SUCCESS_CODE && failStrategy == ExecutorFailStrategyEnum.FAIL_RETRY) {
                triggerResult = executorRouteStrategyEnum.getRouter().routeRun(triggerParam, addressList);
                triggerMsgSb.append("<br><br><span style=\"color:#F39C12;\" > >>>>>>>>>>>" + I18nUtil.getString("jobconf_trigger_fail_retry") + "<<<<<<<<<<< </span><br>").append(triggerResult.getMsg());
            }
        }
        // 5、save trigger-info
        jobLog.setExecutorAddress(triggerResult.getContent());
        jobLog.setTriggerCode(triggerResult.getCode());
        jobLog.setTriggerMsg(triggerMsgSb.toString());
        XxlJobDynamicScheduler.xxlJobLogDao.updateTriggerInfo(jobLog);
        // 6、monitor trigger
        JobFailMonitorHelper.monitor(jobLog.getId());
        logger.debug(">>>>>>>>>>> xxl-job trigger end, jobId:{}", jobLog.getId());
    }
}
Also used : TriggerParam(com.xxl.job.core.biz.model.TriggerParam) XxlJobGroup(com.xxl.job.admin.core.model.XxlJobGroup) ArrayList(java.util.ArrayList) ExecutorBlockStrategyEnum(com.xxl.job.core.enums.ExecutorBlockStrategyEnum) ExecutorRouteStrategyEnum(com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum) Date(java.util.Date) XxlJobInfo(com.xxl.job.admin.core.model.XxlJobInfo) ExecutorFailStrategyEnum(com.xxl.job.admin.core.enums.ExecutorFailStrategyEnum) ReturnT(com.xxl.job.core.biz.model.ReturnT) XxlJobLog(com.xxl.job.admin.core.model.XxlJobLog)

Example 4 with XxlJobGroup

use of com.xxl.job.admin.core.model.XxlJobGroup in project xxl-job by xuxueli.

the class JobLogController method index.

@RequestMapping
public String index(Model model, @RequestParam(required = false, defaultValue = "0") Integer jobId) {
    // 执行器列表
    List<XxlJobGroup> jobGroupList = xxlJobGroupDao.findAll();
    model.addAttribute("JobGroupList", jobGroupList);
    model.addAttribute("GlueTypeEnum", GlueTypeEnum.values());
    // 任务
    if (jobId > 0) {
        XxlJobInfo jobInfo = xxlJobInfoDao.loadById(jobId);
        model.addAttribute("jobInfo", jobInfo);
    }
    return "joblog/joblog.index";
}
Also used : XxlJobGroup(com.xxl.job.admin.core.model.XxlJobGroup) XxlJobInfo(com.xxl.job.admin.core.model.XxlJobInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with XxlJobGroup

use of com.xxl.job.admin.core.model.XxlJobGroup in project xxl-job by xuxueli.

the class XxlJobServiceImpl method dashboardInfo.

@Override
public Map<String, Object> dashboardInfo() {
    int jobInfoCount = xxlJobInfoDao.findAllCount();
    int jobLogCount = xxlJobLogDao.triggerCountByHandleCode(-1);
    int jobLogSuccessCount = xxlJobLogDao.triggerCountByHandleCode(ReturnT.SUCCESS_CODE);
    // executor count
    Set<String> executerAddressSet = new HashSet<String>();
    List<XxlJobGroup> groupList = xxlJobGroupDao.findAll();
    if (CollectionUtils.isNotEmpty(groupList)) {
        for (XxlJobGroup group : groupList) {
            if (CollectionUtils.isNotEmpty(group.getRegistryList())) {
                executerAddressSet.addAll(group.getRegistryList());
            }
        }
    }
    int executorCount = executerAddressSet.size();
    Map<String, Object> dashboardMap = new HashMap<String, Object>();
    dashboardMap.put("jobInfoCount", jobInfoCount);
    dashboardMap.put("jobLogCount", jobLogCount);
    dashboardMap.put("jobLogSuccessCount", jobLogSuccessCount);
    dashboardMap.put("executorCount", executorCount);
    return dashboardMap;
}
Also used : XxlJobGroup(com.xxl.job.admin.core.model.XxlJobGroup)

Aggregations

XxlJobGroup (com.xxl.job.admin.core.model.XxlJobGroup)7 XxlJobInfo (com.xxl.job.admin.core.model.XxlJobInfo)4 ReturnT (com.xxl.job.core.biz.model.ReturnT)2 ArrayList (java.util.ArrayList)2 ExecutorFailStrategyEnum (com.xxl.job.admin.core.enums.ExecutorFailStrategyEnum)1 XxlJobLog (com.xxl.job.admin.core.model.XxlJobLog)1 XxlJobRegistry (com.xxl.job.admin.core.model.XxlJobRegistry)1 ExecutorRouteStrategyEnum (com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum)1 TriggerParam (com.xxl.job.core.biz.model.TriggerParam)1 ExecutorBlockStrategyEnum (com.xxl.job.core.enums.ExecutorBlockStrategyEnum)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Test (org.junit.Test)1 SchedulerException (org.quartz.SchedulerException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1