87 lines
3.2 KiB
Java
87 lines
3.2 KiB
Java
package SuMoC.backend.scheduling;
|
|
|
|
import SuMoC.backend.notification.services.NotificationWorker;
|
|
import SuMoC.backend.suggestion.services.RegularTripSuggestionService;
|
|
import SuMoC.backend.trend.services.StatTableUpdateService;
|
|
import SuMoC.backend.trend.services.TrendNotificationService;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.time.LocalDate;
|
|
|
|
@Component
|
|
@RequiredArgsConstructor
|
|
public class ScheduledTasks {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
|
|
|
|
private final SchedulingProperties properties;
|
|
|
|
private final StatTableUpdateService statTableUpdateService;
|
|
|
|
private final TrendNotificationService trendNotificationService;
|
|
|
|
private final RegularTripSuggestionService regularTripSuggestionService;
|
|
|
|
private final NotificationWorker notificationWorker;
|
|
|
|
/**
|
|
* Calculate weekly mobility stats for all users and generate corresponding notifications.
|
|
*/
|
|
@Scheduled(cron = "#{@schedulingProperties.notifyWeeklyMobilityStats}")
|
|
public void scheduleNotifyWeeklyMobilityStats() {
|
|
logger.info("Scheduled task triggered: calculateWeeklyMobilityStatsForAllUsers");
|
|
try {
|
|
LocalDate dayOfLastWeek = LocalDate.now().minusWeeks(1);
|
|
statTableUpdateService.updateWeeklyMobilityStatsForAllUsers(dayOfLastWeek);
|
|
trendNotificationService.detectAndNotifyTrends(dayOfLastWeek.getYear(), dayOfLastWeek.get(java.time.temporal.IsoFields.WEEK_OF_WEEK_BASED_YEAR));
|
|
} catch (Exception e) {
|
|
logger.error("Error calculating weekly mobility stats.", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Run the notification worker to process pending notifications.
|
|
*/
|
|
@Scheduled(cron = "#{@schedulingProperties.runNotificationWorker}")
|
|
public void scheduleRunNotificationWorker() {
|
|
logger.info("Scheduled task triggered: runNotificationWorker");
|
|
try {
|
|
notificationWorker.run();
|
|
} catch (Exception e) {
|
|
logger.error("Error running notification worker.", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clean up old notifications from the database.
|
|
*/
|
|
@Scheduled(cron = "#{@schedulingProperties.cleanUpOldNotifications}")
|
|
public void scheduleCleanUpOldNotifications() {
|
|
logger.info("Scheduled task triggered: cleanUpOldNotifications");
|
|
try {
|
|
notificationWorker.cleanUpOldNotifications();
|
|
} catch (Exception e) {
|
|
logger.error("Error cleaning up old notifications.", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Detect regular routes for all users and plan notifications for better alternatives.
|
|
*/
|
|
@Scheduled(cron = "#{@schedulingProperties.notifyRegularRouteAlternatives}")
|
|
public void scheduleNotifyRegularRouteAlternatives() {
|
|
logger.info("Scheduled task triggered: notifyRegularRouteAlternatives");
|
|
try {
|
|
regularTripSuggestionService.detectAndNotifyRegularTripsForTodaysDayOfWeek();
|
|
} catch (Exception e) {
|
|
logger.error("Error notifying regular route alternatives.", e);
|
|
}
|
|
}
|
|
|
|
// TODO clean up task for carrousel candidates
|
|
}
|