package com.qmth.sop.task.job;

import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.lock.MemoryLock;
import com.qmth.sop.task.job.service.JobService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;

/**
 * @Description: 流程任务提醒job
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/7/9
 */
public class FlowTaskRemindJob extends QuartzJobBean {
    private final static Logger log = LoggerFactory.getLogger(FlowTaskRemindJob.class);

    @Resource
    JobService jobService;

    @Resource
    MemoryLock memoryLock;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        boolean lock = memoryLock.lock(SystemConstant.LOCK_JOB_PREFIX + context.getJobDetail().getKey(), context.getJobDetail().getKey(), SystemConstant.LOCK_JOB_TIME_OUT);
        if (!lock) {
            return;
        }
        try {
            jobService.flowTaskRemindTask();
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
        } finally {
            memoryLock.unlock(SystemConstant.LOCK_JOB_PREFIX + context.getJobDetail().getKey());
        }
    }
}
