TaskInitRunner.java 1.96 KB
package com.viontech.fanxing.task.runner;

import com.viontech.fanxing.commons.constant.TaskStatus;
import com.viontech.fanxing.commons.model.Task;
import com.viontech.fanxing.commons.model.TaskExample;
import com.viontech.fanxing.task.mapper.TaskMapper;
import com.viontech.fanxing.task.model.TaskData;
import com.viontech.fanxing.task.service.TaskDataService;
import com.viontech.fanxing.task.utils.TaskUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/9/2
 */

@Component
@Slf4j
@Profile("!test")
public class TaskInitRunner implements CommandLineRunner {

    @Resource
    private TaskMapper taskMapper;
    @Resource
    private TaskDataService taskDataService;

    @Override

    public void run(String... args) throws Exception {
        log.info("===================任务初始化开始===================");

        List<Task> tasks = taskMapper.selectByExampleWithBLOBs(new TaskExample());
        for (Task task : tasks) {
            // scene和storage不为空,不处于未部署状态
            if (TaskUtils.INSTANCE.canRun(task)) {
                try {
                    TaskData taskData = taskDataService.getRepository().getTaskDataByUnid(task.getUnid());
                    if (taskData == null) {
                        taskDataService.addTask(task);
                    } else if (taskData.getTask().getStatus() == TaskStatus.RUNNING.val) {
                        taskDataService.distributeTask(taskData);
                    }
                } catch (Exception e) {
                    log.info("初始化任务失败,任务unid:{},失败信息:{}", task.getUnid(), e.getMessage());
                }
            }
        }

        log.info("===================任务初始化结束===================");
    }
}