eventmachine-tail 0.5.20101005181243 → 0.5.20101204110840

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.
Files changed (3) hide show
  1. data/lib/em/filetail.rb +43 -11
  2. data/test/test_filetail.rb +9 -1
  3. metadata +11 -4
data/lib/em/filetail.rb CHANGED
@@ -13,6 +13,13 @@ EventMachine.kqueue = true if EventMachine.kqueue?
13
13
  # def receive_data(data)
14
14
  # puts "Got #{data.length} bytes"
15
15
  # end
16
+ #
17
+ # # Optional
18
+ # def eof
19
+ # puts "Got EOF!"
20
+ # # If you want to stop
21
+ # stop
22
+ # end
16
23
  # end
17
24
  #
18
25
  # # Now add it to EM
@@ -64,7 +71,7 @@ class EventMachine::FileTail
64
71
  @logger.debug("Tailing #{path} starting at position #{startpos}")
65
72
 
66
73
  @file = nil
67
- @want_eof = false
74
+ @want_eof_handling = false
68
75
  @want_read = false
69
76
  @want_reopen = false
70
77
  @reopen_on_eof = false
@@ -132,6 +139,16 @@ class EventMachine::FileTail
132
139
  end
133
140
  end # def receive_data
134
141
 
142
+ # This method is called when a tailed file reaches EOF.
143
+ #
144
+ # If you want to stop reading this file, call close(), otherwise
145
+ # this eof is handled as normal tailing does. The default
146
+ # EOF handler is to do nothing.
147
+ public
148
+ def eof
149
+ # do nothing, subclassers should implement this.
150
+ end # def eof
151
+
135
152
  # notify is invoked by EM::watch_file when the file you are tailing has been
136
153
  # modified or otherwise needs to be acted on.
137
154
  private
@@ -144,13 +161,17 @@ class EventMachine::FileTail
144
161
  @reopen_on_eof = true
145
162
  schedule_next_read
146
163
  elsif status == :unbind
147
- # Do what?
164
+ # :unbind is called after the :deleted handler
165
+ # :deleted happens on FreeBSD's newsyslog instead of :moved
166
+ # clean up @watch since its reference is wiped in EM's file_deleted callback
167
+ @watch = nil
148
168
  end
149
169
  end # def notify
150
170
 
151
171
  # Open (or reopen, if necessary) our file and schedule a read.
152
172
  private
153
173
  def open
174
+ return if @closed
154
175
  @file.close if @file
155
176
  begin
156
177
  @logger.debug "Opening file #{@path}"
@@ -165,6 +186,15 @@ class EventMachine::FileTail
165
186
  schedule_next_read
166
187
  end # def open
167
188
 
189
+ # Close this filetail
190
+ public
191
+ def close
192
+ @closed = true
193
+ EM.schedule do
194
+ @file.close if @file
195
+ end
196
+ end # def close
197
+
168
198
  # Watch our file.
169
199
  private
170
200
  def watch
@@ -213,6 +243,7 @@ class EventMachine::FileTail
213
243
  # Read CHUNKSIZE from our file and pass it to .receive_data()
214
244
  private
215
245
  def read
246
+ return if @closed
216
247
  @logger.debug "#{self}: Reading..."
217
248
  begin
218
249
  data = @file.sysread(CHUNKSIZE)
@@ -227,17 +258,18 @@ class EventMachine::FileTail
227
258
  rescue EOFError
228
259
  schedule_eof
229
260
  end
230
- end
261
+ end # def read
231
262
 
232
263
  # Do EOF handling on next EM iteration
264
+ private
233
265
  def schedule_eof
234
- if !@want_eof
235
- @want_eof = true
266
+ if !@want_eof_handling
267
+ eof # Call our own eof event
268
+ @want_eof_handling = true
236
269
  EventMachine::next_tick do
237
- @want_eof = false
238
- eof
270
+ handle_eof
239
271
  end # EventMachine::next_tick
240
- end # if !@want_eof
272
+ end # if !@want_eof_handling
241
273
  end # def schedule_eof
242
274
 
243
275
  private
@@ -252,8 +284,8 @@ class EventMachine::FileTail
252
284
  end # def schedule_reopen
253
285
 
254
286
  private
255
- def eof
256
- @want_eof = false
287
+ def handle_eof
288
+ @want_eof_handling = false
257
289
 
258
290
  if @reopen_on_eof
259
291
  @reopen_on_eof = false
@@ -284,7 +316,7 @@ class EventMachine::FileTail
284
316
  end # begin/rescue ENOENT
285
317
  end # EM::PeriodicTimer
286
318
  end # begin/rescue ENOENT
287
- end # def eof
319
+ end # def handle_eof
288
320
 
289
321
  private
290
322
  def read_file_metadata(&block)
@@ -28,9 +28,17 @@ class Reader < EventMachine::FileTail
28
28
  expected = @data.shift
29
29
  @testobj.assert_equal(expected, line,
30
30
  "Expected '#{expected}' on line #{@lineno}, but got '#{line}'")
31
- @testobj.finish if @data.length == 0
32
31
  end # @buffer.extract
33
32
  end # def receive_data
33
+
34
+ # This effectively tests EOF handling by requiring it to work in order
35
+ # for the tests to pass.
36
+ def eof
37
+ if @data.length == 0
38
+ close
39
+ @testobj.finish
40
+ end
41
+ end # def eof
34
42
  end # class Reader
35
43
 
36
44
  class TestFileTail < Test::Unit::TestCase
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eventmachine-tail
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 40202408221691
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 5
8
- - 20101005181243
9
- version: 0.5.20101005181243
9
+ - 20101204110840
10
+ version: 0.5.20101204110840
10
11
  platform: ruby
11
12
  authors:
12
13
  - Jordan Sissel
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-10-05 00:00:00 -07:00
18
+ date: 2010-12-04 00:00:00 -08:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: eventmachine
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 0
29
32
  version: "0"
@@ -62,23 +65,27 @@ require_paths:
62
65
  - lib
63
66
  - lib
64
67
  required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
65
69
  requirements:
66
70
  - - ">="
67
71
  - !ruby/object:Gem::Version
72
+ hash: 3
68
73
  segments:
69
74
  - 0
70
75
  version: "0"
71
76
  required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
72
78
  requirements:
73
79
  - - ">="
74
80
  - !ruby/object:Gem::Version
81
+ hash: 3
75
82
  segments:
76
83
  - 0
77
84
  version: "0"
78
85
  requirements: []
79
86
 
80
87
  rubyforge_project:
81
- rubygems_version: 1.3.6
88
+ rubygems_version: 1.3.7
82
89
  signing_key:
83
90
  specification_version: 3
84
91
  summary: eventmachine tail - a file tail implementation with glob support