Skip to content

Commit 7a25f29

Browse files
committed
Add shutdown hook for graceful metrics submission
1 parent e8eda0c commit 7a25f29

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

core/src/main/java/dev/faststats/core/SimpleMetrics.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ protected SimpleMetrics(SimpleMetrics.Factory<?> factory, Path config) throws Il
6969
this.token = factory.token;
7070
this.tracker = factory.tracker;
7171
this.url = factory.url;
72+
73+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
74+
try {
75+
shutdown();
76+
submitAsync().join();
77+
} catch (Exception e) {
78+
error("Failed to submit metrics on shutdown", e);
79+
}
80+
}, "metrics-shutdown-thread " + getClass().getName()));
7281
}
7382

7483
@VisibleForTesting

0 commit comments

Comments
 (0)