Skip to content

Commit b91ce5e

Browse files
authored
Merge pull request #431 from IABTechLab/ian-UID2-5188-check-if-pod-terminating
check if the pod is terminating in the healthcheck
2 parents dad64d4 + ef3b02d commit b91ce5e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/main/java/com/uid2/shared/health/HealthManager.java

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

3+
import java.io.File;
34
import java.util.ArrayList;
45
import java.util.List;
56
import java.util.concurrent.atomic.AtomicReference;
@@ -22,6 +23,12 @@ public synchronized HealthComponent registerComponent(String name, boolean initi
2223
return component;
2324
}
2425

26+
public synchronized void registerGenericComponent(IHealthComponent component) {
27+
List<IHealthComponent> newList = new ArrayList<IHealthComponent>(this.componentList.get());
28+
newList.add(component);
29+
this.componentList.set(newList);
30+
}
31+
2532
public boolean isHealthy() {
2633
// simple composite logic: service is healthy if none child component is unhealthy
2734
List<IHealthComponent> list = this.componentList.get();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.uid2.shared.health;
2+
3+
import java.io.File;
4+
import java.util.concurrent.atomic.AtomicReference;
5+
6+
public class PodTerminationMonitor implements IHealthComponent{
7+
private AtomicReference<Boolean> cachedPodTerminating = new AtomicReference<>(false);
8+
private long lastPodCheckTime = 0;
9+
private long fileCheckIntervalMs = 3000;
10+
11+
12+
public PodTerminationMonitor() {}
13+
14+
public PodTerminationMonitor(long fileCheckIntervalMs) {
15+
this.fileCheckIntervalMs = fileCheckIntervalMs;
16+
}
17+
18+
@Override
19+
public String name() {
20+
return "PodTerminationMonitor";
21+
}
22+
23+
@Override
24+
public String reason() {
25+
return checkPodTerminating() ? "Pod is terminating" : "";
26+
}
27+
28+
@Override
29+
public boolean isHealthy() {
30+
return !checkPodTerminating();
31+
}
32+
33+
private boolean checkPodTerminating() {
34+
long currentTime = System.currentTimeMillis();
35+
if (currentTime - lastPodCheckTime >= fileCheckIntervalMs) {
36+
File file = new File(File.separator + "app" + File.separator + "pod_terminating");
37+
boolean newStatus = file.exists();
38+
cachedPodTerminating.set(newStatus);
39+
lastPodCheckTime = currentTime;
40+
}
41+
return cachedPodTerminating.get();
42+
}
43+
}

0 commit comments

Comments
 (0)