-
Notifications
You must be signed in to change notification settings - Fork 8
/
filesystem_watch_core.clj
221 lines (196 loc) · 9.29 KB
/
filesystem_watch_core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(ns puppetlabs.trapperkeeper.services.watcher.filesystem-watch-core
(:require [clojure.tools.logging :as log]
[me.raynes.fs :as fs]
[schema.core :as schema]
[puppetlabs.i18n.core :refer [trs]]
[puppetlabs.kitchensink.core :as ks]
[puppetlabs.trapperkeeper.services.protocols.filesystem-watch-service :refer [Event Watcher] :as watch-protocol])
(:import (clojure.lang IFn)
(java.io File)
(java.nio.file StandardWatchEventKinds Path WatchEvent WatchKey FileSystems ClosedWatchServiceException WatchService)
(com.puppetlabs DirWatchUtils)
(java.util.concurrent Future)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def event-type-mappings
{StandardWatchEventKinds/ENTRY_CREATE :create
StandardWatchEventKinds/ENTRY_MODIFY :modify
StandardWatchEventKinds/ENTRY_DELETE :delete
StandardWatchEventKinds/OVERFLOW :unknown})
(def window-min 100)
(def window-max 2000)
(def window-units java.util.concurrent.TimeUnit/MILLISECONDS)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Functions
;;;
;;; Helper functions in this namespace are heavily influenced by WatchDir.java
;;; from Java's official documentation:
;;; https://docs.oracle.com/javase/tutorial/essential/io/notification.html
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(schema/defn clojurize :- Event
"Takes the Java WatchEvent and the watchable Java Path that the event
occurred within and creates a Clojure map to represent the Event
throughout the system. The watched-path key is the registered watchable,
the changed-path the relative path to what changed, and the full path is
the absolute path to the changed event (watched-path + changed-path)."
[event :- WatchEvent
watched-path :- Path]
(let [kind (get event-type-mappings (.kind event))
count (.count event)]
(if (= :unknown kind)
{:type kind
:count count
:watched-path (.toFile watched-path)}
{:type kind
:count count
:watched-path (.toFile watched-path)
:changed-path (.. watched-path (resolve ^Path (.context event)) (toFile))})))
(defn validate-watch-options!
"Validate that the options supplied include a valid Boolean value for key :recursive."
[options]
(when-not (instance? Boolean (:recursive options))
(throw
(IllegalArgumentException.
(trs "Must pass a boolean value for :recursive (directory watching) option")))))
(def all-types #{:create :modify :delete :unknown})
(defrecord WatcherImpl
[watch-service callbacks recursive]
Watcher
(add-watch-dir!
[_this dir]
(let [watched-path (.toPath (fs/file dir))]
(if @recursive
(DirWatchUtils/registerRecursive watch-service [watched-path])
(DirWatchUtils/register watch-service watched-path))))
(add-watch-dir!
[this dir options]
(validate-watch-options! options)
;; The value of recursive was already set by `create-watcher`, possibly by specifying an option.
;; We validate that the option supplied to `add-watch-dir!` has the same value.
(log/debug
(trs "Passing options to `add-watch-dir!` is deprecated. Pass options to `create-watcher` instead"))
(when-not (= @recursive (:recursive options))
(throw
(IllegalArgumentException.
(trs "Watcher already set to :recursive {0}, cannot change to :recursive {1}"
@recursive (:recursive options)))))
(watch-protocol/add-watch-dir! this dir))
(add-file-callback!
[this path-to-file callback]
(watch-protocol/add-file-callback! this path-to-file callback all-types))
(add-file-callback!
[_this path-to-file callback types]
(swap! callbacks conj {:callback callback :file path-to-file :types (set types)}))
(add-callback!
[this callback]
(watch-protocol/add-callback! this callback all-types))
(add-callback!
[_this callback types]
(swap! callbacks conj {:callback callback :types (set types)})))
(defn create-watcher
([]
(create-watcher {:recursive true}))
([{:keys [recursive] :as options}]
(validate-watch-options! options)
(map->WatcherImpl
{:watch-service (.newWatchService (FileSystems/getDefault))
:callbacks (atom [])
:recursive (atom recursive)})))
(schema/defn watch-new-directories!
"Given an initial set of events and a watcher, identify any events that
represent the creation of a directory and register them with the watch service.
If no events are directory creations, nothing is registered."
[events :- [Event]
watcher :- (schema/protocol Watcher)]
(let [dir-create? (fn [event]
(and (= :create (:type event))
(fs/directory? (:changed-path event))))]
(DirWatchUtils/registerRecursive ^WatchService (:watch-service watcher)
(->> events
(filter dir-create?)
(map #(.toPath ^File (:changed-path %)))))))
(schema/defn watch-key->events :- [Event]
[watch-key :- WatchKey]
(let [events (.pollEvents watch-key)]
(map #(clojurize % (.watchable watch-key)) events)))
(schema/defn retrieve-events :- [Event]
"Blocks until an event the watcher is concerned with has occurred. Will then
poll for a new event, waiting at least `window-min` for a new event to
occur. Will continue polling for as long as there are new events that occur
within `window-min`, or the `window-max` time limit has been exceeded."
[watcher :- (schema/protocol Watcher)]
(let [^WatchService watch-service (:watch-service watcher)
watch-key (.take watch-service)
;; use `vec` to ensure the events sequence is correctly concatenated below.
;; Without it, the events will be out of order.
initial-events (vec (watch-key->events watch-key))
time-limit (+ (System/currentTimeMillis) window-max)
recursive @(:recursive watcher)]
(when recursive
(watch-new-directories! initial-events watcher))
(.reset watch-key)
(if-not (empty? initial-events)
(loop [events initial-events]
(if-let [waiting-key (.poll watch-service window-min window-units)]
(let [waiting-events (watch-key->events waiting-key)]
(when recursive
(watch-new-directories! waiting-events watcher))
(.reset waiting-key)
(if (< (System/currentTimeMillis) time-limit)
(recur (into events waiting-events))
(into events waiting-events)))
events))
initial-events)))
(defn filter-events
[callback-entry events]
(let [allowed-types (:types callback-entry)
event-filter (fn [event] (contains? allowed-types (:type event)))
;; construct an optimal filter function based on the type of filtering needed
filter-fn (if (nil? (:file callback-entry))
;; check the type match only
event-filter
;; check the file as well as the type match
(fn [event] (and (event-filter event)
(= (.toPath ^File (:changed-path event)) (:file callback-entry)))))]
(filter filter-fn events)))
(schema/defn process-events!
"Process for side effects any events that occurred for watcher's watch-key"
[watcher :- (schema/protocol Watcher)
events :- [Event]]
(let [callbacks @(:callbacks watcher)]
;; avoid doing a potentially expensive walk when we aren't logging at :debug
(when (log/enabled? :debug)
(let [events-by-dir (group-by :watched-path events)]
(doseq [[dir events'] events-by-dir]
(log/debug (trs "Got {0} event(s) in directory {1}"
(count events') dir)))))
;; avoid doing a potentially expensive print-to-string when we aren't logging at :trace
(when (log/enabled? :trace)
(log/tracef "%s\n%s"
(trs "Events:")
(ks/pprint-to-string events)))
(doseq [callback-entry callbacks]
((:callback callback-entry) (filter-events callback-entry events)))))
(schema/defn watch! :- Future
"Creates and returns a future. Processes events for the passed in watcher within the context of that future.
The future will continue until the underlying WatchService is closed, or the future is interrupted."
[watcher :- (schema/protocol Watcher)
shutdown-fn :- IFn
stopped? :- (schema/atom schema/Bool)]
(future
(shutdown-fn
#(while (not @stopped?)
(try
(let [events (retrieve-events watcher)]
(when-not (empty? events)
(process-events! watcher events)))
(catch ClosedWatchServiceException _e
(log/info (trs "Closing watcher {0}" watcher)))
;; it is possible for `retrieve-events` to generate an InterruptedException if the `.take` occurs when an
;; interrupt is requested. This is explicitly handled to prevent shutting down the whole application.
(catch InterruptedException _e
(log/info (trs "Watching for events interrupted by thread shutdown")))
(catch Throwable e
(log/error e (trs "Fatal error while watching for events"))
(throw e)))))))