-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow running inotify in another process
- Loading branch information
1 parent
f186b2f
commit cdc3548
Showing
9 changed files
with
181 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
$:.unshift __dir__ + "/../lib" | ||
require "listen" | ||
|
||
$stdout.sync = true # make sure it works well with the pipe | ||
files = ARGV.to_a | ||
listener = Listen.to(*files, { prefer_fork: false }) do |modified, added, removed| | ||
modified.each { |file| $stdout.write("M", file, "\0") } | ||
added .each { |file| $stdout.write("A", file, "\0") } | ||
removed .each { |file| $stdout.write("D", file, "\0") } | ||
end | ||
listener.start | ||
sleep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
require "listen" | ||
|
||
module Listen | ||
module Adapter | ||
class ProcessLinux < Base | ||
OS_REGEXP = /linux/i | ||
BIN_PATH = ::File.expand_path(__dir__ + "/../../../bin/inotify_watch") | ||
|
||
def self.forks? | ||
true | ||
end | ||
|
||
def _configure(directory, &callback) | ||
end | ||
|
||
def _run | ||
dirs_to_watch = @callbacks.keys.map(&:to_s) | ||
worker = Worker.new(dirs_to_watch, &method(:_process_changes)) | ||
@worker_thread = Thread.new("worker_thread") { worker.run } | ||
end | ||
|
||
def _process_changes(dirs) | ||
dirs.each do |dir| | ||
dir = Pathname.new(dir.sub(%r{/$}, "")) | ||
|
||
@callbacks.each do |watched_dir, callback| | ||
if watched_dir.eql?(dir) || Listen::Directory.ascendant_of?(watched_dir, dir) | ||
callback.call(dir) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def _process_event(dir, path) | ||
Listen.logger.debug { "inotify: processing path: #{path.inspect}" } | ||
rel_path = path.relative_path_from(dir).to_s | ||
_queue_change(:dir, dir, rel_path, recursive: true) | ||
end | ||
|
||
def _stop | ||
@worker_thread&.kill | ||
super | ||
end | ||
|
||
class Worker | ||
def initialize(dirs_to_watch, &block) | ||
@paths = dirs_to_watch | ||
@callback = block | ||
end | ||
|
||
def run | ||
@pipe = IO.popen([BIN_PATH] + @paths) | ||
@running = true | ||
|
||
while @running && IO.select([@pipe], nil, nil, nil) | ||
command = @pipe.gets("\0") | ||
next unless command | ||
dir = command[1..].chomp("\0") # remove status (M/A/D) and terminator null byte | ||
@callback.call([dir]) | ||
end | ||
rescue Interrupt, IOError, Errno::EBADF | ||
ensure | ||
stop | ||
end | ||
|
||
def stop | ||
unless @pipe.nil? | ||
Process.kill("KILL", @pipe.pid) if process_running?(@pipe.pid) | ||
@pipe.close | ||
end | ||
rescue IOError, Errno::EBADF | ||
ensure | ||
@running = false | ||
end | ||
|
||
def process_running?(pid) | ||
Process.kill(0, pid) | ||
true | ||
rescue Errno::ESRCH | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'listen/adapter/process_linux' | ||
|
||
RSpec.describe Listen::Adapter::ProcessLinux do | ||
describe 'class' do | ||
subject { described_class } | ||
|
||
if linux? | ||
it { should be_usable } | ||
else | ||
it { should_not be_usable } | ||
end | ||
|
||
it '.forks? returns true' do | ||
expect(described_class.forks?).to be true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters