Chcekpoint - needs docs cleaning and tests
This commit is contained in:
@ -4,6 +4,7 @@ import com.plannaplan.models.ConfigData;
|
||||
import com.plannaplan.models.FileData;
|
||||
import com.plannaplan.models.TourData;
|
||||
import com.plannaplan.repositories.AppConfigRepository;
|
||||
import com.plannaplan.tasks.PerformAcceptAlgorythmTask;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -26,29 +27,38 @@ public class ConfiguratorService {
|
||||
private FileToDatabaseMigrator migrator;
|
||||
@Autowired
|
||||
private AppConfigRepository configRepo;
|
||||
@Autowired
|
||||
private EventService eventService;
|
||||
|
||||
public ConfiguratorService() {
|
||||
}
|
||||
|
||||
/**
|
||||
* methoid to config system
|
||||
* methoid to config system. it shedules PerformAcceptAlgorythmTask as a side
|
||||
* effect
|
||||
*
|
||||
* @param data ConfigData containng system configs
|
||||
*/
|
||||
public void config(ConfigData data) {
|
||||
FileReader reader = new FileReader(data.getFilestream());
|
||||
FileData coursesData = reader.read();
|
||||
this.configRepo.save(new AppConfig(data.getFirstTour(), data.getSecondTour()));
|
||||
final AppConfig config = new AppConfig(data.getFirstTour(), data.getSecondTour());
|
||||
this.configRepo.save(config);
|
||||
migrator.migrate(coursesData);
|
||||
this.shceduleTaskAfterToursDateChange(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save tours to DataBase
|
||||
* @param firstTour First tour period.
|
||||
* Save tours to DataBase and shedule PerformAcceptAlgorythmTask as a side
|
||||
* effect
|
||||
*
|
||||
* @param firstTour First tour period.
|
||||
* @param secondTour Second tour period.
|
||||
*/
|
||||
public void saveTours(TourData firstTour, TourData secondTour) {
|
||||
this.configRepo.save(new AppConfig(firstTour, secondTour));
|
||||
final AppConfig config = new AppConfig(firstTour, secondTour);
|
||||
this.configRepo.save(config);
|
||||
this.shceduleTaskAfterToursDateChange(config);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,9 +88,17 @@ public class ConfiguratorService {
|
||||
*
|
||||
* @param inputStream This input stream contains new courses to import.
|
||||
*/
|
||||
public void importCoursesStream(InputStream inputStream) {
|
||||
public void importCoursesStream(InputStream inputStream) {
|
||||
FileReader reader = new FileReader(inputStream);
|
||||
FileData coursesData = reader.read();
|
||||
migrator.migrate(coursesData);
|
||||
}
|
||||
|
||||
private void shceduleTaskAfterToursDateChange(AppConfig config) {
|
||||
this.eventService.scheduleTask(EventService.FIRST_TOUR_SCHEDULE, new PerformAcceptAlgorythmTask(),
|
||||
config.getFirstTourEndCron());
|
||||
|
||||
this.eventService.scheduleTask(EventService.SECOND_TOUR_SCHEDULE, new PerformAcceptAlgorythmTask(),
|
||||
config.getSecondTourEndCron());
|
||||
}
|
||||
}
|
@ -1,28 +1,82 @@
|
||||
package com.plannaplan.services;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import com.plannaplan.entities.AppConfig;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
// import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class EventService {
|
||||
|
||||
public static final int FIRST_TOUR_SCHEDULE = 0;
|
||||
public static final int SECOND_TOUR_SCHEDULE = 1;
|
||||
|
||||
// @Autowired
|
||||
// private EmailService emailService;
|
||||
|
||||
@Scheduled(cron = "0 2 17 * * *")
|
||||
private ThreadPoolTaskScheduler scheduler;
|
||||
private Map<Integer, ScheduledFuture<?>> jobsMap = new HashMap<>();
|
||||
|
||||
// @Autowired
|
||||
// private ConfiguratorService configService;
|
||||
|
||||
@Scheduled(cron = "0 24 17 * * *")
|
||||
public void collectGroupLosses() {
|
||||
System.out.println("Checking for groups");
|
||||
}
|
||||
|
||||
@Scheduled(cron = "#{@getTourValue}")
|
||||
public void performAcceptAction() {
|
||||
System.out.println("Checking for hahaha");
|
||||
// @Scheduled(cron = "#{eventService.getFirstTourValue()}")
|
||||
// public void performAcceptAction() {
|
||||
// System.out.println("Checking for hahaha");
|
||||
// }
|
||||
|
||||
// new CronTrigger("0 0 0 * * ?",
|
||||
// TimeZone.getTimeZone(TimeZone.getDefault().getID()))
|
||||
|
||||
public void scheduleTask(int taskId, Runnable task, CronTrigger cronTrigger) {
|
||||
ScheduledFuture<?> scheduledTask = jobsMap.get(taskId);
|
||||
if (scheduledTask != null) {
|
||||
scheduledTask.cancel(true);
|
||||
jobsMap.put(taskId, null);
|
||||
}
|
||||
scheduledTask = this.scheduler.schedule(task, cronTrigger);
|
||||
jobsMap.put(taskId, scheduledTask);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String getTourValue() {
|
||||
return "0 6 18 * * *";
|
||||
// @Bean
|
||||
// public String getFirstTourValue() {
|
||||
// final AppConfig config = this.configService.getCurrentConfig();
|
||||
// final LocalDate tourEnd = config.getFirstTourEnd().toLocalDate();
|
||||
// final String cronExp = "0 0 0 " + tourEnd.getDayOfMonth() + " " +
|
||||
// tourEnd.getMonthValue() + " "
|
||||
// + tourEnd.getYear();
|
||||
// return cronExp;
|
||||
// }
|
||||
|
||||
// @Bean
|
||||
// public String getSecondTourValue() {
|
||||
// return "0 6 18 * * *";
|
||||
// }
|
||||
|
||||
@PostConstruct
|
||||
public void initialize() {
|
||||
this.scheduler = new ThreadPoolTaskScheduler();
|
||||
this.scheduler.initialize();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user