Skip to content

Commit 3359b7c

Browse files
committed
System Touch
1 parent 93eecc5 commit 3359b7c

4 files changed

Lines changed: 82 additions & 15 deletions

File tree

scripts/shutdown.sh

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
#!/usr/bin/env bash
2-
# shutdown.sh — gracefully stop the WebExpress server and all related connections
2+
# shutdown.sh — kill processes on server ports silently (printing handled by ShutdownHooks via CommonRails)
33

44
PORTS=(49152 49155 5512 6682)
55

6-
echo "[shutdown] Closing server ports: ${PORTS[*]}"
7-
86
for PORT in "${PORTS[@]}"; do
9-
# collect every PID listening on this port
107
PIDS=$(lsof -ti TCP:"$PORT" 2>/dev/null)
11-
if [ -n "$PIDS" ]; then
12-
echo "[shutdown] Sending SIGTERM to PIDs on port $PORT: $PIDS"
13-
kill -TERM $PIDS 2>/dev/null
14-
fi
8+
[ -n "$PIDS" ] && kill -TERM $PIDS 2>/dev/null
159
done
1610

17-
# allow a brief grace period for clean shutdown
1811
sleep 2
1912

2013
for PORT in "${PORTS[@]}"; do
2114
PIDS=$(lsof -ti TCP:"$PORT" 2>/dev/null)
22-
if [ -n "$PIDS" ]; then
23-
echo "[shutdown] Sending SIGKILL to remaining PIDs on port $PORT: $PIDS"
24-
kill -KILL $PIDS 2>/dev/null
25-
fi
15+
[ -n "$PIDS" ] && kill -KILL $PIDS 2>/dev/null
2616
done
27-
28-
echo "[shutdown] Done."

source/exceptions/ExceptionPersistenceService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package exceptions;
22

3+
import java.io.File;
34
import java.io.FileWriter;
45
import java.io.IOException;
56
import java.time.Instant;
@@ -21,6 +22,12 @@ public ExceptionPersistenceService(final String FILEPATH)
2122
*/
2223
public void persist(final ExceptionRecord RECORD)
2324
{
25+
try
26+
{
27+
new File(FILEPATH).getParentFile().mkdirs();
28+
}
29+
catch (Exception ignored) {}
30+
2431
try (FileWriter writer = new FileWriter(FILEPATH, true))
2532
{
2633

source/exceptions/PersistenceListener.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package exceptions;
22

3+
import java.io.File;
34
import java.io.FileWriter;
45
import java.io.IOException;
56
import java.time.Instant;
@@ -29,6 +30,12 @@ public void onException(final ExceptionRecord RECORD)
2930

3031
private void writeRecordToFile(final ExceptionRecord RECORD)
3132
{
33+
try
34+
{
35+
new File(FILEPATH).getParentFile().mkdirs();
36+
}
37+
catch (Exception ignored) {}
38+
3239
try (FileWriter writer = new FileWriter(FILEPATH, true))
3340
{
3441
writer.write("[EXCEPTION] " + Instant.now() + System.lineSeparator() + "Type: " + RECORD.EXCEPTION().getClass().getName() + System.lineSeparator() + "Message: " + RECORD.EXCEPTION().getMessage() + System.lineSeparator() + "Origin: " + RECORD.ORIGIN() + System.lineSeparator() + "StackTrace: " + RECORD.STACKTRACE() + System.lineSeparator() + "------------------------------------------------------------" + System.lineSeparator());

source/shutdown/ShutdownHooks.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package shutdown;
2+
3+
import commons.CommonRails;
4+
5+
import java.io.File;
6+
import java.net.Socket;
7+
8+
/**
9+
* Centralises all JVM shutdown hook registration and shutdown printing via CommonRails.
10+
* The bash script performs the actual OS-level kills silently; all console output goes
11+
* through CommonRails.printShutdownSignal / printSystemComponent.
12+
*/
13+
public class ShutdownHooks
14+
{
15+
public static final int[] PORTS = { 49152, 49155, 5512, 6682 };
16+
17+
public static void register()
18+
{
19+
Runtime.getRuntime().addShutdownHook(new Thread(ShutdownHooks::run, "ShutdownHook"));
20+
}
21+
22+
private static void run()
23+
{
24+
ShutdownHooks owner = new ShutdownHooks();
25+
26+
CommonRails.printSystemComponent(owner, owner.hashCode(), "[shutdown] Closing server ports: 49152 49155 5512 6682");
27+
28+
for (int port : PORTS)
29+
CommonRails.printShutdownSignal(owner, port, "SIGTERM");
30+
31+
// run script silently — it performs the actual kills
32+
try
33+
{
34+
String script = new File("scripts/shutdown.sh").getAbsolutePath();
35+
Process proc = new ProcessBuilder("bash", script)
36+
.redirectOutput(ProcessBuilder.Redirect.DISCARD)
37+
.redirectError(ProcessBuilder.Redirect.DISCARD)
38+
.start();
39+
40+
// grace period mirrors the script's sleep 2
41+
Thread.sleep(2000);
42+
43+
// report any ports that needed SIGKILL
44+
for (int port : PORTS)
45+
{
46+
if (portStillOpen(port))
47+
CommonRails.printShutdownSignal(owner, port, "SIGKILL");
48+
}
49+
50+
proc.waitFor();
51+
}
52+
catch (Exception e)
53+
{
54+
CommonRails.EXCEPTION_SINK.accept(e);
55+
}
56+
57+
CommonRails.printSystemComponent(owner, owner.hashCode(), "[shutdown] Done.");
58+
}
59+
60+
private static boolean portStillOpen(final int PORT)
61+
{
62+
try (Socket s = new Socket("localhost", PORT)) { return true; }
63+
catch (Exception e) { return false; }
64+
}
65+
}

0 commit comments

Comments
 (0)