Search in sources :

Example 1 with ScheduleParam

use of org.apache.dolphinscheduler.api.dto.ScheduleParam in project dolphinscheduler by apache.

the class SchedulerService method insertSchedule.

/**
 * save schedule
 *
 * @param loginUser login user
 * @param projectName project name
 * @param processDefineId process definition id
 * @param schedule scheduler
 * @param warningType  warning type
 * @param warningGroupId warning group id
 * @param failureStrategy failure strategy
 * @param processInstancePriority process instance priority
 * @param receivers receivers
 * @param receiversCc receivers cc
 * @param workerGroup worker group
 * @return create result code
 * @throws IOException ioexception
 */
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> insertSchedule(User loginUser, String projectName, Integer processDefineId, String schedule, WarningType warningType, int warningGroupId, FailureStrategy failureStrategy, String receivers, String receiversCc, Priority processInstancePriority, String workerGroup) throws IOException {
    Map<String, Object> result = new HashMap<String, Object>(5);
    Project project = projectMapper.queryByName(projectName);
    // check project auth
    boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result);
    if (!hasProjectAndPerm) {
        return result;
    }
    // check work flow define release state
    ProcessDefinition processDefinition = processService.findProcessDefineById(processDefineId);
    result = executorService.checkProcessDefinitionValid(processDefinition, processDefineId);
    if (result.get(Constants.STATUS) != Status.SUCCESS) {
        return result;
    }
    Schedule scheduleObj = new Schedule();
    Date now = new Date();
    scheduleObj.setProjectName(projectName);
    scheduleObj.setProcessDefinitionId(processDefinition.getId());
    scheduleObj.setProcessDefinitionName(processDefinition.getName());
    ScheduleParam scheduleParam = JSONUtils.parseObject(schedule, ScheduleParam.class);
    if (DateUtils.differSec(scheduleParam.getStartTime(), scheduleParam.getEndTime()) == 0) {
        logger.warn("The start time must not be the same as the end");
        putMsg(result, Status.SCHEDULE_START_TIME_END_TIME_SAME);
        return result;
    }
    scheduleObj.setStartTime(scheduleParam.getStartTime());
    scheduleObj.setEndTime(scheduleParam.getEndTime());
    if (!org.quartz.CronExpression.isValidExpression(scheduleParam.getCrontab())) {
        logger.error(scheduleParam.getCrontab() + " verify failure");
        putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, scheduleParam.getCrontab());
        return result;
    }
    scheduleObj.setCrontab(scheduleParam.getCrontab());
    scheduleObj.setWarningType(warningType);
    scheduleObj.setWarningGroupId(warningGroupId);
    scheduleObj.setFailureStrategy(failureStrategy);
    scheduleObj.setCreateTime(now);
    scheduleObj.setUpdateTime(now);
    scheduleObj.setUserId(loginUser.getId());
    scheduleObj.setUserName(loginUser.getUserName());
    scheduleObj.setReleaseState(ReleaseState.OFFLINE);
    scheduleObj.setProcessInstancePriority(processInstancePriority);
    scheduleObj.setWorkerGroup(workerGroup);
    scheduleMapper.insert(scheduleObj);
    /**
     * updateProcessInstance receivers and cc by process definition id
     */
    processDefinition.setReceivers(receivers);
    processDefinition.setReceiversCc(receiversCc);
    processDefinitionMapper.updateById(processDefinition);
    putMsg(result, Status.SUCCESS);
    result.put("scheduleId", scheduleObj.getId());
    return result;
}
Also used : Project(org.apache.dolphinscheduler.dao.entity.Project) ScheduleParam(org.apache.dolphinscheduler.api.dto.ScheduleParam) Schedule(org.apache.dolphinscheduler.dao.entity.Schedule) ProcessDefinition(org.apache.dolphinscheduler.dao.entity.ProcessDefinition) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ScheduleParam

use of org.apache.dolphinscheduler.api.dto.ScheduleParam in project dolphinscheduler by apache.

the class SchedulerService method updateSchedule.

/**
 * updateProcessInstance schedule
 *
 * @param loginUser login user
 * @param projectName project name
 * @param id scheduler id
 * @param scheduleExpression scheduler
 * @param warningType warning type
 * @param warningGroupId warning group id
 * @param failureStrategy failure strategy
 * @param workerGroup worker group
 * @param processInstancePriority process instance priority
 * @param receiversCc receiver cc
 * @param receivers receivers
 * @param scheduleStatus schedule status
 * @return update result code
 * @throws IOException ioexception
 */
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> updateSchedule(User loginUser, String projectName, Integer id, String scheduleExpression, WarningType warningType, int warningGroupId, FailureStrategy failureStrategy, String receivers, String receiversCc, ReleaseState scheduleStatus, Priority processInstancePriority, String workerGroup) throws IOException {
    Map<String, Object> result = new HashMap<String, Object>(5);
    Project project = projectMapper.queryByName(projectName);
    // check project auth
    boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result);
    if (!hasProjectAndPerm) {
        return result;
    }
    // check schedule exists
    Schedule schedule = scheduleMapper.selectById(id);
    if (schedule == null) {
        putMsg(result, Status.SCHEDULE_CRON_NOT_EXISTS, id);
        return result;
    }
    ProcessDefinition processDefinition = processService.findProcessDefineById(schedule.getProcessDefinitionId());
    if (processDefinition == null) {
        putMsg(result, Status.PROCESS_DEFINE_NOT_EXIST, schedule.getProcessDefinitionId());
        return result;
    }
    /**
     * scheduling on-line status forbid modification
     */
    if (checkValid(result, schedule.getReleaseState() == ReleaseState.ONLINE, Status.SCHEDULE_CRON_ONLINE_FORBID_UPDATE)) {
        return result;
    }
    Date now = new Date();
    // updateProcessInstance param
    if (StringUtils.isNotEmpty(scheduleExpression)) {
        ScheduleParam scheduleParam = JSONUtils.parseObject(scheduleExpression, ScheduleParam.class);
        if (DateUtils.differSec(scheduleParam.getStartTime(), scheduleParam.getEndTime()) == 0) {
            logger.warn("The start time must not be the same as the end");
            putMsg(result, Status.SCHEDULE_START_TIME_END_TIME_SAME);
            return result;
        }
        schedule.setStartTime(scheduleParam.getStartTime());
        schedule.setEndTime(scheduleParam.getEndTime());
        if (!org.quartz.CronExpression.isValidExpression(scheduleParam.getCrontab())) {
            putMsg(result, Status.SCHEDULE_CRON_CHECK_FAILED, scheduleParam.getCrontab());
            return result;
        }
        schedule.setCrontab(scheduleParam.getCrontab());
    }
    if (warningType != null) {
        schedule.setWarningType(warningType);
    }
    schedule.setWarningGroupId(warningGroupId);
    if (failureStrategy != null) {
        schedule.setFailureStrategy(failureStrategy);
    }
    if (scheduleStatus != null) {
        schedule.setReleaseState(scheduleStatus);
    }
    schedule.setWorkerGroup(workerGroup);
    schedule.setUpdateTime(now);
    schedule.setProcessInstancePriority(processInstancePriority);
    scheduleMapper.updateById(schedule);
    /**
     * updateProcessInstance recipients and cc by process definition ID
     */
    processDefinition.setReceivers(receivers);
    processDefinition.setReceiversCc(receiversCc);
    processDefinitionMapper.updateById(processDefinition);
    putMsg(result, Status.SUCCESS);
    return result;
}
Also used : Project(org.apache.dolphinscheduler.dao.entity.Project) ScheduleParam(org.apache.dolphinscheduler.api.dto.ScheduleParam) Schedule(org.apache.dolphinscheduler.dao.entity.Schedule) ProcessDefinition(org.apache.dolphinscheduler.dao.entity.ProcessDefinition) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ScheduleParam

use of org.apache.dolphinscheduler.api.dto.ScheduleParam in project dolphinscheduler by apache.

the class SchedulerService method previewSchedule.

/**
 * preview schedule
 *
 * @param loginUser login user
 * @param projectName project name
 * @param schedule schedule expression
 * @return the next five fire time
 */
public Map<String, Object> previewSchedule(User loginUser, String projectName, String schedule) {
    Map<String, Object> result = new HashMap<>(5);
    CronExpression cronExpression;
    ScheduleParam scheduleParam = JSONUtils.parseObject(schedule, ScheduleParam.class);
    Date now = new Date();
    Date startTime = now.after(scheduleParam.getStartTime()) ? now : scheduleParam.getStartTime();
    Date endTime = scheduleParam.getEndTime();
    try {
        cronExpression = CronUtils.parse2CronExpression(scheduleParam.getCrontab());
    } catch (ParseException e) {
        logger.error(e.getMessage(), e);
        putMsg(result, Status.PARSE_TO_CRON_EXPRESSION_ERROR);
        return result;
    }
    List<Date> selfFireDateList = CronUtils.getSelfFireDateList(startTime, endTime, cronExpression, Constants.PREVIEW_SCHEDULE_EXECUTE_COUNT);
    result.put(Constants.DATA_LIST, selfFireDateList.stream().map(t -> DateUtils.dateToString(t)));
    putMsg(result, Status.SUCCESS);
    return result;
}
Also used : java.util(java.util) JSONUtils(org.apache.dolphinscheduler.common.utils.JSONUtils) CronUtils(org.apache.dolphinscheduler.service.quartz.cron.CronUtils) ProcessScheduleJob(org.apache.dolphinscheduler.service.quartz.ProcessScheduleJob) CronExpression(org.quartz.CronExpression) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ScheduleParam(org.apache.dolphinscheduler.api.dto.ScheduleParam) ScheduleMapper(org.apache.dolphinscheduler.dao.mapper.ScheduleMapper) ProcessDefinition(org.apache.dolphinscheduler.dao.entity.ProcessDefinition) Server(org.apache.dolphinscheduler.common.model.Server) StringUtils(org.apache.dolphinscheduler.common.utils.StringUtils) Project(org.apache.dolphinscheduler.dao.entity.Project) ProjectMapper(org.apache.dolphinscheduler.dao.mapper.ProjectMapper) Schedule(org.apache.dolphinscheduler.dao.entity.Schedule) Service(org.springframework.stereotype.Service) ProcessDefinitionMapper(org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper) ParseException(java.text.ParseException) DateUtils(org.apache.dolphinscheduler.common.utils.DateUtils) User(org.apache.dolphinscheduler.dao.entity.User) Constants(org.apache.dolphinscheduler.common.Constants) Logger(org.slf4j.Logger) ProcessService(org.apache.dolphinscheduler.service.process.ProcessService) IOException(java.io.IOException) org.apache.dolphinscheduler.common.enums(org.apache.dolphinscheduler.common.enums) Status(org.apache.dolphinscheduler.api.enums.Status) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) QuartzExecutors(org.apache.dolphinscheduler.service.quartz.QuartzExecutors) PageInfo(org.apache.dolphinscheduler.api.utils.PageInfo) IPage(com.baomidou.mybatisplus.core.metadata.IPage) Transactional(org.springframework.transaction.annotation.Transactional) ScheduleParam(org.apache.dolphinscheduler.api.dto.ScheduleParam) CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException)

Aggregations

ScheduleParam (org.apache.dolphinscheduler.api.dto.ScheduleParam)3 ProcessDefinition (org.apache.dolphinscheduler.dao.entity.ProcessDefinition)3 Project (org.apache.dolphinscheduler.dao.entity.Project)3 Schedule (org.apache.dolphinscheduler.dao.entity.Schedule)3 Transactional (org.springframework.transaction.annotation.Transactional)3 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 java.util (java.util)1 Status (org.apache.dolphinscheduler.api.enums.Status)1 PageInfo (org.apache.dolphinscheduler.api.utils.PageInfo)1 Constants (org.apache.dolphinscheduler.common.Constants)1 org.apache.dolphinscheduler.common.enums (org.apache.dolphinscheduler.common.enums)1 Server (org.apache.dolphinscheduler.common.model.Server)1 DateUtils (org.apache.dolphinscheduler.common.utils.DateUtils)1 JSONUtils (org.apache.dolphinscheduler.common.utils.JSONUtils)1 StringUtils (org.apache.dolphinscheduler.common.utils.StringUtils)1 User (org.apache.dolphinscheduler.dao.entity.User)1 ProcessDefinitionMapper (org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper)1