Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix 76 - change the way that target is created; rework response to quit
Browse files Browse the repository at this point in the history
and timing of sincedb write.

add failing spec

doh!

change create order of files in spec

closes 76
Guy Boertje committed Mar 4, 2016

Verified

This commit was signed with the committer’s verified signature. The key has expired.
danielleadams Danielle Adams
1 parent cf60cb4 commit 2eef32a
Showing 7 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/filewatch/observing_tail.rb
Original file line number Diff line number Diff line change
@@ -64,6 +64,8 @@ def subscribe(observer = NullObserver.new)
@logger.warn("unknown event type #{event} for #{path}")
end
end # @watch.subscribe
# when watch.subscribe ends - its because we got quit
_sincedb_write
end # def subscribe

private
8 changes: 3 additions & 5 deletions lib/filewatch/tail.rb
Original file line number Diff line number Diff line change
@@ -11,13 +11,11 @@ class Tail
attr_writer :target

def self.new_observing(opts = {})
new.tap do |instance|
instance.target = ObservingTail.new(opts)
end
new({}, ObservingTail.new(opts))
end

def initialize(opts = {})
@target = YieldingTail.new(opts)
def initialize(opts = {}, target = YieldingTail.new(opts))
@target = target
end
end
end
2 changes: 0 additions & 2 deletions lib/filewatch/tail_base.rb
Original file line number Diff line number Diff line change
@@ -209,8 +209,6 @@ def _sincedb_write
# before the instance is disposed of.
def quit
@watch.quit # <-- should close all the files
# and that should allow the sincedb_write to succeed if it could not before
_sincedb_write
end # def quit

public
16 changes: 16 additions & 0 deletions lib/filewatch/watch.rb
Original file line number Diff line number Diff line change
@@ -118,13 +118,21 @@ def each(&block)
synchronized do
return if @files.empty?

quitting = false
file_deleteable = []
# creates this array just once
watched_files = @files.values

quit_proc = lambda do
quitting.tap do |flag|
file_deleteable.each {|f| @files.delete(f)} if flag
end
end

# look at the closed to see if its changed
watched_files.select {|wf| wf.closed? }.each do |watched_file|
path = watched_file.path
break if (quitting = quit?)
begin
stat = watched_file.restat
if watched_file.size_changed? || watched_file.inode_changed?(inode(path,stat))
@@ -140,10 +148,12 @@ def each(&block)
@logger.debug? && @logger.debug("each: closed: stat failed: #{path}: (#{e.inspect})")
end
end
return if quit_proc.call

# look at the ignored to see if its changed
watched_files.select {|wf| wf.ignored? }.each do |watched_file|
path = watched_file.path
break if (quitting = quit?)
begin
stat = watched_file.restat
if watched_file.size_changed? || watched_file.inode_changed?(inode(path,stat))
@@ -166,9 +176,12 @@ def each(&block)
end
end

return if quit_proc.call

# Send any creates.
if (to_take = @max_active - watched_files.count{|wf| wf.active?}) > 0
watched_files.select {|wf| wf.watched? }.take(to_take).each do |watched_file|
break if (quitting = quit?)
watched_file.activate
# don't do create again
next if watched_file.state_history_any?(:closed, :ignored)
@@ -194,10 +207,13 @@ def each(&block)
end
end

return if quit_proc.call

# wf.active means the actual files were opened
# and have been read once - unless they were empty at the time
watched_files.select {|wf| wf.active? }.each do |watched_file|
path = watched_file.path
break if (quitting = quit?)
begin
stat = watched_file.restat
rescue Errno::ENOENT
2 changes: 2 additions & 0 deletions lib/filewatch/yielding_tail.rb
Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@ def subscribe(&block)
@logger.warn("unknown event type #{event} for #{path}")
end
end # @watch.subscribe
# when watch.subscribe ends - its because we got quit
_sincedb_write
end # def subscribe

private
11 changes: 11 additions & 0 deletions spec/observe_tail_spec.rb
Original file line number Diff line number Diff line change
@@ -21,6 +21,17 @@
FileUtils.rm_rf(sincedb_path)
end

context "when sincedb path is given but ENV[\"HOME\"] or ENV[\"SINCEDB_PATH\"] is not given" do

it "should not raise an exception" do
_home = ENV.delete("HOME")
expect do
FileWatch::Tail.new_observing(:sincedb_path => sincedb_path, :stat_interval => 0.05)
end.not_to raise_error
ENV["HOME"] = _home
end
end

context "when watching before files exist (start at end)" do
subject { FileWatch::Tail.new_observing(
:sincedb_path => sincedb_path, :stat_interval => 0.05) }
4 changes: 2 additions & 2 deletions spec/watch_spec.rb
Original file line number Diff line number Diff line change
@@ -44,8 +44,8 @@
let(:actions) do
RSpec::Sequencing
.run("create file and watch directory") do
File.open(file_path, "wb") { |file| file.write("line1\nline2\n") }
File.open(file_path2, "wb") { |file| file.write("lineA\nlineB\n") }
File.open(file_path, "wb") { |file| file.write("line1\nline2\n") }
subject.watch(watch_dir)
end
.then_after(wait_before_quit, "quit after a short time") do
@@ -87,7 +87,7 @@
it "opens both files" do
ENV["FILEWATCH_MAX_FILES_WARN_INTERVAL"] = "0.8"
subject.max_open_files = max
subject.close_older = 1 #seconds
subject.close_older = 0.75 #seconds
subscribe_proc.call
expect(results).to eq([
[:create_initial, file_path], [:modify, file_path], [:timeout, file_path],

0 comments on commit 2eef32a

Please sign in to comment.