mirror of
https://github.com/logseq/logseq.git
synced 2026-05-17 17:32:35 +00:00
chore: remove cleanup-orphan-processes
This commit is contained in:
@@ -153,8 +153,6 @@
|
||||
(p/let [existing (read-lock path)
|
||||
_ (cleanup-stale-lock! path existing)
|
||||
_ (when (not (fs/existsSync path))
|
||||
(daemon/cleanup-orphan-processes! {:repo repo
|
||||
:data-dir data-dir})
|
||||
(profile/time! profile-session
|
||||
"server.spawn-daemon"
|
||||
(fn []
|
||||
@@ -168,13 +166,9 @@
|
||||
(wait-for-lock path)))
|
||||
(p/catch (fn [e]
|
||||
(if (= :timeout (:code (ex-data e)))
|
||||
(let [orphans (daemon/find-orphan-processes {:repo repo
|
||||
:data-dir data-dir})
|
||||
pids (mapv :pid orphans)]
|
||||
(throw (ex-info "db-worker-node failed to create lock"
|
||||
{:code :server-start-timeout-orphan
|
||||
:repo repo
|
||||
:pids pids})))
|
||||
(throw (ex-info "db-worker-node failed to create lock"
|
||||
{:code :server-start-timeout-orphan
|
||||
:repo repo}))
|
||||
(throw e))))))
|
||||
lock (read-lock path)
|
||||
lock (if (and lock
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
(:require ["child_process" :as child-process]
|
||||
["fs" :as fs]
|
||||
["http" :as http]
|
||||
["path" :as node-path]
|
||||
[clojure.string :as string]
|
||||
[lambdaisland.glogi :as log]
|
||||
[promesa.core :as p]))
|
||||
@@ -21,101 +20,6 @@
|
||||
owner-source
|
||||
:unknown)))
|
||||
|
||||
(defn- platform-supports-process-scan?
|
||||
[]
|
||||
(contains? #{"darwin" "linux"} (.-platform js/process)))
|
||||
|
||||
(defn- normalize-dir
|
||||
[path]
|
||||
(when (seq path)
|
||||
(node-path/resolve path)))
|
||||
|
||||
(defn- unquote-arg
|
||||
[value]
|
||||
(if (and (string? value)
|
||||
(>= (count value) 2)
|
||||
(or (and (string/starts-with? value "\"")
|
||||
(string/ends-with? value "\""))
|
||||
(and (string/starts-with? value "'")
|
||||
(string/ends-with? value "'"))))
|
||||
(subs value 1 (dec (count value)))
|
||||
value))
|
||||
|
||||
(defn- extract-arg
|
||||
[command flag]
|
||||
(some-> (re-find (re-pattern (str "(?:^|\\s)" flag "\\s+((?:\"[^\"]+\"|'[^']+'|\\S+))")) command)
|
||||
second
|
||||
unquote-arg))
|
||||
|
||||
(defn parse-process-args
|
||||
[command]
|
||||
(let [command (string/trim (or command ""))]
|
||||
(when (and (seq command)
|
||||
(re-find #"db-worker-node(?:\.js)?\b" command))
|
||||
(let [repo (extract-arg command "--repo")
|
||||
data-dir (extract-arg command "--data-dir")
|
||||
owner-source (normalize-owner-source (extract-arg command "--owner-source"))]
|
||||
(when (and (seq repo) (seq data-dir))
|
||||
{:repo repo
|
||||
:data-dir (normalize-dir data-dir)
|
||||
:owner-source owner-source})))))
|
||||
|
||||
(defn- parse-process-line
|
||||
[line]
|
||||
(let [line (string/trim (or line ""))]
|
||||
(when-let [[_ pid-str command] (and (seq line)
|
||||
(re-matches #"^(\d+)\s+(.*)$" line))]
|
||||
(let [pid (js/parseInt pid-str 10)]
|
||||
(when (and (number? pid) (pos-int? pid))
|
||||
(when-let [args (parse-process-args command)]
|
||||
(assoc args
|
||||
:pid pid
|
||||
:command command)))))))
|
||||
|
||||
(defn list-db-worker-processes
|
||||
[]
|
||||
(if-not (platform-supports-process-scan?)
|
||||
[]
|
||||
(try
|
||||
(let [output (.execFileSync child-process "ps"
|
||||
#js ["-ax" "-o" "pid=" "-o" "command="]
|
||||
#js {:encoding "utf8"})]
|
||||
(->> (string/split-lines (or output ""))
|
||||
(keep parse-process-line)
|
||||
(vec)))
|
||||
(catch :default e
|
||||
(log/warn :db-worker-daemon/process-scan-failed e)
|
||||
[]))))
|
||||
|
||||
(defn find-orphan-processes
|
||||
[{:keys [repo data-dir]}]
|
||||
(let [data-dir (normalize-dir data-dir)]
|
||||
(->> (list-db-worker-processes)
|
||||
(filter (fn [process]
|
||||
(and (= repo (:repo process))
|
||||
(= data-dir (:data-dir process)))))
|
||||
(vec))))
|
||||
|
||||
(defn cleanup-orphan-processes!
|
||||
[{:keys [repo data-dir]}]
|
||||
(let [orphans (find-orphan-processes {:repo repo :data-dir data-dir})
|
||||
current-pid (.-pid js/process)
|
||||
killed-pids (reduce (fn [result {:keys [pid]}]
|
||||
(if (= current-pid pid)
|
||||
result
|
||||
(try
|
||||
(.kill js/process pid "SIGTERM")
|
||||
(conj result pid)
|
||||
(catch :default e
|
||||
(when-not (= "ESRCH" (.-code e))
|
||||
(log/warn :db-worker-daemon/orphan-kill-failed
|
||||
{:pid pid :error e}))
|
||||
result))))
|
||||
[]
|
||||
orphans)]
|
||||
{:orphans orphans
|
||||
:killed-pids killed-pids}))
|
||||
|
||||
(defn pid-status
|
||||
[pid]
|
||||
(when (number? pid)
|
||||
|
||||
@@ -210,29 +210,20 @@
|
||||
(async done
|
||||
(let [data-dir (node-helper/create-tmp-dir "cli-server-orphan-timeout")
|
||||
repo (str "logseq_db_orphan_timeout_" (subs (str (random-uuid)) 0 8))
|
||||
cleanup-calls (atom 0)
|
||||
spawn-calls (atom 0)]
|
||||
(-> (p/with-redefs [daemon/cleanup-stale-lock! (fn [_ _] (p/resolved nil))
|
||||
daemon/cleanup-orphan-processes! (fn [_]
|
||||
(swap! cleanup-calls inc)
|
||||
{:killed-pids [111]})
|
||||
daemon/spawn-server! (fn [_]
|
||||
(swap! spawn-calls inc)
|
||||
nil)
|
||||
daemon/wait-for-lock (fn [_]
|
||||
(p/rejected (ex-info "timeout"
|
||||
{:code :timeout})))
|
||||
daemon/find-orphan-processes (fn [_]
|
||||
[{:pid 111}
|
||||
{:pid 222}])]
|
||||
{:code :timeout})))]
|
||||
(cli-server/start-server! {:data-dir data-dir
|
||||
:owner-source :cli}
|
||||
repo))
|
||||
(p/then (fn [result]
|
||||
(is (= false (:ok? result)))
|
||||
(is (= :server-start-timeout-orphan (get-in result [:error :code])))
|
||||
(is (= [111 222] (get-in result [:error :pids])))
|
||||
(is (= 1 @cleanup-calls))
|
||||
(is (= 1 @spawn-calls))))
|
||||
(p/catch (fn [e]
|
||||
(is false (str "unexpected error: " e))))
|
||||
@@ -253,7 +244,6 @@
|
||||
nil
|
||||
lock))
|
||||
daemon/cleanup-stale-lock! (fn [_ _] (p/resolved nil))
|
||||
daemon/cleanup-orphan-processes! (fn [_] {:orphans [] :killed-pids []})
|
||||
daemon/spawn-server! (fn [opts]
|
||||
(reset! captured opts)
|
||||
nil)
|
||||
@@ -287,7 +277,6 @@
|
||||
nil
|
||||
lock))
|
||||
daemon/cleanup-stale-lock! (fn [_ _] (p/resolved nil))
|
||||
daemon/cleanup-orphan-processes! (fn [_] {:orphans [] :killed-pids []})
|
||||
daemon/spawn-server! (fn [_] nil)
|
||||
daemon/wait-for-lock (fn [_] (p/resolved true))
|
||||
daemon/wait-for-ready (fn [_] (p/resolved true))]
|
||||
|
||||
@@ -109,46 +109,3 @@
|
||||
(p/catch (fn [e]
|
||||
(is false (str "unexpected error: " e))
|
||||
(done)))))))
|
||||
|
||||
(deftest parse-process-args-reads-db-worker-flags
|
||||
(let [command "node /tmp/db-worker-node.js --repo logseq_db_demo --data-dir /tmp/logseq/graphs --owner-source electron"
|
||||
parsed (daemon/parse-process-args command)]
|
||||
(is (= "logseq_db_demo" (:repo parsed)))
|
||||
(is (= (node-path/resolve "/tmp/logseq/graphs") (:data-dir parsed)))
|
||||
(is (= :electron (:owner-source parsed)))))
|
||||
|
||||
(deftest find-orphan-processes-matches-repo-and-data-dir
|
||||
(let [original-list daemon/list-db-worker-processes
|
||||
target-dir (node-path/resolve "/tmp/logseq/graphs")
|
||||
processes [{:pid 101 :repo "logseq_db_demo" :data-dir target-dir :owner-source :cli}
|
||||
{:pid 102 :repo "logseq_db_demo" :data-dir (node-path/resolve "/tmp/other") :owner-source :cli}
|
||||
{:pid 103 :repo "logseq_db_other" :data-dir target-dir :owner-source :electron}]]
|
||||
(set! daemon/list-db-worker-processes (fn [] processes))
|
||||
(try
|
||||
(let [orphans (daemon/find-orphan-processes {:repo "logseq_db_demo"
|
||||
:data-dir "/tmp/logseq/graphs"})]
|
||||
(is (= [101] (mapv :pid orphans))))
|
||||
(finally
|
||||
(set! daemon/list-db-worker-processes original-list)))))
|
||||
|
||||
(deftest cleanup-orphan-processes-kills-matched-pids
|
||||
(let [original-find daemon/find-orphan-processes
|
||||
original-kill (.-kill js/process)
|
||||
kill-calls (atom [])]
|
||||
(set! daemon/find-orphan-processes
|
||||
(fn [_]
|
||||
[{:pid 90001} {:pid 90002}]))
|
||||
(set! (.-kill js/process)
|
||||
(fn [pid signal]
|
||||
(swap! kill-calls conj [pid signal])
|
||||
true))
|
||||
(try
|
||||
(let [{:keys [killed-pids]} (daemon/cleanup-orphan-processes! {:repo "logseq_db_demo"
|
||||
:data-dir "/tmp/logseq/graphs"})]
|
||||
(is (= [90001 90002] killed-pids))
|
||||
(is (= [[90001 "SIGTERM"]
|
||||
[90002 "SIGTERM"]]
|
||||
@kill-calls)))
|
||||
(finally
|
||||
(set! daemon/find-orphan-processes original-find)
|
||||
(set! (.-kill js/process) original-kill)))))
|
||||
|
||||
Reference in New Issue
Block a user