Schedule notifcations for all todays schedules and stop notification when schedule is activated
This commit is contained in:
parent
6c8f216fa5
commit
f2823ad21e
@ -2,6 +2,7 @@ package core;
|
|||||||
|
|
||||||
import core.entities.RoleEntity;
|
import core.entities.RoleEntity;
|
||||||
import core.entities.UserRole;
|
import core.entities.UserRole;
|
||||||
|
import core.entities.timemanager.AdvancedTaskSchedule;
|
||||||
import core.repositories.RoleRepository;
|
import core.repositories.RoleRepository;
|
||||||
import core.repositories.UserRepository;
|
import core.repositories.UserRepository;
|
||||||
import core.services.PropertyService;
|
import core.services.PropertyService;
|
||||||
@ -14,6 +15,10 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
public class DemoApplication{
|
public class DemoApplication{
|
||||||
@ -24,10 +29,9 @@ public class DemoApplication{
|
|||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@NotTest
|
|
||||||
public CommandLineRunner init(TaskSchedulingService taskSchedulingService) {
|
public CommandLineRunner init(TaskSchedulingService taskSchedulingService) {
|
||||||
return args -> {
|
return args -> {
|
||||||
taskSchedulingService.scheduleStartingTask("Finishing ConceptCreator");
|
taskSchedulingService.scheduleStartReminders();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
package core;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Profile;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
|
|
||||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Profile("!test")
|
|
||||||
public @interface NotTest {
|
|
||||||
}
|
|
@ -9,6 +9,8 @@ import core.entities.timemanager.AdvancedTaskSchedule;
|
|||||||
import core.entities.timemanager.BasicTaskSchedule;
|
import core.entities.timemanager.BasicTaskSchedule;
|
||||||
import core.entities.timemanager.Task;
|
import core.entities.timemanager.Task;
|
||||||
import core.services.*;
|
import core.services.*;
|
||||||
|
import core.services.ntfy.TaskSchedulingService;
|
||||||
|
import org.quartz.SchedulerException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
@ -27,6 +29,7 @@ public class ScheduleController {
|
|||||||
|
|
||||||
@Autowired private TaskScheduleService taskScheduleService;
|
@Autowired private TaskScheduleService taskScheduleService;
|
||||||
@Autowired private TaskService taskService;
|
@Autowired private TaskService taskService;
|
||||||
|
@Autowired private TaskSchedulingService taskSchedulingService;
|
||||||
@GetMapping("/schedules")
|
@GetMapping("/schedules")
|
||||||
public ResponseEntity<?> loadAllSchedulesOfUser() {
|
public ResponseEntity<?> loadAllSchedulesOfUser() {
|
||||||
String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||||
@ -198,6 +201,14 @@ public class ScheduleController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServiceResult<AbstractSchedule> serviceResult = taskScheduleService.activateSchedule(permissionResult.getResult());
|
ServiceResult<AbstractSchedule> serviceResult = taskScheduleService.activateSchedule(permissionResult.getResult());
|
||||||
|
if(serviceResult.getResult() instanceof AdvancedTaskSchedule) {
|
||||||
|
try {
|
||||||
|
taskSchedulingService.stopStartReminderNotification((AdvancedTaskSchedule) serviceResult.getResult());
|
||||||
|
} catch (SchedulerException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return ResponseEntity.ok(new ScheduleActivateResponse(serviceResult.getResult().getStartTime()));
|
return ResponseEntity.ok(new ScheduleActivateResponse(serviceResult.getResult().getStartTime()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package core.repositories.timemanager;
|
||||||
|
|
||||||
|
import core.entities.timemanager.AdvancedTaskSchedule;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface AdvancedScheduleRepository extends CrudRepository<AdvancedTaskSchedule, Long> {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -19,4 +19,5 @@ public interface ScheduleRepository extends CrudRepository<AbstractSchedule, Lon
|
|||||||
|
|
||||||
@Query(value = "SELECT s FROM AbstractSchedule s WHERE s.task.taskgroup.user.username = ?1 AND s.startTime is NOT NULL and s.stopTime is NULL")
|
@Query(value = "SELECT s FROM AbstractSchedule s WHERE s.task.taskgroup.user.username = ?1 AND s.startTime is NOT NULL and s.stopTime is NULL")
|
||||||
Optional<AbstractSchedule> getActiveScheduleOfUser(String username);
|
Optional<AbstractSchedule> getActiveScheduleOfUser(String username);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import core.entities.timemanager.AdvancedTaskSchedule;
|
|||||||
import core.entities.timemanager.BasicTaskSchedule;
|
import core.entities.timemanager.BasicTaskSchedule;
|
||||||
import core.entities.timemanager.Task;
|
import core.entities.timemanager.Task;
|
||||||
import core.repositories.UserRepository;
|
import core.repositories.UserRepository;
|
||||||
|
import core.repositories.timemanager.AdvancedScheduleRepository;
|
||||||
import core.repositories.timemanager.ScheduleRepository;
|
import core.repositories.timemanager.ScheduleRepository;
|
||||||
import core.repositories.timemanager.TaskRepository;
|
import core.repositories.timemanager.TaskRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -32,6 +33,7 @@ public class TaskScheduleService {
|
|||||||
@Autowired private UserRepository userRepository;
|
@Autowired private UserRepository userRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
private TaskRepository taskRepository;
|
private TaskRepository taskRepository;
|
||||||
|
@Autowired private AdvancedScheduleRepository advancedScheduleRepository;
|
||||||
|
|
||||||
public List<AbstractSchedule> getAllSchedulesOfUser(String username) {
|
public List<AbstractSchedule> getAllSchedulesOfUser(String username) {
|
||||||
return scheduleRepository.findAllByUsername(username);
|
return scheduleRepository.findAllByUsername(username);
|
||||||
@ -225,6 +227,18 @@ public class TaskScheduleService {
|
|||||||
return findScheduleByDate(allSchedules, date);
|
return findScheduleByDate(allSchedules, date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<AdvancedTaskSchedule> findSchedulesByDate(LocalDate date) {
|
||||||
|
Iterable<AdvancedTaskSchedule> allSchedules = advancedScheduleRepository.findAll();
|
||||||
|
List<AdvancedTaskSchedule> schedulesToday = new ArrayList<>();
|
||||||
|
for(AdvancedTaskSchedule advancedTaskSchedule : allSchedules) {
|
||||||
|
if(advancedTaskSchedule.getScheduleStart().toLocalDate().equals(date)) {
|
||||||
|
schedulesToday.add(advancedTaskSchedule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return schedulesToday;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private List<AbstractSchedule> findScheduleByDate(List<AbstractSchedule> schedules, LocalDate date) {
|
private List<AbstractSchedule> findScheduleByDate(List<AbstractSchedule> schedules, LocalDate date) {
|
||||||
List<AbstractSchedule> filteredSchedules = new ArrayList<>();
|
List<AbstractSchedule> filteredSchedules = new ArrayList<>();
|
||||||
for(AbstractSchedule schedule : schedules) {
|
for(AbstractSchedule schedule : schedules) {
|
||||||
|
@ -1,18 +1,25 @@
|
|||||||
package core.services.ntfy;
|
package core.services.ntfy;
|
||||||
|
|
||||||
|
|
||||||
import core.NotTest;
|
import core.entities.timemanager.AbstractSchedule;
|
||||||
|
import core.entities.timemanager.AdvancedTaskSchedule;
|
||||||
|
import core.repositories.timemanager.ScheduleRepository;
|
||||||
|
import core.repositories.timemanager.TaskRepository;
|
||||||
|
import core.services.TaskScheduleService;
|
||||||
import org.quartz.*;
|
import org.quartz.*;
|
||||||
import org.quartz.impl.StdSchedulerFactory;
|
import org.quartz.impl.StdSchedulerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@NotTest
|
|
||||||
public class TaskSchedulingService {
|
public class TaskSchedulingService {
|
||||||
|
|
||||||
@Value("${ntfy.host}")
|
@Value("${ntfy.host}")
|
||||||
@ -24,19 +31,25 @@ public class TaskSchedulingService {
|
|||||||
@Value("${frontend.domain}")
|
@Value("${frontend.domain}")
|
||||||
private String frontend_domain;
|
private String frontend_domain;
|
||||||
|
|
||||||
public void scheduleStartingTask(String taskName) throws SchedulerException {
|
@Autowired private TaskScheduleService taskScheduleService;
|
||||||
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
|
|
||||||
|
private Scheduler scheduler;
|
||||||
|
|
||||||
|
public void scheduleStartingTask(AdvancedTaskSchedule advancedTaskSchedule) throws SchedulerException {
|
||||||
|
scheduler = StdSchedulerFactory.getDefaultScheduler();
|
||||||
scheduler.start();
|
scheduler.start();
|
||||||
|
|
||||||
JobDetail job = JobBuilder.newJob(NtfyTaskStartNotification.class).build();
|
JobDetail job = JobBuilder.newJob(NtfyTaskStartNotification.class)
|
||||||
job.getJobDataMap().put("task", taskName);
|
.withIdentity(String.valueOf(advancedTaskSchedule.getScheduleID()))
|
||||||
|
.build();
|
||||||
|
job.getJobDataMap().put("task", advancedTaskSchedule.getTask().getTaskName());
|
||||||
job.getJobDataMap().put("ntfy_host", ntfy_host);
|
job.getJobDataMap().put("ntfy_host", ntfy_host);
|
||||||
job.getJobDataMap().put("ntfy_topic", ntfy_topic);
|
job.getJobDataMap().put("ntfy_topic", ntfy_topic);
|
||||||
job.getJobDataMap().put("frontend_domain", frontend_domain);
|
job.getJobDataMap().put("frontend_domain", frontend_domain);
|
||||||
|
|
||||||
Trigger immediatly = TriggerBuilder.newTrigger().startNow().build();
|
Trigger trigger = TriggerBuilder.newTrigger().startAt(calculateDelayInMillis(advancedTaskSchedule.getScheduleStart())).build();
|
||||||
|
|
||||||
scheduler.scheduleJob(job, immediatly);
|
scheduler.scheduleJob(job, trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void scheduleTask() throws SchedulerException {
|
public static void scheduleTask() throws SchedulerException {
|
||||||
@ -64,4 +77,16 @@ public class TaskSchedulingService {
|
|||||||
private static Date calculateDelayInMillis(LocalDateTime executionTime) {
|
private static Date calculateDelayInMillis(LocalDateTime executionTime) {
|
||||||
return Date.from(executionTime.atZone(ZoneId.systemDefault()).toInstant());
|
return Date.from(executionTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 0 0 * * *")
|
||||||
|
public void scheduleStartReminders() throws SchedulerException {
|
||||||
|
List<AdvancedTaskSchedule> advancedTaskSchedules = taskScheduleService.findSchedulesByDate(LocalDate.now());
|
||||||
|
for(AdvancedTaskSchedule advancedTaskSchedule : advancedTaskSchedules) {
|
||||||
|
scheduleStartingTask(advancedTaskSchedule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopStartReminderNotification(AdvancedTaskSchedule schedule) throws SchedulerException {
|
||||||
|
scheduler.deleteJob(JobKey.jobKey(String.valueOf(schedule.getScheduleID())));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user