From 14aabc483f9d6f8d85fd1f38ba95f9f33dec61c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Thu, 14 Mar 2024 10:32:29 +0100 Subject: [PATCH] Load basic ntfy values from environment --- backend/src/main/java/core/DemoApplication.java | 5 +++-- .../ntfy/NtfyTaskStartNotification.java | 9 +++++++-- .../services/ntfy/TaskSchedulingService.java | 17 ++++++++++++++++- .../src/main/resources/application.properties | 6 +++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/core/DemoApplication.java b/backend/src/main/java/core/DemoApplication.java index c2972ff..b24664d 100644 --- a/backend/src/main/java/core/DemoApplication.java +++ b/backend/src/main/java/core/DemoApplication.java @@ -11,6 +11,7 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @@ -23,9 +24,9 @@ public class DemoApplication{ @Bean - public CommandLineRunner init() { + public CommandLineRunner init(TaskSchedulingService taskSchedulingService) { return args -> { - TaskSchedulingService.scheduleStartingTask("Finishing ConceptCreator"); + taskSchedulingService.scheduleStartingTask("Finishing ConceptCreator"); }; } } diff --git a/backend/src/main/java/core/services/ntfy/NtfyTaskStartNotification.java b/backend/src/main/java/core/services/ntfy/NtfyTaskStartNotification.java index 039b64f..3f15068 100644 --- a/backend/src/main/java/core/services/ntfy/NtfyTaskStartNotification.java +++ b/backend/src/main/java/core/services/ntfy/NtfyTaskStartNotification.java @@ -3,6 +3,8 @@ package core.services.ntfy; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import java.net.URI; import java.net.URISyntaxException; @@ -12,18 +14,21 @@ import java.net.http.HttpResponse; public class NtfyTaskStartNotification implements Job { + + @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { HttpClient httpClient = HttpClient.newHttpClient(); String msg = "Task " + jobExecutionContext.getMergedJobDataMap().getString("task") + " should have been started by now!"; + try { HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://localhost:4280/Test")) + .uri(new URI(jobExecutionContext.getMergedJobDataMap().getString("ntfy_host") + "/Test")) .POST(HttpRequest.BodyPublishers.ofString(msg)) .header("Tags", "warning") .header("Title", "Task Starting Reminder") - .header("Actions", "view, Open TimeScheduler, https://time.fawkes100.de/, clear=true") + .header("Actions", "view, Open TimeScheduler, "+jobExecutionContext.getMergedJobDataMap().getString("frontend_domain")+", clear=true") .build(); httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); diff --git a/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java b/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java index fb1fed1..8bab5ec 100644 --- a/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java +++ b/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java @@ -3,19 +3,34 @@ package core.services.ntfy; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; +@Service public class TaskSchedulingService { - public static void scheduleStartingTask(String taskName) throws SchedulerException { + @Value("${ntfy.host}") + private String ntfy_host; + + @Value("${ntfy.topic}") + private String ntfy_topic; + + @Value("${frontend.domain}") + private String frontend_domain; + + public void scheduleStartingTask(String taskName) throws SchedulerException { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); JobDetail job = JobBuilder.newJob(NtfyTaskStartNotification.class).build(); job.getJobDataMap().put("task", taskName); + job.getJobDataMap().put("ntfy_host", ntfy_host); + job.getJobDataMap().put("ntfy_topic", ntfy_topic); + job.getJobDataMap().put("frontend_domain", frontend_domain); Trigger immediatly = TriggerBuilder.newTrigger().startNow().build(); diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index c43317d..252cbdd 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -43,4 +43,8 @@ spring.servlet.multipart.max-request-size=4196KB demo.webapp.jwtSecret=demoWebappSecretKey demo.webapp.jwtExpirationMS=86400000 spring.jackson.time-zone=UTC -server.servlet.session.cookie.samesite=None \ No newline at end of file +server.servlet.session.cookie.samesite=None + +ntfy.host=http://localhost:4280 +ntfy.topic=Test +frontend.domain=http://localhost:4200 \ No newline at end of file