From 216021bd258651bd8802afe65174abfb74bd028d Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Tue, 10 May 2016 15:02:22 +0700 Subject: [PATCH] [#168] Hotfix: race in 3rd-party rotor appender --- .../timbre/appenders/3rd_party/rotor.clj | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/taoensso/timbre/appenders/3rd_party/rotor.clj b/src/taoensso/timbre/appenders/3rd_party/rotor.clj index 84979495..64e33465 100644 --- a/src/taoensso/timbre/appenders/3rd_party/rotor.clj +++ b/src/taoensso/timbre/appenders/3rd_party/rotor.clj @@ -21,7 +21,7 @@ (.listFiles (file-filter (.getName f))) seq))) -(defn- rotate-logs +(def ^:private rotate-logs "Performs log file rotation for the given files matching `basepath` and up to a maximum of `max-count`. Historical versions are suffixed with a 3-digit index, e.g. @@ -33,15 +33,18 @@ If the max number of files has been reached, the oldest one will be deleted. In future, there will be a suffix fn to customize the naming of archived logs." - [basepath max-count] - (let [abs-path (-> basepath io/file (.getAbsolutePath)) - logs (-> basepath matching-files sort) - [logs-to-rotate logs-to-delete] (split-at max-count logs)] - (doseq [log-to-delete logs-to-delete] - (io/delete-file log-to-delete)) - (doseq [[^File log-file n] - (reverse (map vector logs-to-rotate (iterate inc 1)))] - (.renameTo log-file (io/file (format "%s.%03d" abs-path n)))))) + (let [locked?_ (atom false)] + (fn [basepath max-count] + (when (compare-and-set! locked?_ false true) + (let [abs-path (-> basepath io/file (.getAbsolutePath)) + logs (-> basepath matching-files sort) + [logs-to-rotate logs-to-delete] (split-at max-count logs)] + (doseq [log-to-delete logs-to-delete] + (io/delete-file log-to-delete)) + (doseq [[^File log-file n] + (reverse (map vector logs-to-rotate (iterate inc 1)))] + (.renameTo log-file (io/file (format "%s.%03d" abs-path n))))) + (reset! locked?_ false))))) (defn rotor-appender "Returns a rotating file appender."