Fix bundling issues & implement gracefull shutdown

This commit is contained in:
Mans Ziesel 2024-02-04 20:18:30 +01:00
parent 431ad8f92d
commit 931b864d76
8 changed files with 114 additions and 51 deletions

View File

@ -4,15 +4,14 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="99a808b4-b8c5-4c6a-8a7b-960f11e07db3" name="Changes" comment="Make PKL work, use config in application"> <list default="true" id="99a808b4-b8c5-4c6a-8a7b-960f11e07db3" name="Changes" comment="Make application use monitors in config, try handle gracefull shutdown">
<change afterPath="$PROJECT_DIR$/src/main/resources/JmonitConfig.pkl" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle.kts" afterDir="false" /> <change beforePath="$PROJECT_DIR$/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle.kts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/nl/mziesel/BaseMonitor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/BaseMonitor.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/nl/mziesel/BaseMonitor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/BaseMonitor.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/nl/mziesel/HttpMonitor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/HttpMonitor.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/nl/mziesel/HttpMonitor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/HttpMonitor.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/nl/mziesel/IcmpMonitor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/IcmpMonitor.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/nl/mziesel/IcmpMonitor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/nl/mziesel/IcmpMonitor.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/nl/mziesel/JmonitConfig.java" beforeDir="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 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 beforePath="$PROJECT_DIR$/src/main/resources/JmonitConfig.pkl" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/JmonitConfig.pkl" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/JmonitConfigTemplate.pkl" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/JmonitConfigTemplate.pkl" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/resources/JmonitConfigTemplate.pkl" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/JmonitConfigTemplate.pkl" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -49,6 +48,30 @@
<item name="Tasks" type="e4a08cd1:TasksNode" /> <item name="Tasks" type="e4a08cd1:TasksNode" />
<item name="build" type="c8890929:TasksNode$1" /> <item name="build" type="c8890929:TasksNode$1" />
</path> </path>
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" />
<item name="Tasks" type="e4a08cd1:TasksNode" />
<item name="build setup" type="c8890929:TasksNode$1" />
</path>
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" />
<item name="Tasks" type="e4a08cd1:TasksNode" />
<item name="distribution" type="c8890929:TasksNode$1" />
</path>
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" />
<item name="Tasks" type="e4a08cd1:TasksNode" />
<item name="documentation" type="c8890929:TasksNode$1" />
</path>
<path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" />
<item name="Tasks" type="e4a08cd1:TasksNode" />
<item name="help" type="c8890929:TasksNode$1" />
</path>
<path> <path>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" /> <item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" /> <item name="jmonit" type="f1a62948:ProjectNode" />
@ -65,15 +88,18 @@
<component name="FileTemplateManagerImpl"> <component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Class" />
<option value="Pkl File" /> <option value="Pkl File" />
<option value="Pkl Template" /> <option value="Pkl Template" />
<option value="Class" />
</list> </list>
</option> </option>
</component> </component>
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component> </component>
<component name="ProblemsViewState">
<option name="selectedTabId" value="ProjectErrors" />
</component>
<component name="ProjectColorInfo"><![CDATA[{ <component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 6 "associatedIndex": 6
}]]></component> }]]></component>
@ -87,6 +113,7 @@
"keyToString": { "keyToString": {
"Gradle.jmonit [:JmonitConfig.main()].executor": "Run", "Gradle.jmonit [:JmonitConfig.main()].executor": "Run",
"Gradle.jmonit [:Main.main()].executor": "Run", "Gradle.jmonit [:Main.main()].executor": "Run",
"Gradle.jmonit [assembleDist].executor": "Run",
"Gradle.jmonit [build].executor": "Run", "Gradle.jmonit [build].executor": "Run",
"Gradle.jmonit [clean].executor": "Run", "Gradle.jmonit [clean].executor": "Run",
"Gradle.jmonit [configClasses].executor": "Run", "Gradle.jmonit [configClasses].executor": "Run",
@ -106,7 +133,29 @@
"vue.rearranger.settings.migration": "true" "vue.rearranger.settings.migration": "true"
} }
}]]></component> }]]></component>
<component name="RunManager"> <component name="RunManager" selected="Gradle.jmonit [testPklConfig]">
<configuration name="jmonit [assembleDist]" 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="assembleDist" />
</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 [build]" type="GradleRunConfiguration" factoryName="Gradle" temporary="true"> <configuration name="jmonit [build]" type="GradleRunConfiguration" factoryName="Gradle" temporary="true">
<ExternalSystemSettings> <ExternalSystemSettings>
<option name="executionName" /> <option name="executionName" />
@ -151,28 +200,6 @@
<RunAsTest>false</RunAsTest> <RunAsTest>false</RunAsTest>
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="jmonit [configClasses]" 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="configClasses" />
</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"> <configuration name="jmonit [jar]" type="GradleRunConfiguration" factoryName="Gradle" temporary="true">
<ExternalSystemSettings> <ExternalSystemSettings>
<option name="executionName" /> <option name="executionName" />
@ -220,10 +247,10 @@
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Gradle.jmonit [testPklConfig]" /> <item itemvalue="Gradle.jmonit [testPklConfig]" />
<item itemvalue="Gradle.jmonit [configClasses]" /> <item itemvalue="Gradle.jmonit [assembleDist]" />
<item itemvalue="Gradle.jmonit [clean]" />
<item itemvalue="Gradle.jmonit [jar]" /> <item itemvalue="Gradle.jmonit [jar]" />
<item itemvalue="Gradle.jmonit [build]" /> <item itemvalue="Gradle.jmonit [build]" />
<item itemvalue="Gradle.jmonit [clean]" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
@ -242,7 +269,7 @@
<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="4346000" /> <workItem from="1707061518829" duration="10760000" />
</task> </task>
<task id="LOCAL-00001" summary="Add PKL lib, add test config, create initial jmonit config template"> <task id="LOCAL-00001" summary="Add PKL lib, add test config, create initial jmonit config template">
<option name="closed" value="true" /> <option name="closed" value="true" />
@ -252,7 +279,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1707062676829</updated> <updated>1707062676829</updated>
</task> </task>
<option name="localTasksCounter" value="2" /> <task id="LOCAL-00002" summary="Make application use monitors in config, try handle gracefull shutdown">
<option name="closed" value="true" />
<created>1707067005971</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1707067005971</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -261,7 +296,8 @@
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="Add PKL lib, add test config, create initial jmonit config template" /> <MESSAGE value="Add PKL lib, add test config, create initial jmonit config template" />
<MESSAGE value="Make PKL work, use config in application" /> <MESSAGE value="Make PKL work, use config in application" />
<option name="LAST_COMMIT_MESSAGE" value="Make PKL work, use config in application" /> <MESSAGE value="Make application use monitors in config, try handle gracefull shutdown" />
<option name="LAST_COMMIT_MESSAGE" value="Make application use monitors in config, try handle gracefull shutdown" />
</component> </component>
<component name="XSLT-Support.FileAssociations.UIState"> <component name="XSLT-Support.FileAssociations.UIState">
<expand /> <expand />

View File

@ -3,6 +3,7 @@ plugins {
`java-library` `java-library`
id("java") id("java")
id("org.pkl-lang") version("0.25.1") id("org.pkl-lang") version("0.25.1")
id("application")
} }
group = "nl.mziesel" group = "nl.mziesel"
@ -12,6 +13,10 @@ repositories {
mavenCentral() mavenCentral()
} }
application {
mainClass = "nl.mziesel.Main"
}
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")
@ -39,6 +44,7 @@ pkl {
} }
} }
tasks.test { tasks.test {
useJUnitPlatform() useJUnitPlatform()
} }

View File

@ -1,6 +1,7 @@
package nl.mziesel; package nl.mziesel;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.BlockingQueue;
public abstract class BaseMonitor implements Runnable { public abstract class BaseMonitor implements Runnable {
private final String friendlyName; private final String friendlyName;
@ -8,18 +9,20 @@ public abstract class BaseMonitor implements Runnable {
private final int interval; private final int interval;
private final int timeout; private final int timeout;
private MonitorStatus prevCheckStatus; private MonitorStatus prevCheckStatus;
private BlockingQueue<Main.Action> queue;
public enum MonitorStatus { public enum MonitorStatus {
ONLINE, ONLINE,
OFFLINE, OFFLINE,
UNKNOWN UNKNOWN
} }
public BaseMonitor(JmonitConfig.Monitor monitor) { public BaseMonitor(JmonitConfig.Monitor monitor, BlockingQueue<Main.Action>blockingQueue) {
this.friendlyName = monitor.friendlyName; this.friendlyName = monitor.friendlyName;
this.target = monitor.target; this.target = monitor.target;
this.interval = (int) monitor.interval; this.interval = (int) monitor.interval;
this.timeout = (int) monitor.timeout; this.timeout = (int) monitor.timeout;
this.prevCheckStatus = MonitorStatus.UNKNOWN; this.prevCheckStatus = MonitorStatus.UNKNOWN;
this.queue = blockingQueue;
} }
public String getFriendlyName() { public String getFriendlyName() {
@ -45,9 +48,12 @@ public abstract class BaseMonitor implements Runnable {
@Override @Override
public void run() { public void run() {
System.out.println("Monitor '" + this.friendlyName + "' start status: " + this.prevCheckStatus);
while (true) { while (true) {
try { try {
if (!queue.isEmpty() && queue.take() == Main.Action.SHUTDOWN) {
return;
}
MonitorStatus checkStatus = this.check(); MonitorStatus checkStatus = this.check();
if (this.prevCheckStatus != checkStatus) { if (this.prevCheckStatus != checkStatus) {
this.setPrevCheckStatus(checkStatus); this.setPrevCheckStatus(checkStatus);

View File

@ -3,10 +3,11 @@ package nl.mziesel;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.concurrent.BlockingQueue;
public class HttpMonitor extends BaseMonitor{ public class HttpMonitor extends BaseMonitor{
public HttpMonitor(JmonitConfig.Monitor monitor) { public HttpMonitor(JmonitConfig.Monitor monitor, BlockingQueue<Main.Action> queue) {
super(monitor); super(monitor, queue);
} }
@Override @Override

View File

@ -1,9 +1,11 @@
package nl.mziesel; package nl.mziesel;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.BlockingQueue;
public class IcmpMonitor extends BaseMonitor { public class IcmpMonitor extends BaseMonitor {
public IcmpMonitor(JmonitConfig.Monitor monitor) { public IcmpMonitor(JmonitConfig.Monitor monitor, BlockingQueue<Main.Action> queue) {
super(monitor); super(monitor, queue);
} }
@Override @Override

View File

@ -1,15 +1,17 @@
package nl.mziesel; package nl.mziesel;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.*;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.pkl.config.java.Config; import org.pkl.config.java.Config;
import org.pkl.config.java.ConfigEvaluator; import org.pkl.config.java.ConfigEvaluator;
import org.pkl.core.ModuleSource; import org.pkl.core.ModuleSource;
public class Main { public class Main {
public enum Action {
SHUTDOWN
}
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
Config config; Config config;
@ -18,31 +20,40 @@ public class Main {
List<JmonitConfig.Monitor> monitors = config.as(JmonitConfig.class).monitors; List<JmonitConfig.Monitor> monitors = config.as(JmonitConfig.class).monitors;
ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor(); ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
BlockingQueue<Action> blockingQueue = new LinkedBlockingDeque<>(1);
for (JmonitConfig.Monitor mon: monitors) { for (JmonitConfig.Monitor mon: monitors) {
switch (mon.monitorType) { switch (mon.monitorType) {
case JmonitConfig.MonitorType.HTTPS -> { case JmonitConfig.MonitorType.HTTPS -> {
HttpMonitor httpMonitor = new HttpMonitor(mon); HttpMonitor httpMonitor = new HttpMonitor(mon, blockingQueue);
executorService.execute(httpMonitor); executorService.execute(httpMonitor);
} }
case JmonitConfig.MonitorType.ICMP -> { case JmonitConfig.MonitorType.ICMP -> {
IcmpMonitor icmpMonitor = new IcmpMonitor(mon); IcmpMonitor icmpMonitor = new IcmpMonitor(mon, blockingQueue);
executorService.execute(icmpMonitor); executorService.execute(icmpMonitor);
} }
} }
} }
// Register a shutdown hook to handle graceful shutdown // 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 // keep running if not shutdown
while (!executorService.isTerminated()) { boolean shuttingDown = false;
Thread.sleep(100); while (!shuttingDown) {
shuttingDown = blockingQueue.take() != Action.SHUTDOWN;
} }
try { try {
// Wait for the tasks to complete before exiting // 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."); System.err.println("Some tasks did not complete within the timeout.");
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@ -5,5 +5,6 @@ monitors {
monitorType = "https" monitorType = "https"
friendlyName = "Website Mans" friendlyName = "Website Mans"
target = "https://mziesel.nl/" target = "https://mziesel.nl/"
timeout = 100
} }
} }

View File

@ -4,13 +4,13 @@ module nl.mziesel.JmonitConfig
typealias MonitorType = "http"|"https"|"icmp" typealias MonitorType = "http"|"https"|"icmp"
const defaultTimeout: Int = 5000 const defaultTimeout: Int = 5000
const defaultInterval: Int = 60 const defaultInterval: Int = 120
class Monitor { class Monitor {
monitorType: MonitorType monitorType: MonitorType
friendlyName: String friendlyName: String(!isEmpty)
target: String target: String(!isEmpty)
timeout: Int = defaultTimeout timeout: Int(this >= 100) = defaultInterval
interval: Int = defaultInterval interval: Int = defaultInterval
} }