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" />
</component>
<component name="ChangeListManager">
<list default="true" id="99a808b4-b8c5-4c6a-8a7b-960f11e07db3" name="Changes" comment="Make PKL work, use config in application">
<change afterPath="$PROJECT_DIR$/src/main/resources/JmonitConfig.pkl" afterDir="false" />
<list default="true" id="99a808b4-b8c5-4c6a-8a7b-960f11e07db3" name="Changes" comment="Make application use monitors in config, try handle gracefull shutdown">
<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$/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/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/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" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -49,6 +48,30 @@
<item name="Tasks" type="e4a08cd1:TasksNode" />
<item name="build" 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="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>
<item name="" type="6a2764b6:ExternalProjectsStructure$RootNode" />
<item name="jmonit" type="f1a62948:ProjectNode" />
@ -65,15 +88,18 @@
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
<option value="Pkl File" />
<option value="Pkl Template" />
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProblemsViewState">
<option name="selectedTabId" value="ProjectErrors" />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 6
}]]></component>
@ -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"
}
}]]></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">
<ExternalSystemSettings>
<option name="executionName" />
@ -151,28 +200,6 @@
<RunAsTest>false</RunAsTest>
<method v="2" />
</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">
<ExternalSystemSettings>
<option name="executionName" />
@ -220,10 +247,10 @@
<recent_temporary>
<list>
<item itemvalue="Gradle.jmonit [testPklConfig]" />
<item itemvalue="Gradle.jmonit [configClasses]" />
<item itemvalue="Gradle.jmonit [clean]" />
<item itemvalue="Gradle.jmonit [assembleDist]" />
<item itemvalue="Gradle.jmonit [jar]" />
<item itemvalue="Gradle.jmonit [build]" />
<item itemvalue="Gradle.jmonit [clean]" />
</list>
</recent_temporary>
</component>
@ -242,7 +269,7 @@
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1707061517834</updated>
<workItem from="1707061518829" duration="4346000" />
<workItem from="1707061518829" duration="10760000" />
</task>
<task id="LOCAL-00001" summary="Add PKL lib, add test config, create initial jmonit config template">
<option name="closed" value="true" />
@ -252,7 +279,15 @@
<option name="project" value="LOCAL" />
<updated>1707062676829</updated>
</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 />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -261,7 +296,8 @@
<component name="VcsManagerConfiguration">
<MESSAGE value="Add PKL lib, add test config, create initial jmonit config template" />
<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 name="XSLT-Support.FileAssociations.UIState">
<expand />

View File

@ -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()
}

View File

@ -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<Main.Action> queue;
public enum MonitorStatus {
ONLINE,
OFFLINE,
UNKNOWN
}
public BaseMonitor(JmonitConfig.Monitor monitor) {
public BaseMonitor(JmonitConfig.Monitor monitor, BlockingQueue<Main.Action>blockingQueue) {
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);

View File

@ -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<Main.Action> queue) {
super(monitor, queue);
}
@Override

View File

@ -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<Main.Action> queue) {
super(monitor, queue);
}
@Override

View File

@ -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<JmonitConfig.Monitor> monitors = config.as(JmonitConfig.class).monitors;
ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
BlockingQueue<Action> 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) {

View File

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

View File

@ -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
}