Search in sources :

Example 1 with TaskState

use of com.bonree.brfs.common.task.TaskState in project BRFS by zhangnianli.

the class TaskStateLifeContral method updateTaskStatusByCompelete.

/**
 * 概述:更新任务状态
 * @param serverId
 * @param taskname
 * @param taskType
 * @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
 */
public static void updateTaskStatusByCompelete(String serverId, String taskname, String taskType, TaskResultModel taskResult) {
    if (BrStringUtils.isEmpty(taskname)) {
        LOG.warn("task name is empty !!! {} {} {}", taskType, taskname, serverId);
        return;
    }
    ManagerContralFactory mcf = ManagerContralFactory.getInstance();
    MetaTaskManagerInterface release = mcf.getTm();
    TaskServerNodeModel sTask = release.getTaskServerContentNodeInfo(taskType, taskname, serverId);
    if (sTask == null) {
        LOG.debug("server task is null !!! {} {} {}", taskType, taskname, serverId);
        sTask = new TaskServerNodeModel();
    }
    LOG.debug("TaskMessage complete  sTask :{}", JsonUtils.toJsonStringQuietly(sTask));
    sTask.setResult(taskResult);
    if (BrStringUtils.isEmpty(sTask.getTaskStartTime())) {
        sTask.setTaskStartTime(TimeUtils.formatTimeStamp(System.currentTimeMillis(), TimeUtils.TIME_MILES_FORMATE));
    }
    sTask.setTaskStopTime(TimeUtils.formatTimeStamp(System.currentTimeMillis(), TimeUtils.TIME_MILES_FORMATE));
    TaskState status = taskResult == null ? TaskState.EXCEPTION : taskResult.isSuccess() ? TaskState.FINISH : TaskState.EXCEPTION;
    sTask.setTaskState(status.code());
    release.updateServerTaskContentNode(serverId, taskname, taskType, sTask);
    LOG.info("Complete server task :{} - {} - {} - {}", taskType, taskname, serverId, TaskState.valueOf(sTask.getTaskState()).name());
    // 更新TaskContent
    List<Pair<String, Integer>> cStatus = release.getServerStatus(taskType, taskname);
    if (cStatus == null || cStatus.isEmpty()) {
        return;
    }
    LOG.debug("complete c List {}", cStatus);
    int cstat;
    boolean isException = false;
    int finishCount = 0;
    int size = cStatus.size();
    for (Pair<String, Integer> pair : cStatus) {
        cstat = pair.getSecond();
        if (TaskState.EXCEPTION.code() == cstat) {
            isException = true;
            finishCount += 1;
        } else if (TaskState.FINISH.code() == cstat) {
            finishCount += 1;
        }
    }
    if (finishCount != size) {
        return;
    }
    TaskModel task = release.getTaskContentNodeInfo(taskType, taskname);
    if (task == null) {
        LOG.debug("task is null !!! {} {} {}", taskType, taskname);
        task = new TaskModel();
        task.setCreateTime(TimeUtils.formatTimeStamp(System.currentTimeMillis(), TimeUtils.TIME_MILES_FORMATE));
    }
    if (isException) {
        task.setTaskState(TaskState.EXCEPTION.code());
    } else {
        task.setTaskState(TaskState.FINISH.code());
    }
    release.updateTaskContentNode(task, taskType, taskname);
    LOG.info("complete task :{} - {} - {}", taskType, taskname, TaskState.valueOf(task.getTaskState()).name());
    if (TaskType.SYSTEM_CHECK.name().equals(taskType) && isException) {
        TaskModel cTask = TasksUtils.converyCopyTaskModel(release, taskname);
        if (cTask == null) {
            LOG.error("[{}]:[{}] task can't recovery !!!", taskType, taskname);
            return;
        }
        String str = TasksUtils.createCopyTask(release, taskname, cTask);
        if (BrStringUtils.isEmpty(str)) {
            boolean flag = release.setTransferTask(taskType, taskname);
            LOG.info("[{}] task [{}] find error ,transfer task create {}  ", taskType, taskname, flag ? "succefull !!!" : " fail !!!");
        } else {
            LOG.info("[{}]:[{}] find error create copy task [{}] to recovery ", taskType, taskname, str);
        }
    }
}
Also used : TaskServerNodeModel(com.bonree.brfs.schedulers.task.model.TaskServerNodeModel) MetaTaskManagerInterface(com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) TaskState(com.bonree.brfs.common.task.TaskState) TaskModel(com.bonree.brfs.schedulers.task.model.TaskModel) AtomTaskModel(com.bonree.brfs.schedulers.task.model.AtomTaskModel)

Example 2 with TaskState

use of com.bonree.brfs.common.task.TaskState in project BRFS by zhangnianli.

the class CycleJobWithZKTask method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    int taskTypeCode = 0;
    int batchSize = 10;
    String currentTaskName = null;
    int batchIndex = 0;
    TaskType taskType = null;
    String serverId = null;
    JobDataMap data = context.getJobDetail().getJobDataMap();
    boolean isSuccess = true;
    try {
        // 获取当前的任务信息
        taskTypeCode = data.getInt(JobDataMapConstract.TASK_TYPE);
        taskType = TaskType.valueOf(taskTypeCode);
        batchSize = data.getInt(JobDataMapConstract.BATCH_SIZE);
        if (!data.containsKey(JobDataMapConstract.CURRENT_TASK_NAME)) {
            data.put(JobDataMapConstract.CURRENT_TASK_NAME, "");
        }
        serverId = data.getString(JobDataMapConstract.SERVER_ID);
        currentTaskName = data.getString(JobDataMapConstract.CURRENT_TASK_NAME);
        if (!data.containsKey(JobDataMapConstract.CURRENT_INDEX)) {
            data.put(JobDataMapConstract.CURRENT_INDEX, "0");
            LOG.info("task:{}-{} start", taskType.name(), currentTaskName);
        }
        batchIndex = data.getInt(JobDataMapConstract.CURRENT_INDEX);
        LOG.debug("current :{}, batchId : {}", currentTaskName, batchIndex);
        if (batchSize == 0) {
            batchSize = 10;
        }
        if (batchIndex >= 1) {
            operation(context);
        }
    } catch (Exception e) {
        LOG.info("happend Exception :{}", e);
        context.put("ExceptionMessage", e.getMessage());
        caughtException(context);
        isSuccess = false;
        EmailPool emailPool = EmailPool.getInstance();
        MailWorker.Builder builder = MailWorker.newBuilder(emailPool.getProgramInfo());
        builder.setModel(this.getClass().getSimpleName() + "模块服务发生问题");
        builder.setException(e);
        ManagerContralFactory mcf = ManagerContralFactory.getInstance();
        builder.setMessage(mcf.getGroupName() + "(" + mcf.getServerId() + ")服务 执行任务时发生问题");
        builder.setVariable(data.getWrappedMap());
        emailPool.sendEmail(builder);
    } finally {
        // 判断是否有恢复任务,有恢复任务则不进行创建
        if (WatchSomeThingJob.getState(WatchSomeThingJob.RECOVERY_STATUSE)) {
            LOG.warn("rebalance task is running !! skip check copy task");
            return;
        }
        if (batchIndex >= 1) {
            LOG.debug("batch ID :{} {} {} {} {}", batchIndex, taskType, currentTaskName, serverId, isSuccess ? TaskState.RUN : TaskState.EXCEPTION);
            TaskResultModel resultTask = new TaskResultModel();
            resultTask.setSuccess(isSuccess);
            TaskStateLifeContral.updateMapTaskMessage(context, resultTask);
        }
        // 最后一次执行更新任务状态并处理任务
        if (batchIndex == 1) {
            String result = data.getString(JobDataMapConstract.TASK_RESULT);
            TaskResultModel tResult = new TaskResultModel();
            if (!BrStringUtils.isEmpty(result)) {
                tResult = JsonUtils.toObjectQuietly(result, TaskResultModel.class);
            }
            TaskState state = isSuccess && tResult.isSuccess() ? TaskState.FINISH : TaskState.EXCEPTION;
            LOG.info("task:{}-{}-{}  end !", taskType.name(), currentTaskName, state.name());
            LOG.debug("batch ID :{} {} {} {} {}", batchIndex, taskType, currentTaskName, serverId, state);
            TaskStateLifeContral.updateTaskStatusByCompelete(serverId, currentTaskName, taskType.name(), tResult);
            data.put(JobDataMapConstract.CURRENT_INDEX, (batchIndex - 1) + "");
            data.put(JobDataMapConstract.TASK_RESULT, "");
            data.put(JobDataMapConstract.CURRENT_TASK_NAME, "");
            data.put(JobDataMapConstract.CURRENT_INDEX, (batchIndex - 1) + "");
        } else if (batchIndex <= 0) {
            ManagerContralFactory mcf = ManagerContralFactory.getInstance();
            MetaTaskManagerInterface release = mcf.getTm();
            // 创建任务
            createBatchData(release, data, serverId, taskType, batchSize, 3);
        } else {
            // 更新任务状态
            data.put(JobDataMapConstract.CURRENT_INDEX, (batchIndex - 1) + "");
        }
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) TaskType(com.bonree.brfs.common.task.TaskType) EmailPool(com.bonree.brfs.email.EmailPool) MetaTaskManagerInterface(com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) TaskResultModel(com.bonree.brfs.schedulers.task.model.TaskResultModel) TaskState(com.bonree.brfs.common.task.TaskState) JobExecutionException(org.quartz.JobExecutionException) UnableToInterruptJobException(org.quartz.UnableToInterruptJobException)

Aggregations

TaskState (com.bonree.brfs.common.task.TaskState)2 ManagerContralFactory (com.bonree.brfs.schedulers.ManagerContralFactory)2 MetaTaskManagerInterface (com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface)2 TaskType (com.bonree.brfs.common.task.TaskType)1 EmailPool (com.bonree.brfs.email.EmailPool)1 AtomTaskModel (com.bonree.brfs.schedulers.task.model.AtomTaskModel)1 TaskModel (com.bonree.brfs.schedulers.task.model.TaskModel)1 TaskResultModel (com.bonree.brfs.schedulers.task.model.TaskResultModel)1 TaskServerNodeModel (com.bonree.brfs.schedulers.task.model.TaskServerNodeModel)1 JobDataMap (org.quartz.JobDataMap)1 JobExecutionException (org.quartz.JobExecutionException)1 UnableToInterruptJobException (org.quartz.UnableToInterruptJobException)1