Class: Watchcat::Executor
- Inherits:
-
Object
- Object
- Watchcat::Executor
- Defined in:
- lib/watchcat/executor.rb
Instance Method Summary collapse
-
#initialize(paths, recursive:, force_polling:, poll_interval:, filters:, debounce:, block:, patterns: [], ignore_patterns: [], ignore_directories: false) ⇒ Executor
constructor
A new instance of Executor.
- #start ⇒ Object
- #stop ⇒ Object
- #unwatch(paths) ⇒ Object
- #watch(paths, recursive: @recursive) ⇒ Object
- #watched ⇒ Object
Constructor Details
#initialize(paths, recursive:, force_polling:, poll_interval:, filters:, debounce:, block:, patterns: [], ignore_patterns: [], ignore_directories: false) ⇒ Executor
Returns a new instance of Executor.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/watchcat/executor.rb', line 5 def initialize(paths, recursive:, force_polling:, poll_interval:, filters:, debounce:, block:, patterns: [], ignore_patterns: [], ignore_directories: false) @paths = paths @recursive = recursive @force_polling = force_polling @poll_interval = poll_interval @filters = filters || {} @debounce = debounce @debouncer = Debouncer.new if @debounce > 0 @patterns = Array(patterns) @ignore_patterns = Array(ignore_patterns) @ignore_directories = ignore_directories @block = block @watcher = Watchcat::Watcher.new @watch_thread = nil @stop_requested = false @owner_pid = nil @stopped = false end |
Instance Method Details
#start ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/watchcat/executor.rb', line 24 def start @owner_pid = Process.pid # Always start watching in a background thread to avoid blocking @watch_thread = Thread.new do Thread.current.name = "watchcat-watcher" start_watching end at_exit do stop end end |
#stop ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/watchcat/executor.rb', line 38 def stop # A forked child inherits the at_exit hook without inheriting the watcher # thread, so it would call #close -- which sends on a channel, and a send # can allocate. If the watcher thread held the allocator lock at fork # time, that lock is never released in the child. return if @owner_pid != Process.pid return if @stopped @stopped = true @stop_requested = true @watcher.close if @watch_thread && @watch_thread.alive? @watch_thread.join(1) # Wait up to 1 second for thread to finish end end |
#unwatch(paths) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/watchcat/executor.rb', line 63 def unwatch(paths) paths = Array(paths) @watcher.unwatch(paths) @paths -= paths self end |
#watch(paths, recursive: @recursive) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/watchcat/executor.rb', line 55 def watch(paths, recursive: @recursive) paths = Array(paths) paths.each { |p| raise ArgumentError, "path does not exist: #{p}" unless File.exist?(p) } @watcher.add(paths, recursive: recursive) @paths |= paths self end |
#watched ⇒ Object
70 71 72 |
# File 'lib/watchcat/executor.rb', line 70 def watched @paths.dup end |