Search in sources :

Example 1 with QuartzTaskDTO

use of io.choerodon.asgard.infra.dto.QuartzTaskDTO in project choerodon-asgard by open-hand.

the class QuartzRealJobServiceImpl method createInstance.

private void createInstance(long taskId, final QuartzTaskInstanceDTO lastInstance) {
    QuartzTaskDTO task = taskMapper.selectByPrimaryKey(taskId);
    if (task == null) {
        LOGGER.warn("task not exist when createInstance {}", taskId);
        return;
    }
    if (!QuartzDefinition.TaskStatus.ENABLE.name().equals(task.getStatus())) {
        LOGGER.warn("task not enable when createInstance {}", task);
        return;
    }
    QuartzMethodDTO query = new QuartzMethodDTO();
    query.setCode(task.getExecuteMethod());
    QuartzMethodDTO db = methodMapper.selectOne(query);
    if (db == null) {
        LOGGER.warn("task method not exist when createInstance {}", task);
        return;
    }
    QuartzTaskInstanceDTO taskInstance = new QuartzTaskInstanceDTO();
    taskInstance.setTaskId(taskId);
    taskInstance.setTaskName(task.getName());
    taskInstance.setPlannedStartTime(new Date());
    taskInstance.setExecuteMethod(task.getExecuteMethod());
    taskInstance.setRetriedCount(0);
    // 设置taskInstance的层级
    taskInstance.setLevel(task.getLevel());
    taskInstance.setSourceId(task.getSourceId());
    if (lastInstance != null) {
        taskInstance.setActualLastTime(lastInstance.getActualStartTime());
        if (lastInstance.getExecuteResult() != null) {
            taskInstance.setExecuteParams(lastInstance.getExecuteResult());
        } else {
            taskInstance.setExecuteParams(task.getExecuteParams());
        }
    } else {
        taskInstance.setExecuteParams(task.getExecuteParams());
    }
    if (StringUtils.isEmpty(taskInstance.getExecuteParams())) {
        taskInstance.setExecuteParams("{}");
    }
    taskInstance.setStatus(QuartzDefinition.InstanceStatus.RUNNING.name());
    taskInstance.setPlannedNextTime(TriggerUtils.getNextFireTime(task, taskInstance));
    taskInstance.setMaxRetryCount(db.getMaxRetryCount());
    if (instanceMapper.insert(taskInstance) != 1) {
        LOGGER.warn("taskInstance insert error when createInstance {}", task);
    } else {
        sagaInstanceEventPublisher.quartzInstanceEvent(db.getService());
    }
}
Also used : QuartzTaskDTO(io.choerodon.asgard.infra.dto.QuartzTaskDTO) QuartzTaskInstanceDTO(io.choerodon.asgard.infra.dto.QuartzTaskInstanceDTO) QuartzMethodDTO(io.choerodon.asgard.infra.dto.QuartzMethodDTO) Date(java.util.Date)

Example 2 with QuartzTaskDTO

use of io.choerodon.asgard.infra.dto.QuartzTaskDTO in project choerodon-asgard by open-hand.

the class ScheduleTaskServiceImpl method getQuartzTaskByName.

private QuartzTaskDTO getQuartzTaskByName(String name, String level, Long sourceId) {
    QuartzTaskDTO quartzTaskDTO = new QuartzTaskDTO();
    quartzTaskDTO.setName(name);
    quartzTaskDTO.setLevel(level);
    quartzTaskDTO.setSourceId(sourceId);
    QuartzTaskDTO quartzTask = taskMapper.selectOne(quartzTaskDTO);
    if (quartzTask == null) {
        throw new CommonException(TASK_NOT_EXIST);
    }
    // 不是当前源的任务
    if (!sourceId.equals(quartzTask.getSourceId())) {
        throw new CommonException(SOURCE_ID_NOT_MATCH);
    }
    if (!level.equals(quartzTask.getLevel())) {
        throw new CommonException(LEVEL_NOT_MATCH);
    }
    return quartzTask;
}
Also used : QuartzTaskDTO(io.choerodon.asgard.infra.dto.QuartzTaskDTO) CommonException(io.choerodon.core.exception.CommonException)

Example 3 with QuartzTaskDTO

use of io.choerodon.asgard.infra.dto.QuartzTaskDTO in project choerodon-asgard by open-hand.

the class ScheduleTaskServiceImpl method deleteByName.

@Transactional
@Override
public void deleteByName(String name, String level, Long sourceId) {
    QuartzTaskDTO quartzTask = getQuartzTaskByName(name, level, sourceId);
    baseDelete(quartzTask.getId(), quartzTask, "error.scheduleTask.deleteTaskFailed.by.name");
}
Also used : QuartzTaskDTO(io.choerodon.asgard.infra.dto.QuartzTaskDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with QuartzTaskDTO

use of io.choerodon.asgard.infra.dto.QuartzTaskDTO in project choerodon-asgard by open-hand.

the class ScheduleTaskServiceImpl method enable.

@Transactional
@Override
public void enable(long id, long objectVersionNumber, String level, Long sourceId) {
    QuartzTaskDTO quartzTask = getQuartzTask(id, level, sourceId);
    // 将 停用的任务 启用,并恢复job
    if (QuartzDefinition.TaskStatus.DISABLE.name().equals(quartzTask.getStatus())) {
        quartzTask.setStatus(QuartzDefinition.TaskStatus.ENABLE.name());
        quartzTask.setObjectVersionNumber(objectVersionNumber);
        if (taskMapper.updateByPrimaryKey(quartzTask) != 1) {
            throw new CommonException("error.scheduleTask.enableTaskFailed");
        }
        // 更新下次执行时间
        QuartzTaskDTO task = taskMapper.selectByPrimaryKey(id);
        QuartzTaskInstanceDTO lastInstance = instanceMapper.selectLastInstance(id);
        if (lastInstance != null) {
            QuartzTaskInstanceDTO instance = new QuartzTaskInstanceDTO();
            instance.setId(lastInstance.getId());
            instance.setObjectVersionNumber(lastInstance.getObjectVersionNumber());
            if (quartzTask.getTriggerType().equalsIgnoreCase(TriggerType.CRON.getValue())) {
                instance.setPlannedNextTime(TriggerUtils.getStartTime(task.getCronExpression()));
            } else {
                instance.setPlannedNextTime(new Date());
            }
            if (instanceMapper.updateByPrimaryKeySelective(instance) != 1) {
                throw new CommonException("error.scheduleTask.enableTask.update.next.time.Failed");
            }
        }
        quartzJobService.resumeJob(id);
        List<QuartzTaskMemberDTO> noticeMembers = getQuartzTaskMembersByTaskId(quartzTask.getId());
        noticeService.sendNotice(quartzTask, noticeMembers, "启用");
        LOGGER.info("enable job: {}", quartzTask);
    }
}
Also used : QuartzTaskMemberDTO(io.choerodon.asgard.infra.dto.QuartzTaskMemberDTO) QuartzTaskDTO(io.choerodon.asgard.infra.dto.QuartzTaskDTO) QuartzTaskInstanceDTO(io.choerodon.asgard.infra.dto.QuartzTaskInstanceDTO) CommonException(io.choerodon.core.exception.CommonException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with QuartzTaskDTO

use of io.choerodon.asgard.infra.dto.QuartzTaskDTO in project choerodon-asgard by open-hand.

the class ScheduleTaskServiceImpl method create.

@Override
@Transactional(rollbackFor = Exception.class)
public QuartzTaskDTO create(final ScheduleTask dto, String level, Long sourceId) {
    Assert.notNull(dto.getMethodId(), "error.methodId.is.null");
    if (dto.getStartTime() == null && dto.getStartTimeStr() != null) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strToDate = formatter.parse(dto.getStartTimeStr(), pos);
        dto.setStartTime(strToDate);
    }
    QuartzTaskDTO quartzTask = modelMapper.map(dto, QuartzTaskDTO.class);
    QuartzMethodDTO method = methodMapper.selectByPrimaryKey(dto.getMethodId());
    validatorLevelAndQuartzMethod(level, method);
    try {
        List<PropertyJobParam> propertyJobParams = objectMapper.readValue(method.getParams(), new TypeReference<List<PropertyJobParam>>() {
        });
        putDefaultParameter(propertyJobParams, dto, level, sourceId);
        quartzTask.setUserDetails(CommonUtils.getUserDetailsJson(objectMapper));
        quartzTask.setExecuteMethod(method.getCode());
        quartzTask.setId(null);
        if (quartzTask.getStatus() == null || quartzTask.getStatus().equals("")) {
            quartzTask.setStatus(QuartzDefinition.TaskStatus.ENABLE.name());
        }
        quartzTask.setExecuteParams(objectMapper.writeValueAsString(dto.getParams()));
        quartzTask.setLevel(level);
        quartzTask.setSourceId(sourceId);
        quartzTask.setExecuteStrategy(dto.getExecuteStrategy());
        validExecuteParams(dto.getParams(), propertyJobParams);
        if (taskMapper.insertSelective(quartzTask) != 1) {
            throw new CommonException("error.scheduleTask.create");
        }
        QuartzTaskDTO db = taskMapper.selectByPrimaryKey(quartzTask.getId());
        // 插入通知对象失败需要回滚
        List<QuartzTaskMemberDTO> noticeMembers = insertNoticeMember(dto, level, quartzTask);
        // 发送通知失败不需要回滚,已捕获异常
        noticeService.sendNotice(quartzTask, noticeMembers, "启用");
        quartzJobService.addJob(db);
        LOGGER.info("create job: {}", quartzTask);
        return db;
    } catch (IOException e) {
        throw new CommonException("error.scheduleTask.createJsonIOException", e);
    }
}
Also used : IOException(java.io.IOException) QuartzMethodDTO(io.choerodon.asgard.infra.dto.QuartzMethodDTO) QuartzTaskMemberDTO(io.choerodon.asgard.infra.dto.QuartzTaskMemberDTO) QuartzTaskDTO(io.choerodon.asgard.infra.dto.QuartzTaskDTO) PropertyJobParam(io.choerodon.asgard.property.PropertyJobParam) CommonException(io.choerodon.core.exception.CommonException) SimpleDateFormat(java.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

QuartzTaskDTO (io.choerodon.asgard.infra.dto.QuartzTaskDTO)18 CommonException (io.choerodon.core.exception.CommonException)9 Transactional (org.springframework.transaction.annotation.Transactional)7 QuartzTaskInstanceDTO (io.choerodon.asgard.infra.dto.QuartzTaskInstanceDTO)6 QuartzTaskMemberDTO (io.choerodon.asgard.infra.dto.QuartzTaskMemberDTO)6 QuartzMethodDTO (io.choerodon.asgard.infra.dto.QuartzMethodDTO)5 IOException (java.io.IOException)4 PropertyJobParam (io.choerodon.asgard.property.PropertyJobParam)3 ParsePosition (java.text.ParsePosition)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Page (io.choerodon.core.domain.Page)2 ResponseEntity (org.springframework.http.ResponseEntity)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 io.choerodon.asgard.api.vo (io.choerodon.asgard.api.vo)1 NoticeService (io.choerodon.asgard.app.service.NoticeService)1 QuartzJobService (io.choerodon.asgard.app.service.QuartzJobService)1 ScheduleTaskService (io.choerodon.asgard.app.service.ScheduleTaskService)1 DefaultAutowiredField (io.choerodon.asgard.infra.enums.DefaultAutowiredField)1 MemberType (io.choerodon.asgard.infra.enums.MemberType)1