package com.friday.distributor.config;

import com.friday.distributor.qrtzjob.TaskPollingJob;
import com.friday.operation.api.distributor.service.IDistributorInternalService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Slf4j
@Configuration
public class DistributorQuartzConfig {
	
	@Autowired
	private IDistributorInternalService distributorInternalService;
	
	@PostConstruct
	public void reloadSyncDataConfig() {
		distributorInternalService.reloadSyncDataConfig();
	}
	
    @Bean
    public Trigger doTaskPollingJobTrigger() {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
        return TriggerBuilder.newTrigger().forJob(doTaskPollingJob())
                .withIdentity("TaskPollingJob", "TaskPollingJob")
                .withSchedule(scheduleBuilder)
                .build();
    }

    @Bean
    public JobDetail doTaskPollingJob() {
        return JobBuilder.newJob(TaskPollingJob.class).withIdentity("TaskPollingJob", "TaskPollingJob").storeDurably().build();
    }
}
