-
Notifications
You must be signed in to change notification settings - Fork 5
/
liveness_probe.clj
38 lines (33 loc) · 1.06 KB
/
liveness_probe.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns lsp4clj.liveness-probe
(:require
[clojure.core.async :as async]
[clojure.java.shell :as shell]
[clojure.string :as string]))
(set! *warn-on-reflection* true)
(defn ^:private windows-process-alive?
[pid]
(let [{:keys [out]} (shell/sh "tasklist" "/fi" (format "\"pid eq %s\"" pid))]
(string/includes? out (str pid))))
(defn ^:private unix-process-alive?
[pid]
(let [{:keys [exit]} (shell/sh "kill" "-0" (str pid))]
(zero? exit)))
(defn ^:private process-alive?
[pid log]
(try
(if (.contains (System/getProperty "os.name") "Windows")
(windows-process-alive? pid)
(unix-process-alive? pid))
(catch Exception e
(log :warn e "Liveness probe - Checking if process is alive failed.")
true)))
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn start!
[ppid log on-exit]
(async/go-loop []
(async/<! (async/timeout 5000))
(if (process-alive? ppid log)
(recur)
(do
(log :info (str "Liveness probe - Parent process " ppid " is not running - exiting server"))
(on-exit)))))