Skip to content

Commit 02c33bb

Browse files
committed
Add some instrumentation code but keep hashing on
1 parent e1ab450 commit 02c33bb

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/lambdaisland/launchpad.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141

4242
(def verbose? (some #{"-v" "--verbose"} *command-line-args*))
4343

44-
(defn debug [& args] (when verbose? (apply println "[DEBUG]" args)))
45-
(defn info [& args] (apply println "[INFO]" args))
46-
(defn warn [& args] (apply println "[WARN]" args))
47-
(defn error [& args] (apply println "[ERROR]" args))
44+
(defn debug [& args] (when verbose? (apply println (java.util.Date.) "[DEBUG]" args)))
45+
(defn info [& args] (apply println (java.util.Date.) "[INFO]" args))
46+
(defn warn [& args] (apply println (java.util.Date.) "[WARN]" args))
47+
(defn error [& args] (apply println (java.util.Date.) "[ERROR]" args))
4848

4949
(defn shellquote [a]
5050
(cond

src/lambdaisland/launchpad/watcher.clj

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
java.nio.file.Files
1313
java.nio.file.Paths
1414
java.nio.file.Path
15-
java.io.File))
15+
java.io.File
16+
io.methvin.watcher.DirectoryWatcher))
1617

1718
(defonce watchers (atom nil))
1819

@@ -24,6 +25,27 @@
2425

2526
(require 'clojure.pprint)
2627

28+
(defn build-watcher
29+
"Creates a watcher taking a callback function `cb` that will be invoked
30+
whenever a file in one of the `paths` chages.
31+
32+
Not meant to be called directly but use `watch` or `watch-blocking` instead."
33+
[cb paths]
34+
(-> (DirectoryWatcher/builder)
35+
(.paths (map #(Path/of % (into-array String [])) paths))
36+
(.listener (#'beholder/fn->listener cb))
37+
#_(.fileHashing false)
38+
(.build)))
39+
40+
(defn watch
41+
"Creates a directory watcher that will invoke the callback function `cb` whenever
42+
a file event in one of the `paths` occurs. Watching will happen asynchronously.
43+
44+
Returns a directory watcher that can be passed to `stop` to stop the watch."
45+
[cb & paths]
46+
(doto (build-watcher cb paths)
47+
(.watchAsync)))
48+
2749
(defn watch!
2850
"Watch a number of files, takes a map from filename (string) to
2951
handler (receives a map with `:type` and `:path`, as with Beholder)."
@@ -41,14 +63,17 @@
4163
(run! beholder/stop w))
4264
(doall
4365
(for [dir directories]
44-
(beholder/watch
45-
(fn [{:keys [type path] :as event}]
46-
(if-let [f (get file->handler (str path))]
47-
(try
48-
(f event)
49-
(catch Exception e
50-
(prn e)))))
51-
(str dir))))))))
66+
(do
67+
(print dir "")
68+
(time
69+
(watch
70+
(fn [{:keys [type path] :as event}]
71+
(if-let [f (get file->handler (str path))]
72+
(try
73+
(f event)
74+
(catch Exception e
75+
(prn e)))))
76+
(str dir))))))))))
5277

5378
(comment
5479
(watch!

0 commit comments

Comments
 (0)