diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index ed3e6d1..2b3d066 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,15 +4,14 @@
-
-
+
-
+
@@ -49,6 +48,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -65,15 +88,18 @@
+
+
+
@@ -87,6 +113,7 @@
"keyToString": {
"Gradle.jmonit [:JmonitConfig.main()].executor": "Run",
"Gradle.jmonit [:Main.main()].executor": "Run",
+ "Gradle.jmonit [assembleDist].executor": "Run",
"Gradle.jmonit [build].executor": "Run",
"Gradle.jmonit [clean].executor": "Run",
"Gradle.jmonit [configClasses].executor": "Run",
@@ -106,7 +133,29 @@
"vue.rearranger.settings.migration": "true"
}
}]]>
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+ true
+ false
+ false
+
+
@@ -151,28 +200,6 @@
false
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- true
- true
- false
- false
-
-
@@ -220,10 +247,10 @@
-
-
+
+
@@ -242,7 +269,7 @@
1707061517834
-
+
@@ -252,7 +279,15 @@
1707062676829
-
+
+
+ 1707067005971
+
+
+
+ 1707067005971
+
+
@@ -261,7 +296,8 @@
-
+
+
diff --git a/build.gradle.kts b/build.gradle.kts
index 6777534..70fcf52 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -3,6 +3,7 @@ plugins {
`java-library`
id("java")
id("org.pkl-lang") version("0.25.1")
+ id("application")
}
group = "nl.mziesel"
@@ -12,6 +13,10 @@ repositories {
mavenCentral()
}
+application {
+ mainClass = "nl.mziesel.Main"
+}
+
dependencies {
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
@@ -39,6 +44,7 @@ pkl {
}
}
+
tasks.test {
useJUnitPlatform()
}
\ No newline at end of file
diff --git a/src/main/java/nl/mziesel/BaseMonitor.java b/src/main/java/nl/mziesel/BaseMonitor.java
index e0699d6..b4cc87d 100644
--- a/src/main/java/nl/mziesel/BaseMonitor.java
+++ b/src/main/java/nl/mziesel/BaseMonitor.java
@@ -1,6 +1,7 @@
package nl.mziesel;
import java.io.IOException;
+import java.util.concurrent.BlockingQueue;
public abstract class BaseMonitor implements Runnable {
private final String friendlyName;
@@ -8,18 +9,20 @@ public abstract class BaseMonitor implements Runnable {
private final int interval;
private final int timeout;
private MonitorStatus prevCheckStatus;
+ private BlockingQueue queue;
public enum MonitorStatus {
ONLINE,
OFFLINE,
UNKNOWN
}
- public BaseMonitor(JmonitConfig.Monitor monitor) {
+ public BaseMonitor(JmonitConfig.Monitor monitor, BlockingQueueblockingQueue) {
this.friendlyName = monitor.friendlyName;
this.target = monitor.target;
this.interval = (int) monitor.interval;
this.timeout = (int) monitor.timeout;
this.prevCheckStatus = MonitorStatus.UNKNOWN;
+ this.queue = blockingQueue;
}
public String getFriendlyName() {
@@ -45,9 +48,12 @@ public abstract class BaseMonitor implements Runnable {
@Override
public void run() {
- System.out.println("Monitor '" + this.friendlyName + "' start status: " + this.prevCheckStatus);
while (true) {
try {
+ if (!queue.isEmpty() && queue.take() == Main.Action.SHUTDOWN) {
+ return;
+ }
+
MonitorStatus checkStatus = this.check();
if (this.prevCheckStatus != checkStatus) {
this.setPrevCheckStatus(checkStatus);
diff --git a/src/main/java/nl/mziesel/HttpMonitor.java b/src/main/java/nl/mziesel/HttpMonitor.java
index 57d0e6b..e755af5 100644
--- a/src/main/java/nl/mziesel/HttpMonitor.java
+++ b/src/main/java/nl/mziesel/HttpMonitor.java
@@ -3,10 +3,11 @@ package nl.mziesel;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.net.URI;
+import java.util.concurrent.BlockingQueue;
public class HttpMonitor extends BaseMonitor{
- public HttpMonitor(JmonitConfig.Monitor monitor) {
- super(monitor);
+ public HttpMonitor(JmonitConfig.Monitor monitor, BlockingQueue queue) {
+ super(monitor, queue);
}
@Override
diff --git a/src/main/java/nl/mziesel/IcmpMonitor.java b/src/main/java/nl/mziesel/IcmpMonitor.java
index 791cef6..9997e01 100644
--- a/src/main/java/nl/mziesel/IcmpMonitor.java
+++ b/src/main/java/nl/mziesel/IcmpMonitor.java
@@ -1,9 +1,11 @@
package nl.mziesel;
import java.io.IOException;
+import java.util.concurrent.BlockingQueue;
+
public class IcmpMonitor extends BaseMonitor {
- public IcmpMonitor(JmonitConfig.Monitor monitor) {
- super(monitor);
+ public IcmpMonitor(JmonitConfig.Monitor monitor, BlockingQueue queue) {
+ super(monitor, queue);
}
@Override
diff --git a/src/main/java/nl/mziesel/Main.java b/src/main/java/nl/mziesel/Main.java
index 40afc01..4dc1a0d 100644
--- a/src/main/java/nl/mziesel/Main.java
+++ b/src/main/java/nl/mziesel/Main.java
@@ -1,15 +1,17 @@
package nl.mziesel;
import java.util.List;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
import org.pkl.config.java.Config;
import org.pkl.config.java.ConfigEvaluator;
import org.pkl.core.ModuleSource;
public class Main {
+ public enum Action {
+ SHUTDOWN
+ }
+
public static void main(String[] args) throws InterruptedException {
Config config;
@@ -18,31 +20,40 @@ public class Main {
List monitors = config.as(JmonitConfig.class).monitors;
ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
+ BlockingQueue blockingQueue = new LinkedBlockingDeque<>(1);
for (JmonitConfig.Monitor mon: monitors) {
switch (mon.monitorType) {
case JmonitConfig.MonitorType.HTTPS -> {
- HttpMonitor httpMonitor = new HttpMonitor(mon);
+ HttpMonitor httpMonitor = new HttpMonitor(mon, blockingQueue);
executorService.execute(httpMonitor);
}
case JmonitConfig.MonitorType.ICMP -> {
- IcmpMonitor icmpMonitor = new IcmpMonitor(mon);
+ IcmpMonitor icmpMonitor = new IcmpMonitor(mon, blockingQueue);
executorService.execute(icmpMonitor);
}
}
}
// Register a shutdown hook to handle graceful shutdown
- Runtime.getRuntime().addShutdownHook(new Thread(executorService::shutdown));
+ Runtime.getRuntime().addShutdownHook(new Thread(() -> {
+ executorService.shutdown();
+ try {
+ blockingQueue.put(Action.SHUTDOWN);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }));
// keep running if not shutdown
- while (!executorService.isTerminated()) {
- Thread.sleep(100);
+ boolean shuttingDown = false;
+ while (!shuttingDown) {
+ shuttingDown = blockingQueue.take() != Action.SHUTDOWN;
}
try {
// Wait for the tasks to complete before exiting
- if (!executorService.awaitTermination(1, TimeUnit.MINUTES)) {
+ if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
System.err.println("Some tasks did not complete within the timeout.");
}
} catch (InterruptedException e) {
diff --git a/src/main/resources/JmonitConfig.pkl b/src/main/resources/JmonitConfig.pkl
index 36a64f0..17a34ec 100644
--- a/src/main/resources/JmonitConfig.pkl
+++ b/src/main/resources/JmonitConfig.pkl
@@ -5,5 +5,6 @@ monitors {
monitorType = "https"
friendlyName = "Website Mans"
target = "https://mziesel.nl/"
+ timeout = 100
}
}
\ No newline at end of file
diff --git a/src/main/resources/JmonitConfigTemplate.pkl b/src/main/resources/JmonitConfigTemplate.pkl
index fc5ba0f..93be0cd 100644
--- a/src/main/resources/JmonitConfigTemplate.pkl
+++ b/src/main/resources/JmonitConfigTemplate.pkl
@@ -4,13 +4,13 @@ module nl.mziesel.JmonitConfig
typealias MonitorType = "http"|"https"|"icmp"
const defaultTimeout: Int = 5000
-const defaultInterval: Int = 60
+const defaultInterval: Int = 120
class Monitor {
monitorType: MonitorType
- friendlyName: String
- target: String
- timeout: Int = defaultTimeout
+ friendlyName: String(!isEmpty)
+ target: String(!isEmpty)
+ timeout: Int(this >= 100) = defaultInterval
interval: Int = defaultInterval
}