From 4dfbe876778742ed246f1b7c898f3f01984fc8b3 Mon Sep 17 00:00:00 2001 From: gmsvalente Date: Tue, 9 Apr 2024 14:23:06 -0300 Subject: [PATCH 1/9] Add hotreload functionality --- src/xiana/hotreload.clj | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/xiana/hotreload.clj diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj new file mode 100644 index 00000000..c2ebc335 --- /dev/null +++ b/src/xiana/hotreload.clj @@ -0,0 +1,43 @@ +(ns xiana.hotreload + (:require + [clojure.core.async :refer [go-loop]] + [ns-tracker.core :refer [ns-tracker]])) + +;;; reloader function from ring.middleware.reload + +(defn- reloader [dirs retry?] + (let [modified-namespaces (ns-tracker dirs) + load-queue (java.util.concurrent.LinkedBlockingDeque.)] + (fn [] + (locking load-queue + (doseq [ns-sym (reverse (modified-namespaces))] + (.push load-queue ns-sym)) + (loop [] + (when-let [ns-sym (.peek load-queue)] + (if retry? + (do (require ns-sym :reload) (.remove load-queue)) + (do (.remove load-queue) (require ns-sym :reload))) + (recur))))))) + +(defn hotreload + "Function to hotreload the system. Add it after server-start at configuration phase \"->system\". + (-> (config/config app-cfg) + ... + xiana.webserver/start + xiana.hotreload/hotreload) + If a :xiana/hotreload config key is provided it needs: + :restart-fn \"the function to restart the system\" if none 'user/start-dev-system + :tracker {:dirs \"the directories vector list to search for changes\" :reload-compile-errors? \"true\"}." + [cfg] + (let [{:keys [restart-fn tracker]} (:xiana/hotreload cfg {:restart-fn 'user/start-dev-system}) + dirs (:dirs tracker ["src"]) + retry? (:reload-compile-errors? tracker true) + track-fn (ns-tracker dirs) + reload! (reloader dirs retry?) + restart-fn (resolve restart-fn)] + (go-loop [] + (if (track-fn) + (do + (reload!) + (restart-fn)) + (recur))))) From 932bc4fcc52de78c97589edd641f9380911351ff Mon Sep 17 00:00:00 2001 From: gmsvalente Date: Tue, 9 Apr 2024 14:35:12 -0300 Subject: [PATCH 2/9] Fix function to pass configuration --- src/xiana/hotreload.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj index c2ebc335..e91bf3b4 100644 --- a/src/xiana/hotreload.clj +++ b/src/xiana/hotreload.clj @@ -40,4 +40,5 @@ (do (reload!) (restart-fn)) - (recur))))) + (recur))) + cfg)) From 76e332c587615262b1de8d15debb66607b768344 Mon Sep 17 00:00:00 2001 From: gmsvalente Date: Tue, 9 Apr 2024 17:47:54 -0300 Subject: [PATCH 3/9] Apply cljstyle --- src/xiana/hotreload.clj | 6 +++--- test/xiana/interceptor/kebab_camel_test.clj | 1 - test/xiana/route_test.clj | 1 - 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj index e91bf3b4..7f2a5e8e 100644 --- a/src/xiana/hotreload.clj +++ b/src/xiana/hotreload.clj @@ -1,9 +1,9 @@ (ns xiana.hotreload (:require - [clojure.core.async :refer [go-loop]] - [ns-tracker.core :refer [ns-tracker]])) + [clojure.core.async :refer [go-loop]] + [ns-tracker.core :refer [ns-tracker]])) -;;; reloader function from ring.middleware.reload +;; reloader function from ring.middleware.reload (defn- reloader [dirs retry?] (let [modified-namespaces (ns-tracker dirs) diff --git a/test/xiana/interceptor/kebab_camel_test.clj b/test/xiana/interceptor/kebab_camel_test.clj index 84680542..bda66c95 100644 --- a/test/xiana/interceptor/kebab_camel_test.clj +++ b/test/xiana/interceptor/kebab_camel_test.clj @@ -27,4 +27,3 @@ leave (:leave kc/interceptor) result (leave state)] (is (= expected result))))) - diff --git a/test/xiana/route_test.clj b/test/xiana/route_test.clj index c168c2d7..3eab0b12 100644 --- a/test/xiana/route_test.clj +++ b/test/xiana/route_test.clj @@ -97,4 +97,3 @@ expected helpers/not-found] ;; verify if action has the expected value (is (= action expected)))) - From 1c1ddfe1ebe405dad33c09873a49d8ed8cd07f69 Mon Sep 17 00:00:00 2001 From: GV <95759575+gmsvalente@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:10:02 -0300 Subject: [PATCH 4/9] Update src/xiana/hotreload.clj Co-authored-by: A. Marius Rabenarivo --- src/xiana/hotreload.clj | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj index 7f2a5e8e..381be49e 100644 --- a/src/xiana/hotreload.clj +++ b/src/xiana/hotreload.clj @@ -5,7 +5,17 @@ ;; reloader function from ring.middleware.reload -(defn- reloader [dirs retry?] +(defn- reloader +"Reload namespaces of modified files before the request is passed to the +supplied handler. + +Accepts the following options: + +:dirs - A list of directories that contain the source files. + Defaults to ["src"]. +:retry? - If true, keep attempting to reload namespaces + that have compile errors. Defaults to true." + [dirs retry?] (let [modified-namespaces (ns-tracker dirs) load-queue (java.util.concurrent.LinkedBlockingDeque.)] (fn [] From 18f59a5a227892423653e786a7e5037479185db6 Mon Sep 17 00:00:00 2001 From: gmsvalente Date: Wed, 10 Apr 2024 11:12:03 -0300 Subject: [PATCH 5/9] Apply cljstyle --- src/xiana/hotreload.clj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj index 381be49e..906f8d4c 100644 --- a/src/xiana/hotreload.clj +++ b/src/xiana/hotreload.clj @@ -6,13 +6,13 @@ ;; reloader function from ring.middleware.reload (defn- reloader -"Reload namespaces of modified files before the request is passed to the + "Reload namespaces of modified files before the request is passed to the supplied handler. Accepts the following options: :dirs - A list of directories that contain the source files. - Defaults to ["src"]. + Defaults to [" src "]. :retry? - If true, keep attempting to reload namespaces that have compile errors. Defaults to true." [dirs retry?] From 355aecbe0b01f6ed468d26d139065dbfbc2ca545 Mon Sep 17 00:00:00 2001 From: gmsvalente Date: Mon, 15 Apr 2024 10:35:06 -0300 Subject: [PATCH 6/9] Fix escaped quote at docstring --- src/xiana/hotreload.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj index 906f8d4c..b81db56c 100644 --- a/src/xiana/hotreload.clj +++ b/src/xiana/hotreload.clj @@ -12,7 +12,7 @@ supplied handler. Accepts the following options: :dirs - A list of directories that contain the source files. - Defaults to [" src "]. + Defaults to [\"src\"]. :retry? - If true, keep attempting to reload namespaces that have compile errors. Defaults to true." [dirs retry?] From 0fdbf640767d75cd47183b482b9a1a87db32a05b Mon Sep 17 00:00:00 2001 From: gmsvalente Date: Mon, 15 Apr 2024 10:42:22 -0300 Subject: [PATCH 7/9] Add timeout to hotreload --- src/xiana/hotreload.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj index b81db56c..63ea3024 100644 --- a/src/xiana/hotreload.clj +++ b/src/xiana/hotreload.clj @@ -1,6 +1,6 @@ (ns xiana.hotreload (:require - [clojure.core.async :refer [go-loop]] + [clojure.core.async :refer [go-loop Date: Mon, 15 Apr 2024 10:48:44 -0300 Subject: [PATCH 8/9] Disable hotreload if not at configuration --- src/xiana/hotreload.clj | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj index 63ea3024..1225d491 100644 --- a/src/xiana/hotreload.clj +++ b/src/xiana/hotreload.clj @@ -39,17 +39,19 @@ Accepts the following options: :restart-fn \"the function to restart the system\" if none 'user/start-dev-system :tracker {:dirs \"the directories vector list to search for changes\" :reload-compile-errors? \"true\"}." [cfg] - (let [{:keys [restart-fn tracker]} (:xiana/hotreload cfg {:restart-fn 'user/start-dev-system}) + (let [{:keys [restart-fn tracker]} (:xiana/hotreload cfg) dirs (:dirs tracker ["src"]) retry? (:reload-compile-errors? tracker true) track-fn (ns-tracker dirs) reload! (reloader dirs retry?) - restart-fn (resolve restart-fn)] - (go-loop [] - ( Date: Mon, 15 Apr 2024 19:45:08 -0300 Subject: [PATCH 9/9] Remove debug println --- src/xiana/hotreload.clj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/xiana/hotreload.clj b/src/xiana/hotreload.clj index 1225d491..65346fd0 100644 --- a/src/xiana/hotreload.clj +++ b/src/xiana/hotreload.clj @@ -48,7 +48,6 @@ Accepts the following options: (when restart-fn (go-loop [] (