logstash-input-file 4.4.7 → 4.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +2 -2
- data/lib/filewatch/stat/windows_path.rb +26 -3
- data/lib/filewatch/tail_mode/handlers/shrink.rb +4 -0
- data/lib/filewatch/tail_mode/processor.rb +1 -0
- data/lib/filewatch/watched_file.rb +7 -1
- data/lib/jars/filewatch-1.0.1.jar +0 -0
- data/logstash-input-file.gemspec +2 -2
- data/spec/filewatch/reading_spec.rb +18 -18
- data/spec/filewatch/rotate_spec.rb +45 -8
- data/spec/filewatch/sincedb_collection_spec.rb +36 -0
- data/spec/filewatch/spec_helper.rb +21 -12
- data/spec/filewatch/tailing_spec.rb +76 -32
- data/spec/filewatch/watch_spec.rb +43 -0
- data/spec/filewatch/watched_file_spec.rb +11 -0
- data/spec/filewatch/windows_path_spec.rb +63 -0
- data/spec/inputs/file_read_spec.rb +46 -63
- data/version +1 -0
- metadata +9 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1cf44bedf030b1d832d6fe95700388e94ba99e810514ec2490d9c458b4ba7ad2
|
|
4
|
+
data.tar.gz: 62034dc97d1ce6b381a113e09f466c64fd14bcea1f3c78d4d6dbaede58fe11e7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 47422dd58db7bbf779a9fd8c2416855d4e306ffa5a1e5e38d73d9a7a8ed2dbc4e16a3ba5150e642ece8c88fb1579c72845866fb23232e46f4a150a33e7188801
|
|
7
|
+
data.tar.gz: 6b5746d4ffc9812095b4fa7cffb2cb0d8e375c02ddeabc917ac489ddb7db1299a660b3d7dc691e5b1ee25678021ce0d798a63e6a70960bc0ac91624dc05503e7
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
## 4.4.8
|
|
2
|
+
- Fix create + rename rotation not being detected on Windows, which caused new content to be lost and old content to be re-read [#340](https://github.com/logstash-plugins/logstash-input-file/pull/340)
|
|
3
|
+
- Read content appended to a file before it was rotated instead of dropping it [#340](https://github.com/logstash-plugins/logstash-input-file/pull/340)
|
|
4
|
+
- Stop joining a partial line to the next line after a file is truncated [#340](https://github.com/logstash-plugins/logstash-input-file/pull/340)
|
|
5
|
+
|
|
1
6
|
## 4.4.7
|
|
2
7
|
- Re-packaging the plugin [#331](https://github.com/logstash-plugins/logstash-input-file/pull/331)
|
|
3
8
|
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Logstash Plugin
|
|
2
|
-
|
|
3
|
-
[](https://github.com/logstash-plugins/logstash-input-file/actions/workflows/unit-tests.yml)
|
|
4
4
|
|
|
5
5
|
This is a plugin for [Logstash](https://github.com/elastic/logstash).
|
|
6
6
|
|
|
@@ -1,20 +1,35 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
|
+
require "logstash/util/loggable"
|
|
2
3
|
|
|
3
4
|
module FileWatch module Stat
|
|
4
5
|
class WindowsPath
|
|
6
|
+
include LogStash::Util::Loggable
|
|
5
7
|
|
|
6
8
|
attr_reader :inode, :modified_at, :size, :inode_struct
|
|
7
9
|
|
|
8
10
|
def initialize(source)
|
|
9
11
|
@source = source # Pathname
|
|
10
|
-
@inode =
|
|
11
|
-
# in windows the dev hi and low are in the identifier
|
|
12
|
-
@inode_struct = InodeStruct.new(@inode, 0, 0)
|
|
12
|
+
@inode = nil
|
|
13
13
|
restat
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def restat
|
|
17
17
|
stat = @source.stat
|
|
18
|
+
identifier = Winhelper.identifier_from_path(@source.to_path)
|
|
19
|
+
if @inode.nil?
|
|
20
|
+
if identifier == 'unknown'
|
|
21
|
+
logger.debug("restat: file identifier lookup failed, rotation will not be detected for this file", :path => @source.to_path)
|
|
22
|
+
end
|
|
23
|
+
set_identifier(identifier)
|
|
24
|
+
elsif @inode == 'unknown'
|
|
25
|
+
# stays unknown
|
|
26
|
+
elsif identifier == 'unknown'
|
|
27
|
+
logger.debug("restat: file identifier lookup failed, keeping previous stat", :path => @source.to_path, :identifier => @inode)
|
|
28
|
+
return
|
|
29
|
+
elsif identifier != @inode
|
|
30
|
+
logger.debug("restat: file identifier changed", :path => @source.to_path, :old_identifier => @inode, :new_identifier => identifier)
|
|
31
|
+
set_identifier(identifier)
|
|
32
|
+
end
|
|
18
33
|
@modified_at = stat.mtime.to_f
|
|
19
34
|
@size = stat.size
|
|
20
35
|
end
|
|
@@ -26,5 +41,13 @@ module FileWatch module Stat
|
|
|
26
41
|
def inspect
|
|
27
42
|
"<#{self.class.name} size=#{@size}, modified_at=#{@modified_at}, inode=#{@inode}, inode_struct=#{@inode_struct}>"
|
|
28
43
|
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def set_identifier(identifier)
|
|
48
|
+
@inode = identifier
|
|
49
|
+
# in windows the dev hi and low are in the identifier
|
|
50
|
+
@inode_struct = InodeStruct.new(@inode, 0, 0)
|
|
51
|
+
end
|
|
29
52
|
end
|
|
30
53
|
end end
|
|
@@ -4,6 +4,10 @@ module FileWatch module TailMode module Handlers
|
|
|
4
4
|
class Shrink < Base
|
|
5
5
|
def handle_specifically(watched_file)
|
|
6
6
|
add_or_update_sincedb_collection(watched_file)
|
|
7
|
+
discarded = watched_file.reset_buffer
|
|
8
|
+
unless discarded.nil? || discarded.empty?
|
|
9
|
+
logger.debug("shrink: discarding partial line buffered before the file shrank", :path => watched_file.path, :bytes => discarded.bytesize)
|
|
10
|
+
end
|
|
7
11
|
watched_file.file_seek(watched_file.bytes_read)
|
|
8
12
|
loop do
|
|
9
13
|
break if quit?
|
|
@@ -141,6 +141,7 @@ module FileWatch module TailMode
|
|
|
141
141
|
logger.trace(__method__.to_s)
|
|
142
142
|
watched_files.each do |watched_file|
|
|
143
143
|
next unless watched_file.rotation_in_progress?
|
|
144
|
+
watched_file.refresh_size_from_open_file
|
|
144
145
|
if !watched_file.all_read?
|
|
145
146
|
if watched_file.file_open?
|
|
146
147
|
# rotated file but original opened file is not fully read
|
|
@@ -233,8 +233,14 @@ module FileWatch
|
|
|
233
233
|
!@file.nil? && !@file.closed?
|
|
234
234
|
end
|
|
235
235
|
|
|
236
|
+
def refresh_size_from_open_file
|
|
237
|
+
return unless file_open?
|
|
238
|
+
@size = @file.sysseek(0, IO::SEEK_END)
|
|
239
|
+
update_bytes_unread
|
|
240
|
+
end
|
|
241
|
+
|
|
236
242
|
def reset_buffer
|
|
237
|
-
@buffer.flush
|
|
243
|
+
@buffer.flush unless @buffer.nil?
|
|
238
244
|
end
|
|
239
245
|
|
|
240
246
|
def read_extract_lines(amount)
|
|
Binary file
|
data/logstash-input-file.gemspec
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Gem::Specification.new do |s|
|
|
2
2
|
|
|
3
3
|
s.name = 'logstash-input-file'
|
|
4
|
-
s.version = '
|
|
4
|
+
s.version = ::File.read('version').split("\n").first
|
|
5
5
|
s.licenses = ['Apache-2.0']
|
|
6
6
|
s.summary = "Streams events from files"
|
|
7
7
|
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
|
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
|
|
|
11
11
|
s.require_paths = ["lib"]
|
|
12
12
|
|
|
13
13
|
# Files
|
|
14
|
-
s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "JAR_VERSION", "docs/**/*"]
|
|
14
|
+
s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "version", "JAR_VERSION", "docs/**/*"]
|
|
15
15
|
|
|
16
16
|
# Tests
|
|
17
17
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
|
@@ -48,7 +48,7 @@ module FileWatch
|
|
|
48
48
|
reading.watch_this(watch_dir)
|
|
49
49
|
end
|
|
50
50
|
.then("wait") do
|
|
51
|
-
wait(2).for{listener1.
|
|
51
|
+
wait(2).for{listener1.calls_history.last}.to eq(:delete)
|
|
52
52
|
end
|
|
53
53
|
.then("quit") do
|
|
54
54
|
reading.quit
|
|
@@ -58,7 +58,7 @@ module FileWatch
|
|
|
58
58
|
actions.activate_quietly
|
|
59
59
|
reading.subscribe(observer)
|
|
60
60
|
actions.assert_no_errors
|
|
61
|
-
expect(listener1.
|
|
61
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :eof, :delete])
|
|
62
62
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
63
63
|
end
|
|
64
64
|
end
|
|
@@ -73,7 +73,7 @@ module FileWatch
|
|
|
73
73
|
reading.watch_this(watch_dir)
|
|
74
74
|
end
|
|
75
75
|
.then("wait") do
|
|
76
|
-
wait(2).for{listener1.
|
|
76
|
+
wait(2).for{listener1.calls_history.last}.to eq(:delete)
|
|
77
77
|
end
|
|
78
78
|
.then("quit") do
|
|
79
79
|
reading.quit
|
|
@@ -83,7 +83,7 @@ module FileWatch
|
|
|
83
83
|
actions.activate_quietly
|
|
84
84
|
reading.subscribe(observer)
|
|
85
85
|
actions.assert_no_errors
|
|
86
|
-
expect(listener1.
|
|
86
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :eof, :delete])
|
|
87
87
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
88
88
|
end
|
|
89
89
|
end
|
|
@@ -104,7 +104,7 @@ module FileWatch
|
|
|
104
104
|
reading.watch_this(watch_dir)
|
|
105
105
|
end
|
|
106
106
|
.then("wait") do
|
|
107
|
-
wait(2).for{listener1.
|
|
107
|
+
wait(2).for{listener1.calls_history.last == :delete && listener2.calls_history.last == :delete}.to eq(true)
|
|
108
108
|
end
|
|
109
109
|
.then("quit") do
|
|
110
110
|
reading.quit
|
|
@@ -114,8 +114,8 @@ module FileWatch
|
|
|
114
114
|
actions.activate_quietly
|
|
115
115
|
reading.subscribe(observer)
|
|
116
116
|
actions.assert_no_errors
|
|
117
|
-
expect(listener1.
|
|
118
|
-
expect(listener2.
|
|
117
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :eof, :delete])
|
|
118
|
+
expect(listener2.calls_history).to eq([:open, :accept, :accept, :eof, :delete])
|
|
119
119
|
expect(lines).to eq(%w(string1 stringA string2 stringB))
|
|
120
120
|
end
|
|
121
121
|
end
|
|
@@ -130,7 +130,7 @@ module FileWatch
|
|
|
130
130
|
reading.watch_this(watch_dir)
|
|
131
131
|
end
|
|
132
132
|
.then("wait") do
|
|
133
|
-
wait(2).for{listener1.
|
|
133
|
+
wait(2).for{listener1.calls_history.last}.to eq(:delete)
|
|
134
134
|
end
|
|
135
135
|
.then("quit") do
|
|
136
136
|
reading.quit
|
|
@@ -140,7 +140,7 @@ module FileWatch
|
|
|
140
140
|
actions.activate_quietly
|
|
141
141
|
reading.subscribe(observer)
|
|
142
142
|
actions.assert_no_errors
|
|
143
|
-
expect(listener1.
|
|
143
|
+
expect(listener1.calls_history).to eq([:open, :accept, :eof, :delete])
|
|
144
144
|
expect(listener1.lines).to eq(["line1\nline2"])
|
|
145
145
|
sincedb_record_fields = File.read(sincedb_path).split(" ")
|
|
146
146
|
position_field_index = 3
|
|
@@ -171,13 +171,13 @@ module FileWatch
|
|
|
171
171
|
reading.watch_this(watch_dir)
|
|
172
172
|
end
|
|
173
173
|
.then("wait12") do
|
|
174
|
-
wait(2).for { listener1.
|
|
174
|
+
wait(2).for { listener1.calls_history.last == :delete && listener2.calls_history.last == :delete }.to eq(true)
|
|
175
175
|
end
|
|
176
176
|
.then_after(2, "create3") do
|
|
177
177
|
File.open(file_path3, "w") { |file| file.write("string31\nstring32") }
|
|
178
178
|
end
|
|
179
179
|
.then("wait3") do
|
|
180
|
-
wait(2).for { listener3.
|
|
180
|
+
wait(2).for { listener3.calls_history.last == :delete }.to eq(true)
|
|
181
181
|
end
|
|
182
182
|
.then("quit") do
|
|
183
183
|
reading.quit
|
|
@@ -233,7 +233,7 @@ module FileWatch
|
|
|
233
233
|
File.open(file_path3, "w") { |file| file.write("line1\nline2\n") }
|
|
234
234
|
reading.watch_this(watch_dir)
|
|
235
235
|
reading.subscribe(observer)
|
|
236
|
-
expect(listener3.
|
|
236
|
+
expect(listener3.calls_history).to eq([:open, :accept, :accept, :eof, :delete, :reading_completed])
|
|
237
237
|
end
|
|
238
238
|
|
|
239
239
|
it "sincedb works correctly" do
|
|
@@ -251,8 +251,8 @@ module FileWatch
|
|
|
251
251
|
reading.subscribe(observer)
|
|
252
252
|
File.open(file_path6, "w") { |file| file.write("foob\nbar\n") }
|
|
253
253
|
expect(listener3.lines).to eq(["line1", "line2"])
|
|
254
|
-
expect(listener3.
|
|
255
|
-
expect(listener6.
|
|
254
|
+
expect(listener3.calls_history).to eq([:open, :accept, :accept, :eof, :delete, :reading_completed])
|
|
255
|
+
expect(listener6.calls_history).to eq([])
|
|
256
256
|
end
|
|
257
257
|
|
|
258
258
|
end
|
|
@@ -264,7 +264,7 @@ module FileWatch
|
|
|
264
264
|
reading.watch_this(watch_dir)
|
|
265
265
|
end
|
|
266
266
|
.then("wait") do
|
|
267
|
-
wait(1).for{listener1.
|
|
267
|
+
wait(1).for{listener1.calls_history.last}.to eq(:delete)
|
|
268
268
|
end
|
|
269
269
|
.then("quit") do
|
|
270
270
|
reading.quit
|
|
@@ -279,7 +279,7 @@ module FileWatch
|
|
|
279
279
|
actions.activate_quietly
|
|
280
280
|
reading.subscribe(observer)
|
|
281
281
|
actions.assert_no_errors
|
|
282
|
-
expect(listener1.
|
|
282
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :eof, :delete])
|
|
283
283
|
expect(listener1.lines.size).to eq(2)
|
|
284
284
|
end
|
|
285
285
|
end
|
|
@@ -293,7 +293,7 @@ module FileWatch
|
|
|
293
293
|
actions.activate_quietly
|
|
294
294
|
reading.subscribe(observer)
|
|
295
295
|
actions.assert_no_errors
|
|
296
|
-
expect(listener1.
|
|
296
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :eof, :delete])
|
|
297
297
|
expect(listener1.lines.size).to eq(2)
|
|
298
298
|
end
|
|
299
299
|
end
|
|
@@ -307,7 +307,7 @@ module FileWatch
|
|
|
307
307
|
actions.activate_quietly
|
|
308
308
|
reading.subscribe(observer)
|
|
309
309
|
actions.assert_no_errors
|
|
310
|
-
expect(listener1.
|
|
310
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :eof, :delete])
|
|
311
311
|
expect(listener1.lines.size).to eq(2)
|
|
312
312
|
end
|
|
313
313
|
end
|
|
@@ -22,7 +22,7 @@ require 'filewatch/observing_tail'
|
|
|
22
22
|
# exist, then they are renamed to app.log.2, app.log.3 etc. respectively.
|
|
23
23
|
|
|
24
24
|
module FileWatch
|
|
25
|
-
describe Watch
|
|
25
|
+
describe Watch do
|
|
26
26
|
let(:directory) { Pathname.new(Stud::Temporary.directory) }
|
|
27
27
|
let(:file1_path) { file_path.to_path }
|
|
28
28
|
let(:max) { 4095 }
|
|
@@ -74,7 +74,7 @@ module FileWatch
|
|
|
74
74
|
end
|
|
75
75
|
.then("wait for expectation") do
|
|
76
76
|
sleep(0.25) # if ENV['CI']
|
|
77
|
-
wait(2).for { listener1.
|
|
77
|
+
wait(2).for { listener1.calls_history }.to eq([:open, :accept, :accept, :accept])
|
|
78
78
|
end
|
|
79
79
|
.then("quit") do
|
|
80
80
|
tailing.quit
|
|
@@ -93,7 +93,7 @@ module FileWatch
|
|
|
93
93
|
end
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
-
context "create + rename rotation: a multiple file rename cascade" do
|
|
96
|
+
context "create + rename rotation: a multiple file rename cascade", :unix => true do
|
|
97
97
|
let(:watch_dir) { directory.join("*B.log") }
|
|
98
98
|
let(:file_path) { directory.join("1B.log") }
|
|
99
99
|
subject { described_class.new(conf) }
|
|
@@ -135,7 +135,7 @@ module FileWatch
|
|
|
135
135
|
end
|
|
136
136
|
end
|
|
137
137
|
|
|
138
|
-
context "create + rename rotation: a two file rename cascade in slow motion" do
|
|
138
|
+
context "create + rename rotation: a two file rename cascade in slow motion", :unix => true do
|
|
139
139
|
let(:watch_dir) { directory.join("*C.log") }
|
|
140
140
|
let(:file_path) { directory.join("1C.log") }
|
|
141
141
|
let(:stat_interval) { 0.01 }
|
|
@@ -179,7 +179,7 @@ module FileWatch
|
|
|
179
179
|
end
|
|
180
180
|
end
|
|
181
181
|
|
|
182
|
-
context "create + rename rotation: a two file rename cascade in normal speed" do
|
|
182
|
+
context "create + rename rotation: a two file rename cascade in normal speed", :unix => true do
|
|
183
183
|
let(:watch_dir) { directory.join("*D.log") }
|
|
184
184
|
let(:file_path) { directory.join("1D.log") }
|
|
185
185
|
subject { described_class.new(conf) }
|
|
@@ -255,11 +255,48 @@ module FileWatch
|
|
|
255
255
|
actions.assert_no_errors
|
|
256
256
|
expected_calls = ([:accept] * 66).unshift(:open)
|
|
257
257
|
expect(listener1.lines.uniq).to eq([line1])
|
|
258
|
-
expect(listener1.
|
|
258
|
+
expect(listener1.calls_history).to eq(expected_calls)
|
|
259
259
|
expect(sincedb_path.readlines.size).to eq(2)
|
|
260
260
|
end
|
|
261
261
|
end
|
|
262
262
|
|
|
263
|
+
context "create + rename rotation: when content is appended to the open file after its last stat and it is then renamed outside glob" do
|
|
264
|
+
let(:stat_interval) { 1 }
|
|
265
|
+
let(:discover_interval) { 1 }
|
|
266
|
+
let(:watch_dir) { directory.join("*G.log") }
|
|
267
|
+
let(:file_path) { directory.join("1G.log") }
|
|
268
|
+
subject { described_class.new(conf) }
|
|
269
|
+
let(:listener1) { observer.listener_for(file1_path) }
|
|
270
|
+
let(:actions) do
|
|
271
|
+
RSpec::Sequencing
|
|
272
|
+
.run_after(0.25, "create file") do
|
|
273
|
+
file_path.open("wb") { |file| file.write("#{line1}\n") }
|
|
274
|
+
end
|
|
275
|
+
.then("wait for the first line") do
|
|
276
|
+
wait(3).for { listener1.lines.size }.to eq(1)
|
|
277
|
+
end
|
|
278
|
+
.then("append and rotate between two stats") do
|
|
279
|
+
file_path.open("ab") { |file| file.write("#{line2}\n") }
|
|
280
|
+
file_path.rename(directory.join("1G.log.1"))
|
|
281
|
+
file_path.open("wb") { |file| file.write("#{line3}\n") }
|
|
282
|
+
end
|
|
283
|
+
.then("wait for expectation") do
|
|
284
|
+
wait(4).for { listener1.lines.size }.to eq(3)
|
|
285
|
+
end
|
|
286
|
+
.then("quit") do
|
|
287
|
+
tailing.quit
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
it "the appended content of the renamed file is read before the new file" do
|
|
292
|
+
actions.activate_quietly
|
|
293
|
+
tailing.watch_this(watch_dir.to_path)
|
|
294
|
+
tailing.subscribe(observer)
|
|
295
|
+
actions.assert_no_errors
|
|
296
|
+
expect(listener1.lines).to eq([line1, line2, line3])
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
263
300
|
context "copy + truncate rotation: when a logfile is copied to a new path and truncated and the open file is fully read" do
|
|
264
301
|
let(:watch_dir) { directory.join("*F.log") }
|
|
265
302
|
let(:file_path) { directory.join("1F.log") }
|
|
@@ -291,7 +328,7 @@ module FileWatch
|
|
|
291
328
|
tailing.subscribe(observer)
|
|
292
329
|
actions.assert_no_errors
|
|
293
330
|
expect(listener1.lines).to eq([line1, line2, line3])
|
|
294
|
-
expect(listener1.
|
|
331
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :accept])
|
|
295
332
|
end
|
|
296
333
|
end
|
|
297
334
|
|
|
@@ -333,7 +370,7 @@ module FileWatch
|
|
|
333
370
|
end
|
|
334
371
|
end
|
|
335
372
|
|
|
336
|
-
context "? rotation: when an active file is renamed inside the glob and the reading does not lag" do
|
|
373
|
+
context "? rotation: when an active file is renamed inside the glob and the reading does not lag", :unix => true do
|
|
337
374
|
let(:watch_dir) { directory.join("*H.log") }
|
|
338
375
|
let(:file_path) { directory.join("1H.log") }
|
|
339
376
|
let(:file2) { directory.join("2H.log") }
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'stud/temporary'
|
|
3
|
+
require_relative 'spec_helper'
|
|
4
|
+
require 'filewatch/sincedb_collection'
|
|
5
|
+
|
|
6
|
+
module FileWatch
|
|
7
|
+
describe SincedbCollection do
|
|
8
|
+
let(:sincedb_path) { Stud::Temporary.pathname }
|
|
9
|
+
let(:now) { Time.now.to_f }
|
|
10
|
+
let(:expired_key) { InodeStruct.new('expired', 1, 1) }
|
|
11
|
+
let(:active_key) { InodeStruct.new('active', 1, 1) }
|
|
12
|
+
let(:settings) do
|
|
13
|
+
Settings.from_options(
|
|
14
|
+
:sincedb_path => sincedb_path,
|
|
15
|
+
:sincedb_clean_after => 3600,
|
|
16
|
+
:sincedb_write_interval => 0
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
subject(:collection) { described_class.new(settings) }
|
|
21
|
+
|
|
22
|
+
before do
|
|
23
|
+
collection.set(expired_key, SincedbValue.new(42, now - 7200))
|
|
24
|
+
collection.set(active_key, SincedbValue.new(99, now))
|
|
25
|
+
collection.flush_at_interval
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'removes expired entries from the collection' do
|
|
29
|
+
expect(collection.keys).to contain_exactly(active_key)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'does not persist expired entries' do
|
|
33
|
+
expect(File.read(sincedb_path)).to eq("#{active_key} 99 #{now}\n")
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -117,47 +117,56 @@ module FileWatch
|
|
|
117
117
|
|
|
118
118
|
class TestObserver
|
|
119
119
|
class Listener
|
|
120
|
-
attr_reader :path, :lines, :
|
|
120
|
+
attr_reader :path, :lines, :calls_history
|
|
121
121
|
|
|
122
|
-
def initialize(path, lines)
|
|
122
|
+
def initialize(path, lines, notification_queue)
|
|
123
123
|
@path = path
|
|
124
124
|
@lines = lines || Concurrent::Array.new
|
|
125
|
-
@
|
|
125
|
+
@calls_history = Concurrent::Array.new
|
|
126
|
+
@notification_queue = notification_queue
|
|
126
127
|
end
|
|
127
128
|
|
|
128
129
|
def accept(line)
|
|
129
130
|
@lines << line
|
|
130
|
-
|
|
131
|
+
record(:accept)
|
|
131
132
|
end
|
|
132
133
|
|
|
133
134
|
def deleted
|
|
134
|
-
|
|
135
|
+
record(:delete)
|
|
135
136
|
end
|
|
136
137
|
|
|
137
138
|
def opened
|
|
138
|
-
|
|
139
|
+
record(:open)
|
|
139
140
|
end
|
|
140
141
|
|
|
141
142
|
def error
|
|
142
|
-
|
|
143
|
+
record(:error)
|
|
143
144
|
end
|
|
144
145
|
|
|
145
146
|
def eof
|
|
146
|
-
|
|
147
|
+
record(:eof)
|
|
147
148
|
end
|
|
148
149
|
|
|
149
150
|
def timed_out
|
|
150
|
-
|
|
151
|
+
record(:timed_out)
|
|
151
152
|
end
|
|
152
153
|
|
|
153
154
|
def reading_completed
|
|
154
|
-
|
|
155
|
+
record(:reading_completed)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
def record(event)
|
|
161
|
+
@calls_history << event
|
|
162
|
+
@notification_queue << [@path, event] unless @notification_queue.nil?
|
|
155
163
|
end
|
|
156
164
|
end
|
|
157
165
|
|
|
158
166
|
attr_reader :listeners
|
|
159
167
|
|
|
160
|
-
def initialize(combined_lines = nil)
|
|
168
|
+
def initialize(combined_lines = nil, notification_queue = nil)
|
|
169
|
+
@notification_queue = notification_queue
|
|
161
170
|
@listeners = Concurrent::Hash.new { |hash, key| hash[key] = new_listener(key, combined_lines) }
|
|
162
171
|
end
|
|
163
172
|
|
|
@@ -172,7 +181,7 @@ module FileWatch
|
|
|
172
181
|
private
|
|
173
182
|
|
|
174
183
|
def new_listener(path, lines = nil)
|
|
175
|
-
Listener.new(path, lines)
|
|
184
|
+
Listener.new(path, lines, @notification_queue)
|
|
176
185
|
end
|
|
177
186
|
|
|
178
187
|
end
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# encoding: utf-8
|
|
2
2
|
require 'stud/temporary'
|
|
3
|
+
require 'timeout'
|
|
3
4
|
require_relative 'spec_helper'
|
|
4
5
|
require 'filewatch/observing_tail'
|
|
5
6
|
|
|
@@ -43,14 +44,24 @@ module FileWatch
|
|
|
43
44
|
|
|
44
45
|
describe "max open files (set to 1)" do
|
|
45
46
|
let(:max) { 1 }
|
|
46
|
-
let(:wait_before_quit) { 0.15 }
|
|
47
47
|
let(:stat_interval) { 0.01 }
|
|
48
48
|
let(:discover_interval) { 4 }
|
|
49
49
|
let(:start_new_files_at) { :beginning }
|
|
50
|
+
let(:listener_notification_queue) { Queue.new }
|
|
51
|
+
let(:observer) { TestObserver.new(nil, listener_notification_queue) }
|
|
52
|
+
let(:completion_listener) { listener1 }
|
|
53
|
+
let(:expected_calls_history) { [:open, :accept, :accept] }
|
|
50
54
|
let(:actions) do
|
|
51
55
|
RSpec::Sequencing
|
|
52
|
-
.
|
|
53
|
-
|
|
56
|
+
.run("quit after expected listener calls") do
|
|
57
|
+
begin
|
|
58
|
+
Timeout.timeout(30) do
|
|
59
|
+
# Block on listener notifications until the complete call history matches
|
|
60
|
+
listener_notification_queue.pop until completion_listener.calls_history == expected_calls_history
|
|
61
|
+
end
|
|
62
|
+
ensure
|
|
63
|
+
tailing.quit
|
|
64
|
+
end
|
|
54
65
|
end
|
|
55
66
|
end
|
|
56
67
|
|
|
@@ -70,24 +81,25 @@ module FileWatch
|
|
|
70
81
|
actions.assert_no_errors
|
|
71
82
|
expect(tailing.settings.max_active).to eq(max)
|
|
72
83
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
73
|
-
expect(listener1.
|
|
74
|
-
expect(listener2.
|
|
84
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept])
|
|
85
|
+
expect(listener2.calls_history).to be_empty
|
|
75
86
|
end
|
|
76
87
|
end
|
|
77
88
|
|
|
78
89
|
context "when close_older is set" do
|
|
79
|
-
let(:wait_before_quit) { 0.8 }
|
|
80
90
|
let(:opts) { super().merge(:close_older => 0.1, :max_open_files => 1, :stat_interval => 0.1) }
|
|
81
91
|
let(:suffix) { "B" }
|
|
92
|
+
let(:completion_listener) { listener2 }
|
|
93
|
+
let(:expected_calls_history) { [:open, :accept, :accept, :timed_out] }
|
|
82
94
|
it "opens both files" do
|
|
83
95
|
actions.activate_quietly
|
|
84
96
|
tailing.watch_this(watch_dir)
|
|
85
97
|
tailing.subscribe(observer)
|
|
86
98
|
actions.assert_no_errors
|
|
87
99
|
expect(tailing.settings.max_active).to eq(1)
|
|
88
|
-
expect(listener2.
|
|
100
|
+
expect(listener2.calls_history).to eq([:open, :accept, :accept, :timed_out])
|
|
89
101
|
expect(listener2.lines).to eq(["line-A", "line-B"])
|
|
90
|
-
expect(listener1.
|
|
102
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :timed_out])
|
|
91
103
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
92
104
|
end
|
|
93
105
|
end
|
|
@@ -118,7 +130,7 @@ module FileWatch
|
|
|
118
130
|
actions.activate_quietly
|
|
119
131
|
tailing.subscribe(observer)
|
|
120
132
|
actions.assert_no_errors
|
|
121
|
-
expect(listener1.
|
|
133
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept])
|
|
122
134
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
123
135
|
end
|
|
124
136
|
end
|
|
@@ -145,7 +157,7 @@ module FileWatch
|
|
|
145
157
|
actions.activate_quietly
|
|
146
158
|
tailing.subscribe(observer)
|
|
147
159
|
actions.assert_no_errors
|
|
148
|
-
expect(listener1.
|
|
160
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept])
|
|
149
161
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
150
162
|
end
|
|
151
163
|
end
|
|
@@ -171,7 +183,7 @@ module FileWatch
|
|
|
171
183
|
RSpec::Sequencing.run_after(quit_after, "quit") { tailing.quit }
|
|
172
184
|
tailing.subscribe(observer)
|
|
173
185
|
expect(tailing.watch.watched_files_collection).to be_empty
|
|
174
|
-
expect(listener1.
|
|
186
|
+
expect(listener1.calls_history).to eq([:delete])
|
|
175
187
|
end
|
|
176
188
|
end
|
|
177
189
|
|
|
@@ -181,7 +193,7 @@ module FileWatch
|
|
|
181
193
|
RSpec::Sequencing.run_after(quit_after, "quit") { tailing.quit }
|
|
182
194
|
tailing.subscribe(observer)
|
|
183
195
|
expect(tailing.watch.watched_files_collection).to be_empty
|
|
184
|
-
expect(listener1.
|
|
196
|
+
expect(listener1.calls_history).to eq([:delete])
|
|
185
197
|
end
|
|
186
198
|
end
|
|
187
199
|
|
|
@@ -191,7 +203,7 @@ module FileWatch
|
|
|
191
203
|
RSpec::Sequencing.run_after(quit_after, "quit") { tailing.quit }
|
|
192
204
|
tailing.subscribe(observer)
|
|
193
205
|
expect(tailing.watch.watched_files_collection).to be_empty
|
|
194
|
-
expect(listener1.
|
|
206
|
+
expect(listener1.calls_history).to eq([:delete])
|
|
195
207
|
end
|
|
196
208
|
end
|
|
197
209
|
|
|
@@ -201,7 +213,7 @@ module FileWatch
|
|
|
201
213
|
RSpec::Sequencing.run_after(quit_after, "quit") { tailing.quit }
|
|
202
214
|
tailing.subscribe(observer)
|
|
203
215
|
expect(tailing.watch.watched_files_collection).to be_empty
|
|
204
|
-
expect(listener1.
|
|
216
|
+
expect(listener1.calls_history).to eq([:delete])
|
|
205
217
|
end
|
|
206
218
|
end
|
|
207
219
|
end
|
|
@@ -235,11 +247,43 @@ module FileWatch
|
|
|
235
247
|
actions.activate_quietly
|
|
236
248
|
tailing.subscribe(observer)
|
|
237
249
|
actions.assert_no_errors
|
|
238
|
-
expect(listener1.
|
|
250
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :accept, :accept, :accept, :accept])
|
|
239
251
|
expect(listener1.lines).to eq(["line1", "line2", "line3", "line4", "lineA", "lineB"])
|
|
240
252
|
end
|
|
241
253
|
end
|
|
242
254
|
|
|
255
|
+
context "when a processed file with a partial last line shrinks" do
|
|
256
|
+
let(:discover_interval) { 1 }
|
|
257
|
+
let(:suffix) { "P" }
|
|
258
|
+
let(:actions) do
|
|
259
|
+
RSpec::Sequencing
|
|
260
|
+
.run_after(0.1, "start watching") do
|
|
261
|
+
tailing.watch_this(watch_dir)
|
|
262
|
+
end
|
|
263
|
+
.then_after(0.1, "create file") do
|
|
264
|
+
File.open(file_path, "wb") { |file| file.write("line1\nline2\nline3\npartial") }
|
|
265
|
+
end
|
|
266
|
+
.then("wait for initial lines to be read") do
|
|
267
|
+
wait(0.8).for{listener1.lines.size}.to eq(3), "listener1.lines.size not eq 3"
|
|
268
|
+
end
|
|
269
|
+
.then_after(0.25, "truncate file and write new content") do
|
|
270
|
+
File.truncate(file_path, 0)
|
|
271
|
+
File.open(file_path, "ab") { |file| file.write("lineA\nlineB\n") }
|
|
272
|
+
wait(0.5).for{listener1.lines.size}.to eq(5), "listener1.lines.size not eq 5"
|
|
273
|
+
end
|
|
274
|
+
.then("quit") do
|
|
275
|
+
tailing.quit
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
it "the partial line is not joined to the first line after the shrink" do
|
|
280
|
+
actions.activate_quietly
|
|
281
|
+
tailing.subscribe(observer)
|
|
282
|
+
actions.assert_no_errors
|
|
283
|
+
expect(listener1.lines).to eq(["line1", "line2", "line3", "lineA", "lineB"])
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
|
|
243
287
|
context "when watching a directory with files and a file is renamed to not match glob", :unix => true do
|
|
244
288
|
let(:suffix) { "G" }
|
|
245
289
|
let(:new_file_path) { file_path + ".old" }
|
|
@@ -269,9 +313,9 @@ module FileWatch
|
|
|
269
313
|
actions.activate_quietly
|
|
270
314
|
tailing.subscribe(observer)
|
|
271
315
|
actions.assert_no_errors
|
|
272
|
-
expect(listener1.
|
|
316
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept, :delete])
|
|
273
317
|
expect(listener1.lines).to eq(["line1", "line2"])
|
|
274
|
-
expect(new_file_listener.
|
|
318
|
+
expect(new_file_listener.calls_history).to eq([])
|
|
275
319
|
expect(new_file_listener.lines).to eq([])
|
|
276
320
|
end
|
|
277
321
|
end
|
|
@@ -290,7 +334,7 @@ module FileWatch
|
|
|
290
334
|
tailing.watch_this(watch_dir)
|
|
291
335
|
end
|
|
292
336
|
.then("wait") do
|
|
293
|
-
wait(0.5).for{listener1.
|
|
337
|
+
wait(0.5).for{listener1.calls_history.last}.to eq(:timed_out)
|
|
294
338
|
end
|
|
295
339
|
.then("rename file") do
|
|
296
340
|
FileUtils.mv(file_path, file_path2)
|
|
@@ -311,9 +355,9 @@ module FileWatch
|
|
|
311
355
|
tailing.subscribe(observer)
|
|
312
356
|
actions.assert_no_errors
|
|
313
357
|
expect(listener1.lines).to eq([])
|
|
314
|
-
expect(listener1.
|
|
358
|
+
expect(listener1.calls_history).to eq([:open, :timed_out, :delete])
|
|
315
359
|
expect(listener2.lines).to eq(["line3", "line4"])
|
|
316
|
-
expect(listener2.
|
|
360
|
+
expect(listener2.calls_history).to eq([:open, :accept, :accept, :timed_out])
|
|
317
361
|
end
|
|
318
362
|
end
|
|
319
363
|
|
|
@@ -340,7 +384,7 @@ module FileWatch
|
|
|
340
384
|
actions.activate_quietly
|
|
341
385
|
tailing.subscribe(observer)
|
|
342
386
|
actions.assert_no_errors
|
|
343
|
-
expect(listener1.
|
|
387
|
+
expect(listener1.calls_history).to eq([:open, :accept, :accept])
|
|
344
388
|
expect(listener1.lines).to eq(["line3", "line4"])
|
|
345
389
|
end
|
|
346
390
|
end
|
|
@@ -354,7 +398,7 @@ module FileWatch
|
|
|
354
398
|
end
|
|
355
399
|
.then("watch and wait") do
|
|
356
400
|
tailing.watch_this(watch_dir)
|
|
357
|
-
wait(1.25).for{listener1.
|
|
401
|
+
wait(1.25).for{listener1.calls_history}.to eq([:open, :timed_out])
|
|
358
402
|
end
|
|
359
403
|
.then("quit") do
|
|
360
404
|
tailing.quit
|
|
@@ -381,16 +425,16 @@ module FileWatch
|
|
|
381
425
|
File.open(file_path, "wb") { |file| file.write("line1\nline2\n") }
|
|
382
426
|
end
|
|
383
427
|
.then("wait for file to be read") do
|
|
384
|
-
wait(0.5).for{listener1.
|
|
428
|
+
wait(0.5).for{listener1.calls_history}.to eq([:open, :accept, :accept]), "file is not read"
|
|
385
429
|
end
|
|
386
430
|
.then("wait for file to be read and time out") do
|
|
387
|
-
wait(0.75).for{listener1.
|
|
431
|
+
wait(0.75).for{listener1.calls_history}.to eq([:open, :accept, :accept, :timed_out]), "file did not timeout the first time"
|
|
388
432
|
end
|
|
389
433
|
.then("append more lines to file after file ages more than close_older") do
|
|
390
434
|
File.open(file_path, "ab") { |file| file.write("line3\nline4\n") }
|
|
391
435
|
end
|
|
392
436
|
.then("wait for last timeout") do
|
|
393
|
-
wait(0.75).for{listener1.
|
|
437
|
+
wait(0.75).for{listener1.calls_history}.to eq([:open, :accept, :accept, :timed_out, :open, :accept, :accept, :timed_out]), "file did not timeout the second time"
|
|
394
438
|
end
|
|
395
439
|
.then("quit") do
|
|
396
440
|
tailing.quit
|
|
@@ -423,7 +467,7 @@ module FileWatch
|
|
|
423
467
|
it "no files are read" do
|
|
424
468
|
actions.activate_quietly
|
|
425
469
|
tailing.subscribe(observer)
|
|
426
|
-
expect(listener1.
|
|
470
|
+
expect(listener1.calls_history).to eq([])
|
|
427
471
|
expect(listener1.lines).to eq([])
|
|
428
472
|
end
|
|
429
473
|
end
|
|
@@ -451,8 +495,8 @@ module FileWatch
|
|
|
451
495
|
end
|
|
452
496
|
.then("wait") do
|
|
453
497
|
wait(4).for do
|
|
454
|
-
listener1.lines.size == 32 && listener2.
|
|
455
|
-
end.to eq(true), "listener1.lines != 32 or listener2.
|
|
498
|
+
listener1.lines.size == 32 && listener2.calls_history == [:delete] && listener3.calls_history == [:open, :accept, :timed_out]
|
|
499
|
+
end.to eq(true), "listener1.lines != 32 or listener2.calls_history != [:delete] or listener3.calls_history != [:open, :accept, :timed_out]"
|
|
456
500
|
end
|
|
457
501
|
.then("quit") do
|
|
458
502
|
tailing.quit
|
|
@@ -481,7 +525,7 @@ module FileWatch
|
|
|
481
525
|
tailing.watch_this(watch_dir)
|
|
482
526
|
end
|
|
483
527
|
.then("wait for lines") do
|
|
484
|
-
wait(1.5).for{listener1.
|
|
528
|
+
wait(1.5).for{listener1.calls_history}.to eq([:open, :accept, :accept, :timed_out])
|
|
485
529
|
end
|
|
486
530
|
.then("quit") do
|
|
487
531
|
tailing.quit
|
|
@@ -516,7 +560,7 @@ module FileWatch
|
|
|
516
560
|
it "no files are read" do
|
|
517
561
|
actions.activate_quietly
|
|
518
562
|
tailing.subscribe(observer)
|
|
519
|
-
expect(listener1.
|
|
563
|
+
expect(listener1.calls_history).to eq([])
|
|
520
564
|
expect(listener1.lines).to eq([])
|
|
521
565
|
end
|
|
522
566
|
end
|
|
@@ -535,7 +579,7 @@ module FileWatch
|
|
|
535
579
|
File.open(file_path, "ab") { |file| file.write("line3\nline4\n") }
|
|
536
580
|
end
|
|
537
581
|
.then("wait for lines") do
|
|
538
|
-
wait(2).for{listener1.
|
|
582
|
+
wait(2).for{listener1.calls_history}.to eq([:open, :accept, :accept, :timed_out])
|
|
539
583
|
end
|
|
540
584
|
.then_after(0.1, "quit after allowing time to close the file") do
|
|
541
585
|
tailing.quit
|
|
@@ -562,7 +606,7 @@ module FileWatch
|
|
|
562
606
|
File.open(file_path, "wb") { |file| file.write("line1\nline2") }
|
|
563
607
|
end
|
|
564
608
|
.then("wait for :timeout") do
|
|
565
|
-
wait(2).for{listener1.
|
|
609
|
+
wait(2).for{listener1.calls_history}.to eq([:open, :timed_out])
|
|
566
610
|
end
|
|
567
611
|
.then_after(0.75, "quit after allowing time to close the file") do
|
|
568
612
|
tailing.quit
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'timeout'
|
|
3
|
+
require_relative 'spec_helper'
|
|
4
|
+
require 'filewatch/watch'
|
|
5
|
+
|
|
6
|
+
module FileWatch
|
|
7
|
+
describe Watch do
|
|
8
|
+
let(:watched_files) { double('watched files', :empty? => true, :close_all => nil) }
|
|
9
|
+
let(:discoverer) { double('discoverer', :watched_files_collection => watched_files, :discover => nil) }
|
|
10
|
+
let(:processor) { double('processor', :add_watch => nil, :initialize_handlers => nil) }
|
|
11
|
+
let(:settings) do
|
|
12
|
+
double(
|
|
13
|
+
'settings',
|
|
14
|
+
:discover_interval => 2,
|
|
15
|
+
:exit_after_read => false,
|
|
16
|
+
:stat_interval => 0
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
let(:sincedb_collection) { double('sincedb collection', :write_if_requested => nil, :flush_at_interval => nil) }
|
|
20
|
+
let(:observer) { double('observer') }
|
|
21
|
+
|
|
22
|
+
subject(:watch) { described_class.new(discoverer, processor, settings) }
|
|
23
|
+
|
|
24
|
+
it 'flushes sincedb after a sleep interval' do
|
|
25
|
+
sleep_started = Queue.new
|
|
26
|
+
allow(watch).to receive(:sleep) { sleep_started << true if sleep_started.empty? }
|
|
27
|
+
|
|
28
|
+
run_thread = Thread.new do
|
|
29
|
+
Thread.current.abort_on_exception = true
|
|
30
|
+
watch.subscribe(observer, sincedb_collection)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
begin
|
|
34
|
+
Timeout.timeout(30) { sleep_started.pop }
|
|
35
|
+
ensure
|
|
36
|
+
watch.quit
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
expect(run_thread.join(30)).not_to be_nil
|
|
40
|
+
expect(sincedb_collection).to have_received(:flush_at_interval).at_least(:once)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -64,6 +64,17 @@ module FileWatch
|
|
|
64
64
|
expect( file.modified_at ).to eql mtime # until updated
|
|
65
65
|
expect( file.modified_at(true) ).to be > mtime
|
|
66
66
|
end
|
|
67
|
+
|
|
68
|
+
it 'detects rotation when the path is renamed and a new file created in its place' do
|
|
69
|
+
File.write(file_path, "line1\nline2\nline3\n")
|
|
70
|
+
file = WatchedFile.new(pathname, PathStatClass.new(pathname), Settings.new)
|
|
71
|
+
original_key = file.sincedb_key
|
|
72
|
+
File.rename(file_path, "#{file_path}.1")
|
|
73
|
+
File.write(file_path, "lineA\n")
|
|
74
|
+
expect( file.restat! ).to be_truthy
|
|
75
|
+
expect( file.rotation_in_progress? ).to be true
|
|
76
|
+
expect( file.stat_sincedb_key ).not_to eq(original_key)
|
|
77
|
+
end
|
|
67
78
|
end
|
|
68
79
|
end
|
|
69
80
|
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'stud/temporary'
|
|
3
|
+
require_relative 'spec_helper'
|
|
4
|
+
require_relative '../../lib/filewatch/stat/windows_path'
|
|
5
|
+
|
|
6
|
+
module FileWatch
|
|
7
|
+
describe Stat::WindowsPath do
|
|
8
|
+
let(:directory) { Stud::Temporary.directory }
|
|
9
|
+
let(:file_path) { ::File.join(directory, "windows_path.log") }
|
|
10
|
+
let(:pathname) { Pathname.new(file_path) }
|
|
11
|
+
let(:winhelper) { double("Winhelper") }
|
|
12
|
+
let(:logger) { double("logger", :debug => nil) }
|
|
13
|
+
|
|
14
|
+
before do
|
|
15
|
+
stub_const("Winhelper", winhelper)
|
|
16
|
+
allow(described_class).to receive(:logger).and_return(logger)
|
|
17
|
+
File.write(file_path, "line1\nline2\n")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
after { FileUtils.rm_rf(directory) }
|
|
21
|
+
|
|
22
|
+
it "picks up a new identifier on restat" do
|
|
23
|
+
allow(winhelper).to receive(:identifier_from_path).and_return("1-2-3", "1-2-4")
|
|
24
|
+
stat = described_class.new(pathname)
|
|
25
|
+
File.write(file_path, "lineA\n")
|
|
26
|
+
stat.restat
|
|
27
|
+
expect(stat.inode).to eq("1-2-4")
|
|
28
|
+
expect(stat.inode_struct).to eq(InodeStruct.new("1-2-4", 0, 0))
|
|
29
|
+
expect(stat.size).to eq(6)
|
|
30
|
+
expect(logger).to have_received(:debug).with(/identifier changed/, hash_including(:old_identifier => "1-2-3", :new_identifier => "1-2-4"))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "does not log when the identifier is unchanged" do
|
|
34
|
+
allow(winhelper).to receive(:identifier_from_path).and_return("1-2-3")
|
|
35
|
+
stat = described_class.new(pathname)
|
|
36
|
+
File.write(file_path, "lineA\n")
|
|
37
|
+
stat.restat
|
|
38
|
+
expect(stat.inode).to eq("1-2-3")
|
|
39
|
+
expect(stat.size).to eq(6)
|
|
40
|
+
expect(logger).not_to have_received(:debug)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "keeps the previous snapshot when the lookup fails after a successful one" do
|
|
44
|
+
allow(winhelper).to receive(:identifier_from_path).and_return("1-2-3", "unknown")
|
|
45
|
+
stat = described_class.new(pathname)
|
|
46
|
+
File.write(file_path, "lineA\n")
|
|
47
|
+
stat.restat
|
|
48
|
+
expect(stat.inode).to eq("1-2-3")
|
|
49
|
+
expect(stat.size).to eq(12)
|
|
50
|
+
expect(logger).to have_received(:debug).with(/keeping previous stat/, hash_including(:identifier => "1-2-3"))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "stays unknown when the first lookup fails" do
|
|
54
|
+
allow(winhelper).to receive(:identifier_from_path).and_return("unknown", "1-2-3")
|
|
55
|
+
stat = described_class.new(pathname)
|
|
56
|
+
File.write(file_path, "line1\nline2\nline3\n")
|
|
57
|
+
stat.restat
|
|
58
|
+
expect(stat.inode).to eq("unknown")
|
|
59
|
+
expect(stat.size).to eq(18)
|
|
60
|
+
expect(logger).to have_received(:debug).with(/rotation will not be detected/, anything).once
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -7,6 +7,7 @@ require "logstash/inputs/file"
|
|
|
7
7
|
|
|
8
8
|
require "tempfile"
|
|
9
9
|
require "stud/temporary"
|
|
10
|
+
require "timecop"
|
|
10
11
|
require "logstash/codecs/multiline"
|
|
11
12
|
|
|
12
13
|
describe LogStash::Inputs::File do
|
|
@@ -293,24 +294,40 @@ describe LogStash::Inputs::File do
|
|
|
293
294
|
super().merge({ 'file_completed_action' => "delete", 'exit_after_read' => false })
|
|
294
295
|
end
|
|
295
296
|
|
|
297
|
+
let(:file_completions) { Queue.new }
|
|
298
|
+
let(:sincedb_flush_notifications) { Queue.new }
|
|
299
|
+
|
|
296
300
|
let(:sample_file) { File.join(temp_directory, "sample.log") }
|
|
297
301
|
|
|
298
302
|
before do
|
|
299
303
|
plugin.register
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
304
|
+
allow_any_instance_of(FileWatch::SincedbCollection).to receive(:flush_at_interval).and_wrap_original do |original|
|
|
305
|
+
result = original.call
|
|
306
|
+
sincedb_flush_notifications << true
|
|
307
|
+
result
|
|
308
|
+
end
|
|
309
|
+
allow(plugin).to receive(:handle_deletable_path).and_wrap_original do |original, path|
|
|
310
|
+
original.call(path)
|
|
311
|
+
file_completions << [path, File.exist?(path)]
|
|
303
312
|
end
|
|
304
313
|
|
|
305
314
|
File.open(sample_file, 'w') { |fd| fd.write("sample-content\n") }
|
|
306
315
|
|
|
307
|
-
|
|
316
|
+
@run_thread = Thread.new(plugin) do |plugin|
|
|
317
|
+
Thread.current.abort_on_exception = true
|
|
318
|
+
plugin.run queue
|
|
319
|
+
end
|
|
308
320
|
end
|
|
309
321
|
|
|
310
|
-
after
|
|
322
|
+
after do
|
|
323
|
+
plugin.stop
|
|
324
|
+
unless @run_thread.join(30)
|
|
325
|
+
fail "run thread did not exit within 30 seconds"
|
|
326
|
+
end
|
|
327
|
+
end
|
|
311
328
|
|
|
312
329
|
it 'processes a file' do
|
|
313
|
-
|
|
330
|
+
wait_for_file_completion(sample_file)
|
|
314
331
|
|
|
315
332
|
expect( plugin.queue.size ).to eql 1
|
|
316
333
|
event = plugin.queue.pop
|
|
@@ -318,78 +335,44 @@ describe LogStash::Inputs::File do
|
|
|
318
335
|
end
|
|
319
336
|
|
|
320
337
|
it 'removes watched file from collection' do
|
|
321
|
-
|
|
322
|
-
sleep(0.25) # give CI some space to execute the removal
|
|
323
|
-
# TODO shouldn't be necessary once WatchedFileCollection does proper locking
|
|
338
|
+
wait_for_file_completion(sample_file)
|
|
324
339
|
watched_files = plugin.watcher.watch.watched_files_collection
|
|
325
340
|
expect( watched_files ).to be_empty
|
|
326
341
|
end
|
|
327
|
-
end
|
|
328
|
-
|
|
329
|
-
describe 'sincedb cleanup' do
|
|
330
|
-
|
|
331
|
-
let(:options) do
|
|
332
|
-
super().merge(
|
|
333
|
-
'sincedb_path' => sincedb_path,
|
|
334
|
-
'sincedb_clean_after' => '1.0 seconds',
|
|
335
|
-
'sincedb_write_interval' => 0.25,
|
|
336
|
-
'stat_interval' => 0.1,
|
|
337
|
-
)
|
|
338
|
-
end
|
|
339
|
-
|
|
340
|
-
let(:sincedb_path) { "#{temp_directory}/.sincedb" }
|
|
341
342
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
plugin.register
|
|
346
|
-
@run_thread = Thread.new(plugin) do |plugin|
|
|
347
|
-
Thread.current.abort_on_exception = true
|
|
348
|
-
plugin.run queue
|
|
343
|
+
context 'with sincedb cleanup enabled' do
|
|
344
|
+
let(:options) do
|
|
345
|
+
super().merge('sincedb_clean_after' => '1 second')
|
|
349
346
|
end
|
|
350
347
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
wait_for_start_processing(@run_thread)
|
|
354
|
-
end
|
|
355
|
-
|
|
356
|
-
after { plugin.stop }
|
|
348
|
+
around { |example| Timecop.freeze { example.run } }
|
|
357
349
|
|
|
358
|
-
|
|
359
|
-
|
|
350
|
+
it 'cleans up sincedb entry' do
|
|
351
|
+
wait_for_file_completion(sample_file)
|
|
360
352
|
|
|
361
|
-
|
|
362
|
-
|
|
353
|
+
sincedb_collection = plugin.watcher.sincedb_collection
|
|
354
|
+
expect(sincedb_collection.keys.size).to eq(1)
|
|
355
|
+
sincedb_value = sincedb_collection.get(sincedb_collection.keys.first)
|
|
356
|
+
after_expiry = Time.at(sincedb_value.last_changed_at + 2)
|
|
363
357
|
|
|
364
|
-
|
|
365
|
-
|
|
358
|
+
expect(File.read(options['sincedb_path'])).to_not be_empty
|
|
359
|
+
Timecop.freeze(after_expiry) do
|
|
360
|
+
Timeout.timeout(30) do
|
|
361
|
+
# Wait for scheduled flushes until the expired entry is removed
|
|
362
|
+
sincedb_flush_notifications.pop until sincedb_collection.keys.empty?
|
|
363
|
+
end
|
|
364
|
+
end
|
|
366
365
|
|
|
367
|
-
|
|
368
|
-
expect(
|
|
366
|
+
expect(sincedb_collection.keys).to be_empty
|
|
367
|
+
expect(File.read(options['sincedb_path'])).to be_empty
|
|
369
368
|
end
|
|
370
369
|
end
|
|
371
|
-
|
|
372
370
|
end
|
|
373
371
|
|
|
374
372
|
private
|
|
375
373
|
|
|
376
|
-
def
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
sleep(0.01) while run_thread.status != 'sleep'
|
|
380
|
-
sleep(timeout) unless plugin.queue
|
|
381
|
-
end
|
|
382
|
-
rescue Timeout::Error
|
|
383
|
-
raise "plugin did not start processing (timeout: #{timeout})" unless plugin.queue
|
|
384
|
-
else
|
|
385
|
-
raise "plugin did not start processing" unless plugin.queue
|
|
386
|
-
end
|
|
387
|
-
end
|
|
388
|
-
|
|
389
|
-
def wait_for_file_removal(path)
|
|
390
|
-
timeout = interval
|
|
391
|
-
try(5) do
|
|
392
|
-
wait(timeout).for { File.exist?(path) }.to be_falsey
|
|
393
|
-
end
|
|
374
|
+
def wait_for_file_completion(path, timeout: 30)
|
|
375
|
+
completion = Timeout.timeout(timeout) { file_completions.pop }
|
|
376
|
+
expect(completion).to eq([path, false])
|
|
394
377
|
end
|
|
395
378
|
end
|
data/version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
4.4.8
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: logstash-input-file
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.4.
|
|
4
|
+
version: 4.4.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Elastic
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-
|
|
10
|
+
date: 2026-11-25 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: logstash-core-plugin-api
|
|
@@ -243,11 +243,14 @@ files:
|
|
|
243
243
|
- spec/filewatch/reading_spec.rb
|
|
244
244
|
- spec/filewatch/rotate_spec.rb
|
|
245
245
|
- spec/filewatch/settings_spec.rb
|
|
246
|
+
- spec/filewatch/sincedb_collection_spec.rb
|
|
246
247
|
- spec/filewatch/sincedb_record_serializer_spec.rb
|
|
247
248
|
- spec/filewatch/spec_helper.rb
|
|
248
249
|
- spec/filewatch/tailing_spec.rb
|
|
250
|
+
- spec/filewatch/watch_spec.rb
|
|
249
251
|
- spec/filewatch/watched_file_spec.rb
|
|
250
252
|
- spec/filewatch/watched_files_collection_spec.rb
|
|
253
|
+
- spec/filewatch/windows_path_spec.rb
|
|
251
254
|
- spec/filewatch/winhelper_spec.rb
|
|
252
255
|
- spec/fixtures/compressed.log.gz
|
|
253
256
|
- spec/fixtures/compressed.log.gzip
|
|
@@ -260,6 +263,7 @@ files:
|
|
|
260
263
|
- spec/inputs/file_read_spec.rb
|
|
261
264
|
- spec/inputs/file_tail_spec.rb
|
|
262
265
|
- spec/inputs/friendly_durations_spec.rb
|
|
266
|
+
- version
|
|
263
267
|
homepage: http://www.elastic.co/guide/en/logstash/current/index.html
|
|
264
268
|
licenses:
|
|
265
269
|
- Apache-2.0
|
|
@@ -290,11 +294,14 @@ test_files:
|
|
|
290
294
|
- spec/filewatch/reading_spec.rb
|
|
291
295
|
- spec/filewatch/rotate_spec.rb
|
|
292
296
|
- spec/filewatch/settings_spec.rb
|
|
297
|
+
- spec/filewatch/sincedb_collection_spec.rb
|
|
293
298
|
- spec/filewatch/sincedb_record_serializer_spec.rb
|
|
294
299
|
- spec/filewatch/spec_helper.rb
|
|
295
300
|
- spec/filewatch/tailing_spec.rb
|
|
301
|
+
- spec/filewatch/watch_spec.rb
|
|
296
302
|
- spec/filewatch/watched_file_spec.rb
|
|
297
303
|
- spec/filewatch/watched_files_collection_spec.rb
|
|
304
|
+
- spec/filewatch/windows_path_spec.rb
|
|
298
305
|
- spec/filewatch/winhelper_spec.rb
|
|
299
306
|
- spec/fixtures/compressed.log.gz
|
|
300
307
|
- spec/fixtures/compressed.log.gzip
|