-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow running inotify in another process #583
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
$LOAD_PATH.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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# 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(dir, &callback) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/EmptyMethod: Put empty method definitions on a single line. |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [89/80] |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/Documentation: Missing top-level class documentation comment. |
||
def initialize(dirs_to_watch, &block) | ||
@paths = dirs_to_watch | ||
@callback = block | ||
end | ||
|
||
def run | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/MethodLength: Method has too many lines. [11/10] |
||
@pipe = IO.popen([BIN_PATH] + @paths) | ||
@running = true | ||
|
||
while @running && IO.select([@pipe], nil, nil, nil) | ||
command = @pipe.gets("\0") | ||
next unless command | ||
# remove status (M/A/D) and terminator null byte | ||
dir = command[1..-1].chomp("\0") | ||
@callback.call([dir]) | ||
end | ||
rescue Interrupt, IOError, Errno::EBADF | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint/HandleExceptions: Do not suppress exceptions. |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint/HandleExceptions: Do not suppress exceptions. |
||
ensure | ||
@running = false | ||
end | ||
|
||
def process_running?(pid) | ||
Process.kill(0, pid) | ||
true | ||
rescue Errno::ESRCH | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/Documentation: Missing top-level class documentation comment.