Add PKL lib, add test config, create initial jmonit config template

This commit is contained in:
Mans Ziesel 2024-02-04 17:04:35 +01:00
parent 4bb818ed76
commit bec9ff0895
6 changed files with 204 additions and 28 deletions

6
.idea/pklSettings.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PklSettings">
<option name="pklPath" value="/usr/bin/pkl" />
</component>
</project>

View File

@ -5,21 +5,11 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="99a808b4-b8c5-4c6a-8a7b-960f11e07db3" name="Changes" comment=""> <list default="true" id="99a808b4-b8c5-4c6a-8a7b-960f11e07db3" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/JmonitConfig.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/resources/JmonitConfigTemplate.pkl" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle.kts" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/nl/mziesel/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/build.gradle.kts" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradle/wrapper/gradle-wrapper.jar" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradle/wrapper/gradle-wrapper.properties" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradlew" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradlew.bat" afterDir="false" />
<change afterPath="$PROJECT_DIR$/settings.gradle.kts" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/BaseMonitor.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/HttpMonitor.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/IcmpMonitor.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/Main.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -31,10 +21,42 @@
<ProjectState /> <ProjectState />
</projectState> </projectState>
</component> </component>
<component name="ExternalProjectsManager">
<system id="GRADLE">
<state>
<task path="$PROJECT_DIR$">
<activation />
</task>
<projects_view>
<tree_state>
<expand>
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" />
</path>
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" />
<item name="Tasks" type="e4a08cd1:TasksNode" />
</path>
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" />
<item name="Tasks" type="e4a08cd1:TasksNode" />
<item name="build" type="c8890929:TasksNode$1" />
</path>
</expand>
<select />
</tree_state>
</projects_view>
</state>
</system>
</component>
<component name="FileTemplateManagerImpl"> <component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Class" /> <option value="Class" />
<option value="Pkl File" />
</list> </list>
</option> </option>
</component> </component>
@ -52,14 +74,75 @@
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent"><![CDATA[{
"keyToString": { "keyToString": {
"Gradle.jmonit [:JmonitConfig.main()].executor": "Run",
"Gradle.jmonit [:Main.main()].executor": "Run",
"Gradle.jmonit [build].executor": "Run",
"Gradle.jmonit [jar].executor": "Run",
"RunOnceActivity.OpenProjectViewOnStart": "true", "RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "master", "git-widget-placeholder": "gradle",
"kotlin-language-version-configured": "true", "kotlin-language-version-configured": "true",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm", "nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "org.pkl.intellij.settings.PklSettingsConfigurable",
"vue.rearranger.settings.migration": "true" "vue.rearranger.settings.migration": "true"
} }
}]]></component> }]]></component>
<component name="RunManager">
<configuration name="jmonit [build]" type="GradleRunConfiguration" factoryName="Gradle" temporary="true">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="build" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
<configuration name="jmonit [jar]" type="GradleRunConfiguration" factoryName="Gradle" temporary="true">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="jar" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Gradle.jmonit [jar]" />
<item itemvalue="Gradle.jmonit [build]" />
</list>
</recent_temporary>
</component>
<component name="SharedIndexes"> <component name="SharedIndexes">
<attachedChunks> <attachedChunks>
<set> <set>
@ -75,11 +158,15 @@
<option name="number" value="Default" /> <option name="number" value="Default" />
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1707061517834</updated> <updated>1707061517834</updated>
<workItem from="1707061518829" duration="158000" /> <workItem from="1707061518829" duration="1106000" />
</task> </task>
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" /> <option name="version" value="3" />
</component> </component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />
<select />
</component>
</project> </project>

View File

@ -1,4 +1,6 @@
plugins { plugins {
idea
`java-library`
id("java") id("java")
} }
@ -12,6 +14,7 @@ repositories {
dependencies { dependencies {
testImplementation(platform("org.junit:junit-bom:5.9.1")) testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter") testImplementation("org.junit.jupiter:junit-jupiter")
implementation("org.pkl-lang:pkl-config-java:0.25.1")
} }
tasks.test { tasks.test {

View File

@ -0,0 +1,55 @@
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<String>`.
var favoriteFoods = parrot.get("favoriteFoods").as(JavaType.listOf(String.class));
System.out.println(favoriteFoods);
}
}

View File

@ -2,21 +2,33 @@ package nl.mziesel;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
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 class Main {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
HttpMonitor mzieselHttpMonit = new HttpMonitor("Website van Mans", "https://mziesel.nl/"); Config config;
HttpMonitor testHttpMonit = new HttpMonitor("Test website", "https://whoami.mzsl.nl/");
HttpMonitor mcaHttpMonit = new HttpMonitor("Microcenter website", "https://microcenter.nl/");
ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor(); try (var evaluator = ConfigEvaluator.preconfigured()){
config = evaluator.evaluate(ModuleSource.modulePath("jmonitconfig.pkl"));
executorService.execute(mzieselHttpMonit);
executorService.execute(testHttpMonit);
executorService.execute(mcaHttpMonit);
while (!executorService.isTerminated()) {
Thread.sleep(1000);
} }
System.out.println(config);
// ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
// 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);
// }
} }
} }

View File

@ -0,0 +1,13 @@
typealias MonitorType = "http"|"https"|"icmp"
const defaultTimeout: Int = 5000
const defaultInterval: Int = 60
class Monitor {
friendlyName: String
target: String
timeout: Int = defaultTimeout
interval: Int = defaultInterval
}
monitors: Listing<Monitor>