package me.qwq.doghouse.task;

import org.quartz.*;
import org.springframework.stereotype.Component;

import me.qwq.doghouse.cache.CacheEvictionJob;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;

@Component
public class CacheDailyEvictionScheduler extends TaskSchedulerFactory {

    @Resource(name="scheduler")
	Scheduler scheduler;
    
    @PostConstruct
    void onPostConstruct() throws SchedulerException {
        JobDetail cacheEviction = JobBuilder
                .newJob(CacheEvictionJob.class)
                .withIdentity(TaskConstants.CACHE_EVICTION_JOB)
                .build();              
        CronTrigger cacheDailyEvictionTrigger = 
                TriggerBuilder.newTrigger()
                .withIdentity(TaskConstants.CACHE_DAILY_EVICTION_TRIGGER)
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ? ")).build();
        scheduler.scheduleJob(cacheEviction, cacheDailyEvictionTrigger);
        scheduler.start();
        // 启动时手动触发一遍缓存更新
        scheduler.triggerJob(JobKey.jobKey(TaskConstants.CACHE_EVICTION_JOB));
    }
    
}