Search in sources :

Example 1 with Command

use of org.apache.dolphinscheduler.dao.entity.Command in project dolphinscheduler by apache.

the class ProcessScheduleJob method execute.

/**
 * Called by the Scheduler when a Trigger fires that is associated with the Job
 *
 * @param context JobExecutionContext
 * @throws JobExecutionException if there is an exception while executing the job.
 */
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    Assert.notNull(getProcessService(), "please call init() method first");
    JobDataMap dataMap = context.getJobDetail().getJobDataMap();
    int projectId = dataMap.getInt(Constants.PROJECT_ID);
    int scheduleId = dataMap.getInt(Constants.SCHEDULE_ID);
    Date scheduledFireTime = context.getScheduledFireTime();
    Date fireTime = context.getFireTime();
    logger.info("scheduled fire time :{}, fire time :{}, process id :{}", scheduledFireTime, fireTime, scheduleId);
    // query schedule
    Schedule schedule = getProcessService().querySchedule(scheduleId);
    if (schedule == null) {
        logger.warn("process schedule does not exist in db,delete schedule job in quartz, projectId:{}, scheduleId:{}", projectId, scheduleId);
        deleteJob(projectId, scheduleId);
        return;
    }
    ProcessDefinition processDefinition = getProcessService().findProcessDefineById(schedule.getProcessDefinitionId());
    // release state : online/offline
    ReleaseState releaseState = processDefinition.getReleaseState();
    if (processDefinition == null || releaseState == ReleaseState.OFFLINE) {
        logger.warn("process definition does not exist in db or offline,need not to create command, projectId:{}, processId:{}", projectId, scheduleId);
        return;
    }
    Command command = new Command();
    command.setCommandType(CommandType.SCHEDULER);
    command.setExecutorId(schedule.getUserId());
    command.setFailureStrategy(schedule.getFailureStrategy());
    command.setProcessDefinitionId(schedule.getProcessDefinitionId());
    command.setScheduleTime(scheduledFireTime);
    command.setStartTime(fireTime);
    command.setWarningGroupId(schedule.getWarningGroupId());
    String workerGroup = StringUtils.isEmpty(schedule.getWorkerGroup()) ? Constants.DEFAULT_WORKER_GROUP : schedule.getWorkerGroup();
    command.setWorkerGroup(workerGroup);
    command.setWarningType(schedule.getWarningType());
    command.setProcessInstancePriority(schedule.getProcessInstancePriority());
    getProcessService().createCommand(command);
}
Also used : JobDataMap(org.quartz.JobDataMap) Command(org.apache.dolphinscheduler.dao.entity.Command) ReleaseState(org.apache.dolphinscheduler.common.enums.ReleaseState) Schedule(org.apache.dolphinscheduler.dao.entity.Schedule) ProcessDefinition(org.apache.dolphinscheduler.dao.entity.ProcessDefinition) Date(java.util.Date)

Example 2 with Command

use of org.apache.dolphinscheduler.dao.entity.Command in project dolphinscheduler by apache.

the class ProcessServiceTest method testCreateSubCommand.

@Test
public void testCreateSubCommand() {
    ProcessService processService = new ProcessService();
    ProcessInstance parentInstance = new ProcessInstance();
    parentInstance.setProcessDefinitionId(1);
    parentInstance.setWarningType(WarningType.SUCCESS);
    parentInstance.setWarningGroupId(0);
    TaskInstance task = new TaskInstance();
    task.setTaskJson("{params:{processDefinitionId:100}}");
    task.setId(10);
    ProcessInstance childInstance = null;
    ProcessInstanceMap instanceMap = new ProcessInstanceMap();
    instanceMap.setParentProcessInstanceId(1);
    instanceMap.setParentTaskInstanceId(10);
    Command command = null;
    // father history: start; child null == command type: start
    parentInstance.setHistoryCmd("START_PROCESS");
    parentInstance.setCommandType(CommandType.START_PROCESS);
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.START_PROCESS, command.getCommandType());
    // father history: start,start failure; child null == command type: start
    parentInstance.setCommandType(CommandType.START_FAILURE_TASK_PROCESS);
    parentInstance.setHistoryCmd("START_PROCESS,START_FAILURE_TASK_PROCESS");
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.START_PROCESS, command.getCommandType());
    // father history: scheduler,start failure; child null == command type: scheduler
    parentInstance.setCommandType(CommandType.START_FAILURE_TASK_PROCESS);
    parentInstance.setHistoryCmd("SCHEDULER,START_FAILURE_TASK_PROCESS");
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.SCHEDULER, command.getCommandType());
    // father history: complement,start failure; child null == command type: complement
    parentInstance.setCommandType(CommandType.START_FAILURE_TASK_PROCESS);
    parentInstance.setHistoryCmd("COMPLEMENT_DATA,START_FAILURE_TASK_PROCESS");
    parentInstance.setCommandParam("{complementStartDate:'2020-01-01',complementEndDate:'2020-01-10'}");
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.COMPLEMENT_DATA, command.getCommandType());
    JSONObject complementDate = JSONUtils.parseObject(command.getCommandParam());
    Assert.assertEquals("2020-01-01", String.valueOf(complementDate.get(Constants.CMDPARAM_COMPLEMENT_DATA_START_DATE)));
    Assert.assertEquals("2020-01-10", String.valueOf(complementDate.get(Constants.CMDPARAM_COMPLEMENT_DATA_END_DATE)));
    // father history: start,failure,start failure; child not null == command type: start failure
    childInstance = new ProcessInstance();
    parentInstance.setCommandType(CommandType.START_FAILURE_TASK_PROCESS);
    parentInstance.setHistoryCmd("START_PROCESS,START_FAILURE_TASK_PROCESS");
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.START_FAILURE_TASK_PROCESS, command.getCommandType());
}
Also used : TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) JSONObject(com.alibaba.fastjson.JSONObject) Command(org.apache.dolphinscheduler.dao.entity.Command) ProcessInstanceMap(org.apache.dolphinscheduler.dao.entity.ProcessInstanceMap) ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance) Test(org.junit.Test)

Example 3 with Command

use of org.apache.dolphinscheduler.dao.entity.Command in project dolphinscheduler by apache.

the class CommandMapperTest method testGetOneToRun.

/**
 * test get on command to run
 */
@Test
public void testGetOneToRun() {
    ProcessDefinition processDefinition = createProcessDefinition();
    Command expectedCommand = createCommand(CommandType.START_PROCESS, processDefinition.getId());
    Command actualCommand = commandMapper.getOneToRun();
    assertNotNull(actualCommand);
}
Also used : Command(org.apache.dolphinscheduler.dao.entity.Command) ProcessDefinition(org.apache.dolphinscheduler.dao.entity.ProcessDefinition) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with Command

use of org.apache.dolphinscheduler.dao.entity.Command in project dolphinscheduler by apache.

the class CommandMapperTest method createCommandMap.

/**
 * create command map
 * @param count map count
 * @return command map
 */
private Map<Integer, Command> createCommandMap(Integer count) {
    Map<Integer, Command> commandMap = new HashMap<>();
    for (int i = 0; i < count; i++) {
        Command command = createCommand();
        commandMap.put(command.getId(), command);
    }
    return commandMap;
}
Also used : Command(org.apache.dolphinscheduler.dao.entity.Command) HashMap(java.util.HashMap)

Example 5 with Command

use of org.apache.dolphinscheduler.dao.entity.Command in project dolphinscheduler by apache.

the class CommandMapperTest method testInsert.

/**
 * test insert
 */
@Test
public void testInsert() {
    Command command = createCommand();
    assertNotNull(command.getId());
    assertThat(command.getId(), greaterThan(0));
}
Also used : Command(org.apache.dolphinscheduler.dao.entity.Command) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Command (org.apache.dolphinscheduler.dao.entity.Command)15 Test (org.junit.Test)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 ProcessDefinition (org.apache.dolphinscheduler.dao.entity.ProcessDefinition)2 ProcessInstance (org.apache.dolphinscheduler.dao.entity.ProcessInstance)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 InterProcessMutex (org.apache.curator.framework.recipes.locks.InterProcessMutex)1 ReleaseState (org.apache.dolphinscheduler.common.enums.ReleaseState)1 ProcessInstanceMap (org.apache.dolphinscheduler.dao.entity.ProcessInstanceMap)1 Schedule (org.apache.dolphinscheduler.dao.entity.Schedule)1 TaskInstance (org.apache.dolphinscheduler.dao.entity.TaskInstance)1 JobDataMap (org.quartz.JobDataMap)1