The most user-friendly file-system watch library written in Clojure, built on top of the Java 7 WatchEvent API.
Add this to your dependencies:
[clojure-watch "LATEST"]
Check out Clojars for more specific installation instructions.
An example:
(ns clojure-watch.example
(:require [clojure-watch.core :refer [start-watch]]))
(start-watch [{:path "/some/valid/path"
:event-types [:create :modify :delete]
:bootstrap (fn [path] (println "Starting to watch " path))
:callback (fn [event filename] (println event filename))
:options {:recursive true}}])
You call start-watch
with a collection of specifications. A specification is a map with five entries:
:path
. The path to a directory that you want to watch.:event-types
. A collection of the types of events that you want to watch. Possible values include:create
,:modify
, and:delete
.:bootstrap
(optional). A function to be run only once whenstart-watch
is invoked. The function should accept one argument: the path given in the spec.:callback
. A callback function to be invoked when an event occurs. The function should accept two arguments, the first one being the type of the event that happened (:create
,:modify
, or:delete
), and the second one being the full path to the file to which the event happened.:options
. A map of options. Currently the only available option is:recursive
. If it's set to true, all sub-directories will be watched.
start-watch
returns a function that can be called to stop the watch. For example:
(let [stop-watch (start-watch [{ :path "/some/valid/path" :event-types [:create :modify :delete] :callback (fn [event filename] (println event filename))}])] ; start the watch (Thread/sleep 20000) ; manipulate files on the path (stop-watch)) ; stop the watch