package com.yunzhupaas.job;

import com.yunzhupaas.engine.model.flowtask.WorkTimeoutJobModel;
import com.yunzhupaas.util.RedisUtil;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 超时设置定时器
 *
 * @author 云筑产品开发平台组
 * @version V3.4.2
 * @copyright 深圳市乐程软件有限公司
 * @date 2024/6/15 16:23
 */
@DisallowConcurrentExecution
public class TimeoutSettingJob extends QuartzJobBean {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private WorkTimeoutJobUtil workTimeoutJobUtil;
    @Autowired
    private
    RedisTemplate redisTemplate;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<WorkTimeoutJobModel> listRedis = workTimeoutJobUtil.getListRedis(redisUtil);
        for (WorkTimeoutJobModel entity : listRedis) {
            boolean useSuccess = redisTemplate.opsForValue().setIfAbsent(WorkTimeoutJobUtil.WORKTIMEOUT_REDIS_KEY + "_key:" + entity.getTaskNodeOperatorId(), System.currentTimeMillis(), 300, TimeUnit.SECONDS);
            if (!useSuccess || entity.isSuspend()) continue;
            workTimeoutJobUtil.runTimeOutMethod(entity, true);
            redisUtil.remove(WorkTimeoutJobUtil.WORKTIMEOUT_REDIS_KEY + "_key:" + entity.getTaskNodeOperatorId());
        }
    }

}

