filewatch 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/filewatch/tail.rb +32 -10
- metadata +42 -55
data/lib/filewatch/tail.rb
CHANGED
@@ -4,6 +4,10 @@ require "logger"
|
|
4
4
|
|
5
5
|
module FileWatch
|
6
6
|
class Tail
|
7
|
+
# how often (in seconds) we @logger.warn a failed file open, per path.
|
8
|
+
OPEN_WARN_INTERVAL = ENV["FILEWATCH_OPEN_WARN_INTERVAL"] ?
|
9
|
+
ENV["FILEWATCH_OPEN_WARN_INTERVAL"].to_i : 300
|
10
|
+
|
7
11
|
attr_accessor :logger
|
8
12
|
|
9
13
|
public
|
@@ -15,6 +19,7 @@ module FileWatch
|
|
15
19
|
@logger.level = Logger::INFO
|
16
20
|
end
|
17
21
|
@files = {}
|
22
|
+
@lastwarn = Hash.new { |h, k| h[k] = 0 }
|
18
23
|
@buffers = {}
|
19
24
|
@watch = FileWatch::Watch.new
|
20
25
|
@watch.logger = @logger
|
@@ -23,7 +28,9 @@ module FileWatch
|
|
23
28
|
@statcache = {}
|
24
29
|
@opts = {
|
25
30
|
:sincedb_write_interval => 10,
|
26
|
-
:sincedb_path => "#{ENV["HOME"]}/.sincedb",
|
31
|
+
:sincedb_path => ENV["SINCEDB_PATH"] || "#{ENV["HOME"]}/.sincedb",
|
32
|
+
:stat_interval => 1,
|
33
|
+
:discover_interval => 5,
|
27
34
|
:exclude => [],
|
28
35
|
}.merge(opts)
|
29
36
|
@watch.exclude(@opts[:exclude])
|
@@ -45,21 +52,26 @@ module FileWatch
|
|
45
52
|
public
|
46
53
|
def subscribe(&block)
|
47
54
|
# subscribe(stat_interval = 1, discover_interval = 5, &block)
|
48
|
-
@watch.subscribe
|
55
|
+
@watch.subscribe(@opts[:stat_interval],
|
56
|
+
@opts[:discover_interval]) do |event, path|
|
49
57
|
case event
|
50
58
|
when :create, :create_initial
|
51
59
|
if @files.member?(path)
|
52
60
|
@logger.debug("#{event} for #{path}: already exists in @files")
|
53
61
|
next
|
54
62
|
end
|
55
|
-
_open_file(path, event)
|
56
|
-
|
63
|
+
if _open_file(path, event)
|
64
|
+
_read_file(path, &block)
|
65
|
+
end
|
57
66
|
when :modify
|
58
67
|
if !@files.member?(path)
|
59
68
|
@logger.debug(":modify for #{path}, does not exist in @files")
|
60
|
-
_open_file(path)
|
69
|
+
if _open_file(path, event)
|
70
|
+
_read_file(path, &block)
|
71
|
+
end
|
72
|
+
else
|
73
|
+
_read_file(path, &block)
|
61
74
|
end
|
62
|
-
_read_file(path, &block)
|
63
75
|
when :delete
|
64
76
|
@logger.debug(":delete for #{path}, deleted from @files")
|
65
77
|
_read_file(path, &block)
|
@@ -75,13 +87,21 @@ module FileWatch
|
|
75
87
|
private
|
76
88
|
def _open_file(path, event)
|
77
89
|
@logger.debug("_open_file: #{path}: opening")
|
78
|
-
# TODO(petef): handle File.open failing
|
79
90
|
begin
|
80
91
|
@files[path] = File.open(path)
|
81
|
-
rescue
|
82
|
-
|
92
|
+
rescue
|
93
|
+
# don't emit this message too often. if a file that we can't
|
94
|
+
# read is changing a lot, we'll try to open it more often,
|
95
|
+
# and might be spammy.
|
96
|
+
now = Time.now.to_i
|
97
|
+
if now - @lastwarn[path] > OPEN_WARN_INTERVAL
|
98
|
+
@logger.warn("failed to open #{path}: #{$!}")
|
99
|
+
@lastwarn[path] = now
|
100
|
+
else
|
101
|
+
@logger.debug("(warn supressed) failed to open #{path}: #{$!}")
|
102
|
+
end
|
83
103
|
@files.delete(path)
|
84
|
-
return
|
104
|
+
return false
|
85
105
|
end
|
86
106
|
|
87
107
|
stat = File::Stat.new(path)
|
@@ -105,6 +125,8 @@ module FileWatch
|
|
105
125
|
else
|
106
126
|
@logger.debug("#{path}: staying at position 0, no sincedb")
|
107
127
|
end
|
128
|
+
|
129
|
+
return true
|
108
130
|
end # def _open_file
|
109
131
|
|
110
132
|
private
|
metadata
CHANGED
@@ -1,63 +1,56 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filewatch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 19
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 0
|
8
|
-
- 3
|
9
|
-
- 0
|
10
|
-
version: 0.3.0
|
5
|
+
version: 0.3.2
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
|
-
- Jordan Sissel
|
14
|
-
- Pete Fritchman
|
8
|
+
- Jordan Sissel
|
9
|
+
- Pete Fritchman
|
15
10
|
autorequire:
|
16
11
|
bindir: bin
|
17
12
|
cert_chain: []
|
18
13
|
|
19
|
-
date:
|
20
|
-
default_executable:
|
14
|
+
date: 2012-01-02 00:00:00 Z
|
21
15
|
dependencies: []
|
22
16
|
|
23
17
|
description: Watch files and directories in ruby. Also supports tailing and glob file patterns.
|
24
18
|
email:
|
25
|
-
- jls@semicomplete.com
|
26
|
-
- petef@databits.net
|
19
|
+
- jls@semicomplete.com
|
20
|
+
- petef@databits.net
|
27
21
|
executables:
|
28
|
-
- globtail
|
22
|
+
- globtail
|
29
23
|
extensions: []
|
30
24
|
|
31
25
|
extra_rdoc_files: []
|
32
26
|
|
33
27
|
files:
|
34
|
-
- lib/filewatch/
|
35
|
-
- lib/filewatch/
|
36
|
-
- lib/filewatch/
|
37
|
-
- test/globtail/
|
38
|
-
- test/globtail/
|
39
|
-
- test/globtail/
|
40
|
-
- test/globtail/
|
41
|
-
- test/globtail/
|
42
|
-
- test/globtail/
|
43
|
-
- test/globtail/
|
44
|
-
- test/globtail/
|
45
|
-
- test/globtail/test10.sh
|
46
|
-
- test/globtail/
|
47
|
-
- test/globtail/
|
48
|
-
- test/globtail/
|
49
|
-
- test/globtail/
|
50
|
-
- test/globtail/
|
51
|
-
- test/globtail/
|
52
|
-
- test/globtail/
|
53
|
-
- test/globtail/
|
54
|
-
- test/globtail/test5.sh
|
55
|
-
- test/globtail/
|
56
|
-
- test/globtail/
|
57
|
-
- test/globtail/
|
58
|
-
- test/globtail/
|
59
|
-
- bin/globtail
|
60
|
-
has_rdoc: true
|
28
|
+
- lib/filewatch/watch.rb
|
29
|
+
- lib/filewatch/tail.rb
|
30
|
+
- lib/filewatch/buftok.rb
|
31
|
+
- test/globtail/test8.data
|
32
|
+
- test/globtail/framework.sh
|
33
|
+
- test/globtail/test4.sh
|
34
|
+
- test/globtail/test9.sh
|
35
|
+
- test/globtail/test8.sh
|
36
|
+
- test/globtail/test5.data
|
37
|
+
- test/globtail/test7.data
|
38
|
+
- test/globtail/test7.sh
|
39
|
+
- test/globtail/test10.sh
|
40
|
+
- test/globtail/Makefile
|
41
|
+
- test/globtail/test1.data
|
42
|
+
- test/globtail/test10.data
|
43
|
+
- test/globtail/test3.sh
|
44
|
+
- test/globtail/test1.sh
|
45
|
+
- test/globtail/test6.data
|
46
|
+
- test/globtail/test3.data
|
47
|
+
- test/globtail/test2.data
|
48
|
+
- test/globtail/test5.sh
|
49
|
+
- test/globtail/test4.data
|
50
|
+
- test/globtail/test6.sh
|
51
|
+
- test/globtail/test2.sh
|
52
|
+
- test/globtail/test9.data
|
53
|
+
- bin/globtail
|
61
54
|
homepage: https://github.com/jordansissel/ruby-filewatch
|
62
55
|
licenses: []
|
63
56
|
|
@@ -65,30 +58,24 @@ post_install_message:
|
|
65
58
|
rdoc_options: []
|
66
59
|
|
67
60
|
require_paths:
|
68
|
-
- lib
|
69
|
-
- lib
|
61
|
+
- lib
|
62
|
+
- lib
|
70
63
|
required_ruby_version: !ruby/object:Gem::Requirement
|
71
64
|
none: false
|
72
65
|
requirements:
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
segments:
|
77
|
-
- 0
|
78
|
-
version: "0"
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
79
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
70
|
none: false
|
81
71
|
requirements:
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
segments:
|
86
|
-
- 0
|
87
|
-
version: "0"
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
88
75
|
requirements: []
|
89
76
|
|
90
77
|
rubyforge_project:
|
91
|
-
rubygems_version: 1.
|
78
|
+
rubygems_version: 1.8.9
|
92
79
|
signing_key:
|
93
80
|
specification_version: 3
|
94
81
|
summary: filewatch - file watching for ruby
|