diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f78dd24..ed3e6d1 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,12 +4,16 @@
-
-
-
+
+
+
+
+
+
+
@@ -45,6 +49,12 @@
+
+
+
+
+
+
@@ -57,6 +67,7 @@
+
@@ -77,9 +88,13 @@
"Gradle.jmonit [:JmonitConfig.main()].executor": "Run",
"Gradle.jmonit [:Main.main()].executor": "Run",
"Gradle.jmonit [build].executor": "Run",
+ "Gradle.jmonit [clean].executor": "Run",
+ "Gradle.jmonit [configClasses].executor": "Run",
"Gradle.jmonit [jar].executor": "Run",
+ "Gradle.jmonit [testPklConfig].executor": "Run",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
+ "SHARE_PROJECT_CONFIGURATION_FILES": "true",
"git-widget-placeholder": "gradle",
"kotlin-language-version-configured": "true",
"node.js.detected.package.eslint": "true",
@@ -114,6 +129,50 @@
false
+
+
+
+
+
+
+
+
+
+
+ true
+ true
+ false
+ false
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+ true
+ false
+ false
+
+
@@ -136,8 +195,33 @@
false
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+ true
+ false
+ false
+
+
+
+
+
@@ -158,13 +242,27 @@
1707061517834
-
+
+
+
+ 1707062676829
+
+
+
+ 1707062676829
+
+
+
+
+
+
+
diff --git a/build.gradle.kts b/build.gradle.kts
index 84d8f67..6777534 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -2,6 +2,7 @@ plugins {
idea
`java-library`
id("java")
+ id("org.pkl-lang") version("0.25.1")
}
group = "nl.mziesel"
@@ -17,6 +18,27 @@ dependencies {
implementation("org.pkl-lang:pkl-config-java:0.25.1")
}
+pkl {
+ javaCodeGenerators {
+ register("configClasses") {
+ sourceModules.set(files("src/main/resources/JmonitConfigTemplate.pkl"))
+ generateJavadoc.set(true)
+ }
+ }
+ evaluators {
+ // Create an evaluator that evaluates the application's runtime configuration.
+ // This creates an equally named task called "testPklConfig".
+ //
+ // This task is registered as a dependency to `check`, so that Pkl modules are checked for
+ // correctness as part of a project's tests.
+ register("testPklConfig") {
+ sourceModules.set(files("src/main/resources/JmonitConfig.pkl"))
+ modulePath.from(sourceSets.main.get().runtimeClasspath)
+ outputFile.set(file("${layout.buildDirectory.get()}/testPklConfig/config"))
+ }
+ }
+}
+
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 d73bc38..e0699d6 100644
--- a/src/main/java/nl/mziesel/BaseMonitor.java
+++ b/src/main/java/nl/mziesel/BaseMonitor.java
@@ -14,11 +14,11 @@ public abstract class BaseMonitor implements Runnable {
OFFLINE,
UNKNOWN
}
- public BaseMonitor(String name, String target) {
- this.friendlyName = name;
- this.target = target;
- this.interval = 5000;
- this.timeout = 15000;
+ public BaseMonitor(JmonitConfig.Monitor monitor) {
+ this.friendlyName = monitor.friendlyName;
+ this.target = monitor.target;
+ this.interval = (int) monitor.interval;
+ this.timeout = (int) monitor.timeout;
this.prevCheckStatus = MonitorStatus.UNKNOWN;
}
diff --git a/src/main/java/nl/mziesel/HttpMonitor.java b/src/main/java/nl/mziesel/HttpMonitor.java
index 8c499e4..57d0e6b 100644
--- a/src/main/java/nl/mziesel/HttpMonitor.java
+++ b/src/main/java/nl/mziesel/HttpMonitor.java
@@ -5,8 +5,8 @@ import java.io.IOException;
import java.net.URI;
public class HttpMonitor extends BaseMonitor{
- public HttpMonitor(String name, String target) {
- super(name, target);
+ public HttpMonitor(JmonitConfig.Monitor monitor) {
+ super(monitor);
}
@Override
diff --git a/src/main/java/nl/mziesel/IcmpMonitor.java b/src/main/java/nl/mziesel/IcmpMonitor.java
index e5157e6..791cef6 100644
--- a/src/main/java/nl/mziesel/IcmpMonitor.java
+++ b/src/main/java/nl/mziesel/IcmpMonitor.java
@@ -2,14 +2,16 @@ package nl.mziesel;
import java.io.IOException;
public class IcmpMonitor extends BaseMonitor {
- public IcmpMonitor(String name, String target) {
- super(name, target);
+ public IcmpMonitor(JmonitConfig.Monitor monitor) {
+ super(monitor);
}
@Override
public MonitorStatus check() throws IOException, InterruptedException {
- Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 " + this.getTarget());
- int returnVal = p1.waitFor();
- return (returnVal == 0) ? MonitorStatus.ONLINE : MonitorStatus.OFFLINE;
+// Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 " + this.getTarget());
+// int returnVal = p1.waitFor();
+// return (returnVal == 0) ? MonitorStatus.ONLINE : MonitorStatus.OFFLINE;
+// *Temp fix*
+ return MonitorStatus.ONLINE;
}
}
\ No newline at end of file
diff --git a/src/main/java/nl/mziesel/JmonitConfig.java b/src/main/java/nl/mziesel/JmonitConfig.java
deleted file mode 100644
index 505d974..0000000
--- a/src/main/java/nl/mziesel/JmonitConfig.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package nl.mziesel;
-
-import org.pkl.config.java.Config;
-import org.pkl.config.java.ConfigEvaluator;
-import org.pkl.config.java.JavaType;
-import org.pkl.core.ModuleSource;
-
-public class JmonitConfig {
- public static void main(String[] args) {
- // Configuration is represented as a tree of `Config` objects.
- Config config;
-
- // To evaluate a Pkl module to a `Config` tree,
- // create a `ConfigEvaluator` and call its `evaluate()` method.
- //
- // There are two ways to create a `ConfigEvaluator`:
- // * `ConfigEvaluator.preconfigured()`
- // * `ConfigEvaluatorBuilder`
- //
- // `ConfigEvaluator.preconfigured()` behaves much the same as the CLI.
- // For example, it allows evaluating modules over HTTPS, which isn't always desirable.
- // `ConfigEvaluatorBuilder` gives more control over a `ConfigEvaluator`'s behavior.
- //
- // It's important to close an evaluator after the last `evaluate()` call.
- // Previously returned `Config` objects remain valid.
- try (var evaluator = ConfigEvaluator.preconfigured()) {
- config = evaluator.evaluate(ModuleSource.text("""
- parrot {
- name = "Polly";
- age = 31;
- favoriteFoods = new Listing {
- apples";
- "crackers"
- }
- }
- """
- ));
- }
-
- // To descend the `Config` tree, use the `get()` method,
- // which returns another `Config` object.
- var parrot = config.get("parrot");
-
- // To access a `Config` object's value,
- // call `as()` and pass the Java type to convert the value to.
- // The conversion is performed by `ConfigEvaluator.getValueMapper()`,
- // which can be customized via `ConfigEvaluatorBuilder`.
- var age = parrot.get("age").as(int.class);
- System.out.println(age);
-
- // The `as()` method also accepts parameterized types such as `List`.
- var favoriteFoods = parrot.get("favoriteFoods").as(JavaType.listOf(String.class));
- System.out.println(favoriteFoods);
- }
-}
\ No newline at end of file
diff --git a/src/main/java/nl/mziesel/Main.java b/src/main/java/nl/mziesel/Main.java
index 87bb35a..40afc01 100644
--- a/src/main/java/nl/mziesel/Main.java
+++ b/src/main/java/nl/mziesel/Main.java
@@ -1,34 +1,52 @@
package nl.mziesel;
+import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
import org.pkl.config.java.Config;
import org.pkl.config.java.ConfigEvaluator;
-import org.pkl.config.java.JavaType;
import org.pkl.core.ModuleSource;
public class Main {
public static void main(String[] args) throws InterruptedException {
Config config;
- try (var evaluator = ConfigEvaluator.preconfigured()){
- config = evaluator.evaluate(ModuleSource.modulePath("jmonitconfig.pkl"));
+ var evaluator = ConfigEvaluator.preconfigured();
+ config = evaluator.evaluate(ModuleSource.modulePath("/JmonitConfig.pkl"));
+ List monitors = config.as(JmonitConfig.class).monitors;
+
+ ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
+
+ for (JmonitConfig.Monitor mon: monitors) {
+ switch (mon.monitorType) {
+ case JmonitConfig.MonitorType.HTTPS -> {
+ HttpMonitor httpMonitor = new HttpMonitor(mon);
+ executorService.execute(httpMonitor);
+ }
+ case JmonitConfig.MonitorType.ICMP -> {
+ IcmpMonitor icmpMonitor = new IcmpMonitor(mon);
+ executorService.execute(icmpMonitor);
+ }
+ }
}
- System.out.println(config);
+ // Register a shutdown hook to handle graceful shutdown
+ Runtime.getRuntime().addShutdownHook(new Thread(executorService::shutdown));
-// ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
+ // keep running if not shutdown
+ while (!executorService.isTerminated()) {
+ Thread.sleep(100);
+ }
-// HttpMonitor mzieselHttpMonit = new HttpMonitor("Website van Mans", "https://mziesel.nl/");
-// HttpMonitor testHttpMonit = new HttpMonitor("Test website", "https://whoami.mzsl.nl/");
-// HttpMonitor mcaHttpMonit = new HttpMonitor("Microcenter website", "https://microcenter.nl/");
-//
-// executorService.execute(mzieselHttpMonit);
-// executorService.execute(testHttpMonit);
-// executorService.execute(mcaHttpMonit);
-
-// while (!executorService.isTerminated()) {
-// Thread.sleep(1000);
-// }
+ try {
+ // Wait for the tasks to complete before exiting
+ if (!executorService.awaitTermination(1, TimeUnit.MINUTES)) {
+ System.err.println("Some tasks did not complete within the timeout.");
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
}
}
\ No newline at end of file
diff --git a/src/main/resources/JmonitConfig.pkl b/src/main/resources/JmonitConfig.pkl
new file mode 100644
index 0000000..36a64f0
--- /dev/null
+++ b/src/main/resources/JmonitConfig.pkl
@@ -0,0 +1,9 @@
+amends "./JmonitConfigTemplate.pkl"
+
+monitors {
+ new {
+ monitorType = "https"
+ friendlyName = "Website Mans"
+ target = "https://mziesel.nl/"
+ }
+}
\ No newline at end of file
diff --git a/src/main/resources/JmonitConfigTemplate.pkl b/src/main/resources/JmonitConfigTemplate.pkl
index 602e764..fc5ba0f 100644
--- a/src/main/resources/JmonitConfigTemplate.pkl
+++ b/src/main/resources/JmonitConfigTemplate.pkl
@@ -1,9 +1,13 @@
+@ModuleInfo { minPklVersion = "0.25.1" }
+module nl.mziesel.JmonitConfig
+
typealias MonitorType = "http"|"https"|"icmp"
const defaultTimeout: Int = 5000
const defaultInterval: Int = 60
class Monitor {
+ monitorType: MonitorType
friendlyName: String
target: String
timeout: Int = defaultTimeout