[#174] Smarter (faster) spit appender path creation

This commit is contained in:
Peter Taoussanis 2016-07-01 14:43:43 +07:00
parent ba6bc3f79c
commit f399d250cb
1 changed files with 15 additions and 14 deletions

View File

@ -59,15 +59,6 @@
;;;; Spit appender (clj only)
#+clj
(def ^:private ensure-spit-dir-exists!
(enc/memoize* (enc/ms :mins 1)
(fn [fname]
(when-not (str/blank? fname)
(let [file (java.io.File. ^String fname)
dir (.getParentFile (.getCanonicalFile file))]
(when-not (.exists dir) (.mkdirs dir)))))))
#+clj
(defn spit-appender
"Returns a simple `spit` file appender for Clojure."
@ -78,14 +69,24 @@
:rate-limit nil
:output-fn :inherit
:fn
(fn [data]
(fn self [data]
(let [{:keys [output_]} data]
(try ; To allow TTL-memoization of dir creator
(ensure-spit-dir-exists! fname)
(try
(spit fname (str (force output_) "\n") :append true)
(catch java.io.IOException _))))})
(catch java.io.IOException e
(if (:__spit-appender/retry? data)
(throw e) ; Unexpected error
(let [_ (have? enc/nblank-str? fname)
file (java.io.File. ^String fname)
dir (.getParentFile (.getCanonicalFile file))]
(comment (spit-appender))
(when-not (.exists dir) (.mkdirs dir))
(self (assoc data :__spit-appender/retry? true))))))))})
(comment
(spit-appender)
(let [f (:fn (spit-appender))]
(enc/qb 1000 (f {:output_ "boo"}))))
;;;; js/console appender (cljs only)