From 2386cbe25418fc8db42439c2b3e77183b1597312 Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Wed, 12 Oct 2022 15:34:50 -0400 Subject: [PATCH] Instrument to debug failure in file/alter-file Also create a fn to handle uncertain global-config-dir paths. Follow up to 24975cda015962a1a00c8881647aa43f8e71b416 --- src/main/frontend/handler/file.cljs | 20 +++++++++--- src/main/frontend/handler/global_config.cljs | 32 ++++++++++++-------- src/main/frontend/handler/web/nfs.cljs | 3 +- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/main/frontend/handler/file.cljs b/src/main/frontend/handler/file.cljs index 09baf751b..6e5f8b69e 100644 --- a/src/main/frontend/handler/file.cljs +++ b/src/main/frontend/handler/file.cljs @@ -98,8 +98,9 @@ #(p/resolved nil) #(let [path-dir (if (and (config/global-config-enabled?) - (global-config-handler/global-config-dir) - (= (path/dirname path) (global-config-handler/global-config-dir))) + ;; Hack until we better understand failure in error handler + (global-config-handler/global-config-dir-exists?) + (= (path/dirname path) (global-config-handler/global-config-dir))) (global-config-handler/global-config-dir) (config/get-repo-dir repo))] (fs/write-file! repo path-dir path content @@ -136,13 +137,22 @@ (state/pub-event! [:shortcut/refresh])))) (fn [error] (when (and (config/global-config-enabled?) + ;; Global-config not started correctly but don't + ;; know root cause yet + ;; https://sentry.io/organizations/logseq/issues/3587411237/events/4b5da8b8e58b4f929bd9e43562213d32/events/?cursor=0%3A0%3A1&project=5311485&statsPeriod=14d + (global-config-handler/global-config-dir-exists?) (= path (global-config-handler/global-config-path))) (state/pub-event! [:notification/show - {:content (str "Failed to write to file " path) - :status :error}])) + {:content (str "Failed to write to file " path) + :status :error}])) (println "Write file failed, path: " path ", content: " content) - (log/error :write/failed error))) + (log/error :write/failed error) + (state/pub-event! [:instrument {:type :write-file/failed-for-alter-file + :payload {:path path + :content-length (count content) + :error-str (str error) + :error error}}]))) result)) (defn set-file-content! diff --git a/src/main/frontend/handler/global_config.cljs b/src/main/frontend/handler/global_config.cljs index 4a0294bcb..73616857c 100644 --- a/src/main/frontend/handler/global_config.cljs +++ b/src/main/frontend/handler/global_config.cljs @@ -16,13 +16,19 @@ (defonce root-dir (atom nil)) +(defn global-config-dir-exists? + "This is used in contexts where we are unusure whether global-config has been + started correctly e.g. an error handler" + [] + (some? @root-dir)) + (defn global-config-dir [] - (when @root-dir (path/join @root-dir "config"))) + (path/join @root-dir "config")) (defn global-config-path [] - (when @root-dir (path/join @root-dir "config" "config.edn"))) + (path/join @root-dir "config" "config.edn")) (defn- set-global-config-state! [content] @@ -34,21 +40,21 @@ (defn- create-global-config-file-if-not-exists [repo-url] - (when-let [config-dir (global-config-dir)] - (let [config-path (global-config-path)] - (p/let [_ (fs/mkdir-if-not-exists config-dir) - file-exists? (fs/create-if-not-exists repo-url config-dir config-path default-content)] - (when-not file-exists? - (file-common-handler/reset-file! repo-url config-path default-content) - (set-global-config-state! default-content)))))) + (let [config-dir (global-config-dir) + config-path (global-config-path)] + (p/let [_ (fs/mkdir-if-not-exists config-dir) + file-exists? (fs/create-if-not-exists repo-url config-dir config-path default-content)] + (when-not file-exists? + (file-common-handler/reset-file! repo-url config-path default-content) + (set-global-config-state! default-content))))) (defn restore-global-config! "Sets global config state from config file" [] - (when-let [config-dir (global-config-dir)] - (let [config-path (global-config-path)] - (p/let [config-content (fs/read-file config-dir config-path)] - (set-global-config-state! config-content))))) + (let [config-dir (global-config-dir) + config-path (global-config-path)] + (p/let [config-content (fs/read-file config-dir config-path)] + (set-global-config-state! config-content)))) (defn start "This component has four responsibilities on start: diff --git a/src/main/frontend/handler/web/nfs.cljs b/src/main/frontend/handler/web/nfs.cljs index 89b7eb2cb..3705bc6bf 100644 --- a/src/main/frontend/handler/web/nfs.cljs +++ b/src/main/frontend/handler/web/nfs.cljs @@ -342,7 +342,8 @@ new-local-files (-> (->db-files mobile-native? electron? dir-name local-files-result) (remove-ignore-files dir-name nfs?)) new-global-files (if (and (config/global-config-enabled?) - (global-config-handler/global-config-dir)) + ;; Hack until we better understand failure in frontend.handler.file/alter-file + (global-config-handler/global-config-dir-exists?)) (p/let [global-files-result (fs/get-files (global-config-handler/global-config-dir) (constantly nil))