Skip to content

Commit abf91ba

Browse files
committed
Watcher: logging/debugging facility, pipes watcher debug logs out to Spring application logger
1 parent c4a3ee0 commit abf91ba

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

lib/spring/application.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ def initialized?
6666

6767
def start_watcher
6868
@watcher = Spring.watcher
69-
@watcher.on_stale { state! :watcher_stale }
69+
70+
@watcher.on_stale do
71+
state! :watcher_stale
72+
end
73+
74+
if @watcher.respond_to? :on_debug
75+
@watcher.on_debug do |message|
76+
spring_env.log "[watcher:#{app_env}] #{message}"
77+
end
78+
end
79+
7080
@watcher.start
7181
end
7282

lib/spring/watcher/abstract.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,21 @@ def initialize(root, latency)
2323
@directories = Set.new
2424
@stale = false
2525
@listeners = []
26+
27+
@on_debug = nil
28+
end
29+
30+
def on_debug(&block)
31+
@on_debug = block
32+
end
33+
34+
def debug
35+
@on_debug.call(yield) if @on_debug
2636
end
2737

2838
def add(*items)
39+
debug { "watcher: add: #{items.inspect}" }
40+
2941
items = items.flatten.map do |item|
3042
item = Pathname.new(item)
3143

@@ -56,16 +68,19 @@ def stale?
5668
end
5769

5870
def on_stale(&block)
71+
debug { "added listener: #{block.inspect}" }
5972
@listeners << block
6073
end
6174

6275
def mark_stale
6376
return if stale?
6477
@stale = true
78+
debug { "marked stale, calling listeners: listeners=#{@listeners.inspect}" }
6579
@listeners.each(&:call)
6680
end
6781

6882
def restart
83+
debug { "restarting" }
6984
stop
7085
start
7186
end

lib/spring/watcher/polling.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ def initialize(root, latency)
1212
end
1313

1414
def check_stale
15-
synchronize { mark_stale if mtime < compute_mtime }
15+
synchronize do
16+
computed = compute_mtime
17+
if mtime < computed
18+
debug { "check_stale: mtime=#{mtime.inspect} < computed=#{computed.inspect}" }
19+
mark_stale
20+
end
21+
end
1622
end
1723

1824
def add(*)
@@ -21,27 +27,38 @@ def add(*)
2127
end
2228

2329
def start
30+
debug { "start: poller=#{@poller.inspect}" }
2431
unless @poller
2532
@poller = Thread.new {
2633
Thread.current.abort_on_exception = true
2734

28-
loop do
29-
Kernel.sleep latency
30-
check_stale
35+
begin
36+
loop do
37+
Kernel.sleep latency
38+
check_stale
39+
end
40+
rescue Exception => e
41+
debug do
42+
"poller: aborted: #{e.class}: #{e}\n #{e.backtrace.join("\n ")}"
43+
end
44+
raise
3145
end
3246
}
3347
end
3448
end
3549

3650
def stop
51+
debug { "stopping poller: #{@poller.inspect}" }
3752
if @poller
3853
@poller.kill
3954
@poller = nil
4055
end
4156
end
4257

4358
def subjects_changed
44-
@mtime = compute_mtime
59+
computed = compute_mtime
60+
debug { "subjects_changed: mtime #{@mtime} -> #{computed}" }
61+
@mtime = computed
4562
end
4663

4764
private

0 commit comments

Comments
 (0)