ProcessScheduleJob.java 4.6 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17
package org.apache.dolphinscheduler.service.quartz;
L
ligang 已提交
18 19


Q
qiaozhanwei 已提交
20 21 22 23 24 25
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.ReleaseState;
import org.apache.dolphinscheduler.dao.entity.Command;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.Schedule;
T
Tboy 已提交
26
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext;
27 28 29 30 31
import org.apache.dolphinscheduler.service.process.ProcessService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
L
ligang 已提交
32 33 34 35 36 37 38 39 40 41 42
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * process schedule job
 */
public class ProcessScheduleJob implements Job {

43 44 45
    /**
     * logger of ProcessScheduleJob
     */
L
ligang 已提交
46 47
    private static final Logger logger = LoggerFactory.getLogger(ProcessScheduleJob.class);

T
Tboy 已提交
48 49
    public ProcessService getProcessService(){
        return SpringApplicationContext.getBean(ProcessService.class);
L
ligang 已提交
50 51 52
    }

    /**
53
     * Called by the Scheduler when a Trigger fires that is associated with the Job
L
ligang 已提交
54
     *
55
     * @param context JobExecutionContext
L
ligang 已提交
56 57 58 59 60
     * @throws JobExecutionException if there is an exception while executing the job.
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

T
Tboy 已提交
61
        Assert.notNull(getProcessService(), "please call init() method first");
L
ligang 已提交
62 63 64 65 66 67

        JobDataMap dataMap = context.getJobDetail().getJobDataMap();

        int projectId = dataMap.getInt(Constants.PROJECT_ID);
        int scheduleId = dataMap.getInt(Constants.SCHEDULE_ID);

leon-baoliang's avatar
leon-baoliang 已提交
68

L
ligang 已提交
69 70
        Date scheduledFireTime = context.getScheduledFireTime();

leon-baoliang's avatar
leon-baoliang 已提交
71

L
ligang 已提交
72 73 74 75 76
        Date fireTime = context.getFireTime();

        logger.info("scheduled fire time :{}, fire time :{}, process id :{}", scheduledFireTime, fireTime, scheduleId);

        // query schedule
T
Tboy 已提交
77
        Schedule schedule = getProcessService().querySchedule(scheduleId);
L
ligang 已提交
78 79 80 81 82 83 84
        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;
        }


T
Tboy 已提交
85
        ProcessDefinition processDefinition = getProcessService().findProcessDefineById(schedule.getProcessDefinitionId());
L
ligang 已提交
86 87 88 89 90 91 92 93
        // 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();
94
        command.setCommandType(CommandType.SCHEDULER);
L
ligang 已提交
95 96 97 98 99 100
        command.setExecutorId(schedule.getUserId());
        command.setFailureStrategy(schedule.getFailureStrategy());
        command.setProcessDefinitionId(schedule.getProcessDefinitionId());
        command.setScheduleTime(scheduledFireTime);
        command.setStartTime(fireTime);
        command.setWarningGroupId(schedule.getWarningGroupId());
101
        command.setWorkerGroup(schedule.getWorkerGroup());
L
ligang 已提交
102 103 104
        command.setWarningType(schedule.getWarningType());
        command.setProcessInstancePriority(schedule.getProcessInstancePriority());

T
Tboy 已提交
105
        getProcessService().createCommand(command);
L
ligang 已提交
106 107 108 109 110 111 112
    }


    /**
     * delete job
     */
    private void deleteJob(int projectId, int scheduleId) {
Q
qiaozhanwei 已提交
113 114
        String jobName = QuartzExecutors.buildJobName(scheduleId);
        String jobGroupName = QuartzExecutors.buildJobGroupName(projectId);
L
ligang 已提交
115 116 117
        QuartzExecutors.getInstance().deleteJob(jobName, jobGroupName);
    }
}