Class: RBWatch::Watcher
- Inherits:
-
Object
- Object
- RBWatch::Watcher
- Defined in:
- lib/rbwatch/watcher.rb
Defined Under Namespace
Classes: Event
Instance Attribute Summary collapse
-
#baseline_snapshot ⇒ Object
readonly
Returns the value of attribute baseline_snapshot.
-
#delay ⇒ Object
readonly
Returns the value of attribute delay.
-
#poll_interval ⇒ Object
readonly
Returns the value of attribute poll_interval.
Instance Method Summary collapse
-
#initialize(scanner:, delay:, poll_interval: 0.1, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) ⇒ Watcher
constructor
A new instance of Watcher.
- #reset(snapshot = @scanner.scan) ⇒ Object
- #run ⇒ Object
- #step ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
Constructor Details
#initialize(scanner:, delay:, poll_interval: 0.1, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) ⇒ Watcher
Returns a new instance of Watcher.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rbwatch/watcher.rb', line 9 def initialize(scanner:, delay:, poll_interval: 0.1, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) @scanner = scanner @delay = delay.to_f @poll_interval = poll_interval.to_f @clock = clock @baseline_snapshot = {} @last_observed_snapshot = {} @pending_change = nil @stopped = false end |
Instance Attribute Details
#baseline_snapshot ⇒ Object (readonly)
Returns the value of attribute baseline_snapshot.
7 8 9 |
# File 'lib/rbwatch/watcher.rb', line 7 def baseline_snapshot @baseline_snapshot end |
#delay ⇒ Object (readonly)
Returns the value of attribute delay.
7 8 9 |
# File 'lib/rbwatch/watcher.rb', line 7 def delay @delay end |
#poll_interval ⇒ Object (readonly)
Returns the value of attribute poll_interval.
7 8 9 |
# File 'lib/rbwatch/watcher.rb', line 7 def poll_interval @poll_interval end |
Instance Method Details
#reset(snapshot = @scanner.scan) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/rbwatch/watcher.rb', line 20 def reset(snapshot = @scanner.scan) @baseline_snapshot = snapshot.dup @last_observed_snapshot = snapshot.dup @pending_change = nil self end |
#run ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rbwatch/watcher.rb', line 81 def run return enum_for(:run) unless block_given? until stopped? if (event = step) yield event end sleep poll_interval end end |
#step ⇒ Object
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 |
# File 'lib/rbwatch/watcher.rb', line 35 def step current_snapshot = @scanner.scan now = @clock.call poll_diff = @scanner.diff(@last_observed_snapshot, current_snapshot) if poll_diff.changed? if current_snapshot == @baseline_snapshot @pending_change = nil else @pending_change ||= { first_seen_at: now } @pending_change[:change_set] = @scanner.diff(@baseline_snapshot, current_snapshot) @pending_change[:snapshot] = current_snapshot @pending_change[:last_change_at] = now end elsif @pending_change && now - @pending_change[:last_change_at] >= delay_seconds event = Event.new( change_set: @pending_change[:change_set], snapshot: @pending_change[:snapshot], first_seen_at: @pending_change[:first_seen_at], delivered_at: now ) @baseline_snapshot = @pending_change[:snapshot].dup @last_observed_snapshot = current_snapshot.dup @pending_change = nil return event end @last_observed_snapshot = current_snapshot.dup if @pending_change && now - @pending_change[:last_change_at] >= delay_seconds event = Event.new( change_set: @pending_change[:change_set], snapshot: @pending_change[:snapshot], first_seen_at: @pending_change[:first_seen_at], delivered_at: now ) @baseline_snapshot = @pending_change[:snapshot].dup @pending_change = nil event else nil end end |
#stop ⇒ Object
27 28 29 |
# File 'lib/rbwatch/watcher.rb', line 27 def stop @stopped = true end |
#stopped? ⇒ Boolean
31 32 33 |
# File 'lib/rbwatch/watcher.rb', line 31 def stopped? @stopped end |