package net.northking.cctp.testbug.schedule;

import net.northking.cctp.testbug.schedule.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;

@Component
public class BugSchedule {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private RedisLockRegistry redisLockRegistry;

    /**
     * 同步缺陷数据到dw表中
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void syncBugInfo() {
        Lock lock = redisLockRegistry.obtain("syn-bug-info");
        boolean b = lock.tryLock();
        try {
            if (b) {
                scheduleService.syncBugInfo();
            }
        } finally {
            if (b) {
                lock.unlock();
            }
        }
    }
}
