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());
}
}
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;
}
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");
}
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);
}
}
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);
}
}
Aggregations