Search in sources :

Example 1 with TaskRunPattern

use of com.bonree.brfs.schedulers.task.model.TaskRunPattern in project BRFS by zhangnianli.

the class OperationTaskJob method operation.

@Override
public void operation(JobExecutionContext context) {
    JobDataMap data = context.getJobDetail().getJobDataMap();
    String dataPath = data.getString(JobDataMapConstract.DATA_PATH);
    ManagerContralFactory mcf = ManagerContralFactory.getInstance();
    MetaTaskManagerInterface release = mcf.getTm();
    if (release == null) {
        throw new NullPointerException("MetaTaskManager is empty !!!");
    }
    List<TaskType> switchList = mcf.getTaskOn();
    if (switchList == null || switchList.isEmpty()) {
        LOG.warn("switch task is empty !!!");
        return;
    }
    SchedulerManagerInterface schd = mcf.getStm();
    if (schd == null) {
        throw new NullPointerException("SchedulerManagerInterface is empty !!!");
    }
    RunnableTaskInterface runTask = mcf.getRt();
    if (runTask == null) {
        throw new NullPointerException("RunnableTaskInterface is empty !!!");
    }
    String typeName;
    String currentTaskName;
    TaskModel task;
    TaskRunPattern runPattern;
    int poolSize;
    int sumbitSize;
    String serverId = mcf.getServerId();
    SumbitTaskInterface sumbitTask;
    // 判断是否有恢复任务,有恢复任务则不进行创建
    boolean rebalanceFlag = WatchSomeThingJob.getState(WatchSomeThingJob.RECOVERY_STATUSE);
    for (TaskType taskType : switchList) {
        sumbitTask = null;
        try {
            if (TaskType.SYSTEM_COPY_CHECK.equals(taskType)) {
                continue;
            }
            typeName = taskType.name();
            poolSize = schd.getTaskPoolSize(typeName);
            sumbitSize = schd.getSumbitedTaskCount(typeName);
            // 判断任务是否可以执行
            boolean isRun = runTask.taskRunnable(taskType.code(), poolSize, sumbitSize);
            if (!isRun) {
                LOG.warn("resource is limit !!! skip {} !!!", typeName);
                continue;
            }
            int retryCount = 3;
            if (TaskType.SYSTEM_CHECK.equals(taskType) || TaskType.SYSTEM_MERGER.equals(taskType) || TaskType.SYSTEM_DELETE.equals(taskType)) {
                retryCount = 0;
            }
            Pair<String, TaskModel> taskPair = TaskStateLifeContral.getCurrentOperationTask(release, typeName, serverId, retryCount);
            if (taskPair == null) {
                LOG.warn("taskType :{} taskName: null is vaild ,skiping !!!", typeName);
                continue;
            }
            currentTaskName = taskPair.getFirst();
            task = TaskStateLifeContral.changeRunTaskModel(taskPair.getSecond(), dataPath);
            // 获取执行策略
            runPattern = runTask.taskRunnPattern(task);
            if (runPattern == null) {
                LOG.warn("TaskRunPattern is null will do it once");
                runPattern = new TaskRunPattern();
                runPattern.setRepeateCount(1);
                runPattern.setSleepTime(1000);
            }
            // 创建任务提交信息
            if (TaskType.SYSTEM_DELETE.equals(taskType)) {
                sumbitTask = createSimpleTask(task, runPattern, currentTaskName, mcf.getServerId(), SystemDeleteJob.class.getCanonicalName(), dataPath);
            }
            if (TaskType.SYSTEM_CHECK.equals(taskType)) {
                sumbitTask = createSimpleTask(task, runPattern, currentTaskName, mcf.getServerId(), SystemCheckJob.class.getCanonicalName(), dataPath);
            }
            if (TaskType.USER_DELETE.equals(taskType)) {
                sumbitTask = createSimpleTask(task, runPattern, currentTaskName, mcf.getServerId(), UserDeleteJob.class.getCanonicalName(), dataPath);
            }
            if (rebalanceFlag && TaskType.SYSTEM_CHECK.equals(taskType)) {
                LOG.warn("rebalance task running !! Skip {} sumbit", taskType.name());
                continue;
            }
            if (sumbitTask == null) {
                LOG.warn("sumbit type:{}, taskName :{}, taskcontent is null", typeName, currentTaskName);
                continue;
            }
            boolean isSumbit = schd.addTask(typeName, sumbitTask);
            LOG.info("sumbit type:{}, taskName :{}, state:{}", typeName, currentTaskName, isSumbit);
            if (!isSumbit) {
                LOG.warn("next cycle will sumbit against type : {}, taskName : {}", typeName, currentTaskName);
                continue;
            }
            // 更新任务状态
            // 更新任务执行的位置
            data.put(typeName, currentTaskName);
        } catch (Exception e) {
            LOG.error("{}", e);
            EmailPool emailPool = EmailPool.getInstance();
            MailWorker.Builder builder = MailWorker.newBuilder(emailPool.getProgramInfo());
            builder.setModel(this.getClass().getSimpleName() + "模块服务发生问题");
            builder.setException(e);
            builder.setMessage("执行任务发生错误");
            builder.setVariable(data.getWrappedMap());
            emailPool.sendEmail(builder);
        }
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) SumbitTaskInterface(com.bonree.brfs.schedulers.task.meta.SumbitTaskInterface) TaskRunPattern(com.bonree.brfs.schedulers.task.model.TaskRunPattern) EmailPool(com.bonree.brfs.email.EmailPool) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) JsonException(com.bonree.brfs.common.utils.JsonUtils.JsonException) RunnableTaskInterface(com.bonree.brfs.schedulers.task.manager.RunnableTaskInterface) TaskType(com.bonree.brfs.common.task.TaskType) MetaTaskManagerInterface(com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface) SchedulerManagerInterface(com.bonree.brfs.schedulers.task.manager.SchedulerManagerInterface) TaskModel(com.bonree.brfs.schedulers.task.model.TaskModel)

Example 2 with TaskRunPattern

use of com.bonree.brfs.schedulers.task.model.TaskRunPattern in project BRFS by zhangnianli.

the class DefaultRunnableTask method taskRunnPattern.

@Override
public TaskRunPattern taskRunnPattern(TaskModel task) throws Exception {
    TaskRunPattern runPattern = new TaskRunPattern();
    int dataSize = task.getAtomList().size();
    int repeadCount = 1;
    repeadCount = (dataSize % batchCount == 0) ? dataSize / batchCount : (dataSize / batchCount + 1);
    repeadCount = repeadCount > maxbatchTimes ? maxbatchTimes : repeadCount <= 0 ? 1 : repeadCount;
    long sleepTime = task.getTaskType() * batchSleepTime > maxBatchSleepTime ? maxBatchSleepTime : task.getTaskType() * batchSleepTime;
    sleepTime = sleepTime == 0 ? batchSleepTime : sleepTime;
    runPattern.setRepeateCount(repeadCount);
    runPattern.setSleepTime(sleepTime);
    return runPattern;
}
Also used : TaskRunPattern(com.bonree.brfs.schedulers.task.model.TaskRunPattern)

Aggregations

TaskRunPattern (com.bonree.brfs.schedulers.task.model.TaskRunPattern)2 TaskType (com.bonree.brfs.common.task.TaskType)1 JsonException (com.bonree.brfs.common.utils.JsonUtils.JsonException)1 EmailPool (com.bonree.brfs.email.EmailPool)1 ManagerContralFactory (com.bonree.brfs.schedulers.ManagerContralFactory)1 MetaTaskManagerInterface (com.bonree.brfs.schedulers.task.manager.MetaTaskManagerInterface)1 RunnableTaskInterface (com.bonree.brfs.schedulers.task.manager.RunnableTaskInterface)1 SchedulerManagerInterface (com.bonree.brfs.schedulers.task.manager.SchedulerManagerInterface)1 SumbitTaskInterface (com.bonree.brfs.schedulers.task.meta.SumbitTaskInterface)1 TaskModel (com.bonree.brfs.schedulers.task.model.TaskModel)1 JobDataMap (org.quartz.JobDataMap)1