eventmachine-tail 0.1.20100505022629 → 0.1.20100506012705

Sign up to get free protection for your applications and to get access to all the features.
data/bin/rtail ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "eventmachine"
4
+ require "eventmachine-tail"
5
+
6
+ class Reader < EventMachine::FileTail
7
+ def initialize(path, startpos=-1)
8
+ super(path, startpos)
9
+ @buffer = BufferedTokenizer.new
10
+ end
11
+
12
+ def receive_data(data)
13
+ @buffer.extract(data).each do |line|
14
+ puts "#{path}: #{line}"
15
+ end
16
+ end
17
+ end
18
+
19
+ def main(args)
20
+ if args.length == 0
21
+ puts "Usage: #{$0} <path_or_glob> [path_or_glob2] [...]"
22
+ return 1
23
+ end
24
+
25
+ EventMachine.run do
26
+ args.each do |path|
27
+ EventMachine::FileGlobWatchTail.new(path, Reader)
28
+ end
29
+ end
30
+ end # def main
31
+
32
+ exit(main(ARGV))
data/lib/em/filetail.rb CHANGED
@@ -40,7 +40,8 @@ class EventMachine::FileTail
40
40
  def initialize(path, startpos=-1)
41
41
  @path = path
42
42
  @logger = Logger.new(STDOUT)
43
- @logger.level = Logger::WARN
43
+ @logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
44
+ @logger.debug("Tailing #{path} starting at position #{startpos}")
44
45
 
45
46
  @fstat = File.stat(@path)
46
47
 
@@ -73,6 +74,11 @@ class EventMachine::FileTail
73
74
  end
74
75
  end
75
76
 
77
+ public
78
+ def receive_data(data)
79
+ @logger.warn("Got #{data.length} bytes")
80
+ end
81
+
76
82
  private
77
83
  def open
78
84
  @file.close if @file
@@ -90,7 +96,7 @@ class EventMachine::FileTail
90
96
 
91
97
  private
92
98
  def watch
93
- EventMachine::watch_file(@path, FileWatcher, self)
99
+ EventMachine::watch_file(@path, EventMachine::FileTail::FileWatcher, self)
94
100
  end
95
101
 
96
102
  private
@@ -147,39 +153,41 @@ end # class EventMachine::FileTail
147
153
 
148
154
  # Internal usage only
149
155
  class EventMachine::FileTail::FileWatcher < EventMachine::FileWatch
150
- def initialize(filestream)
151
- @filestream = filestream
156
+ def initialize(filewatch)
157
+ @filewatch = filewatch
152
158
  @logger = Logger.new(STDOUT)
153
- @logger.level = Logger::WARN
159
+ @logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
160
+ @logger.debug("Watching on #{filewatch.path}")
154
161
  end
155
162
 
156
163
  def file_modified
157
- @filestream.notify :modified
164
+ @filewatch.notify :modified
158
165
  end
159
166
 
160
167
  def file_moved
161
- @filestream.notify :moved
168
+ @filewatch.notify :moved
162
169
  end
163
170
 
164
171
  def file_deleted
165
- @filestream.notify :deleted
172
+ @filewatch.notify :deleted
166
173
  end
167
174
 
168
175
  def unbind
169
- @filestream.notify :unbind
176
+ @filewatch.notify :unbind
170
177
  end
171
178
  end # class EventMachine::FileTail::FileWatch < EventMachine::FileWatch
172
179
 
173
180
  # Add EventMachine::file_tail
174
181
  module EventMachine
175
-
176
182
  # Tail a file.
177
183
  #
178
184
  # path is the path to the file to tail.
179
185
  # handler should be a module implementing 'receive_data' or
180
186
  # must be a subclasses of EventMachine::FileTail
181
187
  def self.file_tail(path, handler=nil, *args)
182
- args.unshift(path)
188
+ # This code mostly styled on what EventMachine does in many of it's other
189
+ # methods.
190
+ args = [path, *args]
183
191
  klass = klass_from_handler(EventMachine::FileTail, handler, *args);
184
192
  c = klass.new(*args)
185
193
  yield c if block_given?
@@ -20,11 +20,10 @@ require "em/filetail"
20
20
  class EventMachine::FileGlobWatch
21
21
  def initialize(pathglob, interval=60)
22
22
  @pathglob = pathglob
23
- #@handler = handler
24
23
  @files = Set.new
25
24
  @watches = Hash.new
26
25
  @logger = Logger.new(STDOUT)
27
- @logger.level = Logger::INFO
26
+ @logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
28
27
 
29
28
  # We periodically check here because it is easier than writing our own glob
30
29
  # parser (so we can smartly watch globs like /foo/*/bar*/*.log)
@@ -69,7 +68,11 @@ class EventMachine::FileGlobWatch
69
68
  # If EventMachine::watch_file fails, that's ok, I guess.
70
69
  # We'll still find the file 'missing' from the next glob attempt.
71
70
  begin
72
- @watches[path] = EventMachine::watch_file(path, GlobFileWatch, self)
71
+ # EM currently has a bug that only the first handler for a watch_file
72
+ # on each file gets events. This causes globtails to never get data
73
+ # since the glob is watching the file already.
74
+ # Until we fix that, let's skip file watching here.
75
+ #@watches[path] = EventMachine::watch_file(path, GlobFileWatch, self)
73
76
  rescue Errno::EACCES => e
74
77
  @logger.warn(e)
75
78
  end
@@ -94,15 +97,15 @@ class EventMachine::FileGlobWatch
94
97
  end # class EventMachine::FileGlobWatch
95
98
 
96
99
  class EventMachine::FileGlobWatchTail < EventMachine::FileGlobWatch
97
- def initialize(path, handler=nil, *args)
98
- super(path, *args)
100
+ def initialize(path, handler=nil, interval=60, *args)
101
+ super(path, interval)
99
102
  @handler = handler
100
103
  @args = args
101
104
  end
102
105
 
103
106
  def file_found(path)
104
107
  begin
105
- EventMachine::file_tail(path, @handler, *@args)
108
+ EventMachine::file_tail(path, @handler)
106
109
  rescue Errno::EACCES => e
107
110
  file_error(path, e)
108
111
  rescue Errno::EISDIR => e
data/samples/glob-tail.rb CHANGED
@@ -16,17 +16,17 @@
16
16
  require "rubygems"
17
17
  require "eventmachine"
18
18
  require "eventmachine-tail"
19
- require "ap" # from rubygem awesome_print
20
19
 
21
20
  class Reader < EventMachine::FileTail
22
- def initialize(*args)
23
- super(*args)
21
+ def initialize(path, startpos=-1)
22
+ super(path, startpos)
23
+ puts "Tailing #{path}"
24
24
  @buffer = BufferedTokenizer.new
25
25
  end
26
26
 
27
27
  def receive_data(data)
28
28
  @buffer.extract(data).each do |line|
29
- ap [path, line]
29
+ puts "#{path}: #{line}"
30
30
  end
31
31
  end
32
32
  end
@@ -38,11 +38,8 @@ def main(args)
38
38
  end
39
39
 
40
40
  EventMachine.run do
41
- #handler = EventMachine::FileGlobWatchTail.new(Reader)
42
41
  args.each do |path|
43
42
  EventMachine::FileGlobWatchTail.new(path, Reader)
44
- #EventMachine::FileGlobWatch.new(path, handler)
45
- #EventMachine::file_tail(path, handler)
46
43
  end
47
44
  end
48
45
  end # def main
data/samples/tail.rb CHANGED
@@ -9,8 +9,9 @@ require "eventmachine"
9
9
  require "eventmachine-tail"
10
10
 
11
11
  class Reader < EventMachine::FileTail
12
- def initialize(path, startpos=0)
12
+ def initialize(path, startpos=-1)
13
13
  super(path, startpos)
14
+ puts "Tailing #{path}"
14
15
  @buffer = BufferedTokenizer.new
15
16
  end
16
17
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 20100505022629
9
- version: 0.1.20100505022629
8
+ - 20100506012705
9
+ version: 0.1.20100506012705
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jordan Sissel
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-05 00:00:00 -07:00
17
+ date: 2010-05-06 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -31,8 +31,8 @@ dependencies:
31
31
  version_requirements: *id001
32
32
  description: Add file 'tail' implemented with EventMachine. Also includes a 'glob watch' class for watching a directory pattern for new matches, like /var/log/*.log
33
33
  email: jls@semicomplete.com
34
- executables: []
35
-
34
+ executables:
35
+ - rtail
36
36
  extensions: []
37
37
 
38
38
  extra_rdoc_files: []
@@ -45,6 +45,7 @@ files:
45
45
  - samples/glob-tail.rb
46
46
  - samples/globwatch.rb
47
47
  - test/test_filetail.rb
48
+ - bin/rtail
48
49
  has_rdoc: true
49
50
  homepage: http://code.google.com/p/semicomplete/wiki/EventMachineTail
50
51
  licenses: []