fwd 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/fwd.gemspec +2 -2
- data/lib/fwd/buffer.rb +16 -7
- data/lib/fwd/output.rb +6 -2
- data/spec/fwd/buffer_spec.rb +18 -10
- metadata +2 -2
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/fwd.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.name = File.basename(__FILE__, '.gemspec')
|
10
10
|
s.summary = "fwd >>"
|
11
11
|
s.description = "The minimalistic stream forwarder"
|
12
|
-
s.version = "0.3.
|
12
|
+
s.version = "0.3.1"
|
13
13
|
|
14
14
|
s.authors = ["Black Square Media"]
|
15
15
|
s.email = "info@blacksquaremedia.com"
|
@@ -28,4 +28,4 @@ Gem::Specification.new do |s|
|
|
28
28
|
s.add_development_dependency "bundler"
|
29
29
|
s.add_development_dependency "rspec"
|
30
30
|
s.add_development_dependency "yard"
|
31
|
-
end
|
31
|
+
end
|
data/lib/fwd/buffer.rb
CHANGED
@@ -3,7 +3,7 @@ class Fwd::Buffer
|
|
3
3
|
def_delegators :core, :root, :prefix, :logger
|
4
4
|
|
5
5
|
MAX_SIZE = 64 * 1024 * 1024 # 64M
|
6
|
-
attr_reader :core, :interval, :rate, :count, :limit, :timer, :fd
|
6
|
+
attr_reader :core, :interval, :rate, :count, :limit, :timer, :fd
|
7
7
|
|
8
8
|
# Constructor
|
9
9
|
# @param [Fwd] core
|
@@ -37,19 +37,28 @@ class Fwd::Buffer
|
|
37
37
|
|
38
38
|
# @return [Boolean] true if flush is due
|
39
39
|
def flush?
|
40
|
-
|
40
|
+
return unless @fd
|
41
|
+
(@rate > 0 && @count >= @rate) || (@limit > 0 && @fd.size >= @limit)
|
41
42
|
end
|
42
43
|
|
43
44
|
# (Force) rotate buffer file
|
44
45
|
def rotate!
|
45
|
-
|
46
|
-
|
46
|
+
return if @fd && @fd.size.zero?
|
47
|
+
|
48
|
+
if @fd
|
49
|
+
logger.debug { "Rotate #{File.basename(@fd.path)} (#{@fd.size / 1024} kB)" }
|
50
|
+
FileUtils.mv(@fd.path, @fd.path.sub(/\.open$/, ".closed"))
|
51
|
+
end
|
52
|
+
|
53
|
+
@fd = new_file
|
47
54
|
rescue Errno::ENOENT
|
48
55
|
end
|
49
56
|
|
50
57
|
# @return [Boolean] true if rotation is due
|
51
58
|
def rotate?
|
52
|
-
|
59
|
+
@fd.nil? || @fd.size > MAX_SIZE
|
60
|
+
rescue Errno::ENOENT
|
61
|
+
false
|
53
62
|
end
|
54
63
|
|
55
64
|
private
|
@@ -60,9 +69,9 @@ class Fwd::Buffer
|
|
60
69
|
path = root.join("#{generate_name}.open")
|
61
70
|
end
|
62
71
|
FileUtils.mkdir_p root.to_s
|
63
|
-
file =
|
72
|
+
file = path.open("wb")
|
64
73
|
file.sync = true
|
65
|
-
|
74
|
+
file
|
66
75
|
end
|
67
76
|
|
68
77
|
def reschedule!
|
data/lib/fwd/output.rb
CHANGED
@@ -23,10 +23,11 @@ class Fwd::Output
|
|
23
23
|
# Callback
|
24
24
|
def forward!
|
25
25
|
Dir[root.join("#{prefix}.*.closed")].each do |file|
|
26
|
-
reserve(file) do |data|
|
26
|
+
ok = reserve(file) do |data|
|
27
27
|
logger.debug { "Flushing #{File.basename(file)}, #{data.size.fdiv(1024).round} kB" }
|
28
28
|
write(data)
|
29
29
|
end
|
30
|
+
break unless ok
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
@@ -45,12 +46,15 @@ class Fwd::Output
|
|
45
46
|
target = Pathname.new(file.sub(/\.closed$/, ".reserved"))
|
46
47
|
FileUtils.mv file, target.to_s
|
47
48
|
|
48
|
-
|
49
|
+
result = yield(target.read)
|
50
|
+
if result
|
49
51
|
target.unlink
|
50
52
|
else
|
51
53
|
logger.error "Flushing of #{target} failed."
|
52
54
|
FileUtils.mv target.to_s, target.to_s.sub(/\.reserved$/, ".closed")
|
53
55
|
end
|
56
|
+
|
57
|
+
result
|
54
58
|
rescue Errno::ENOENT
|
55
59
|
# Ignore if file was alread flushed by another process
|
56
60
|
end
|
data/spec/fwd/buffer_spec.rb
CHANGED
@@ -23,7 +23,6 @@ describe Fwd::Buffer do
|
|
23
23
|
its(:limit) { should be(2048) }
|
24
24
|
its(:timer) { should be(timer) }
|
25
25
|
its(:fd) { should be_instance_of(File) }
|
26
|
-
its(:path) { should be_instance_of(Pathname) }
|
27
26
|
its(:logger) { should be(Fwd.logger) }
|
28
27
|
|
29
28
|
describe "concat" do
|
@@ -31,7 +30,7 @@ describe Fwd::Buffer do
|
|
31
30
|
lambda {
|
32
31
|
subject.concat("x" * 1024)
|
33
32
|
}.should change {
|
34
|
-
subject.
|
33
|
+
subject.fd.size
|
35
34
|
}.by(1024)
|
36
35
|
end
|
37
36
|
end
|
@@ -40,14 +39,22 @@ describe Fwd::Buffer do
|
|
40
39
|
before { buffer }
|
41
40
|
subject { lambda { buffer.rotate! } }
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
it { should change { files.size }.by(1) }
|
42
|
+
describe "when changed" do
|
43
|
+
before { buffer.concat("x" * 1024) }
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
it { should change { buffer.fd.path } }
|
46
|
+
it { should change { files.size }.by(1) }
|
47
|
+
|
48
|
+
it 'should archive previous file' do
|
49
|
+
previous = buffer.fd.path
|
50
|
+
subject.call
|
51
|
+
files.should include(previous.sub("open", "closed").to_s)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "when unchanged" do
|
56
|
+
it { should_not change { buffer.fd.path } }
|
57
|
+
it { should_not change { files.size } }
|
51
58
|
end
|
52
59
|
end
|
53
60
|
|
@@ -71,7 +78,8 @@ describe Fwd::Buffer do
|
|
71
78
|
end
|
72
79
|
|
73
80
|
it 'should rotate file' do
|
74
|
-
|
81
|
+
subject.concat("x")
|
82
|
+
lambda { subject.flush! }.should change { subject.fd.path }
|
75
83
|
files.size.should == 2
|
76
84
|
end
|
77
85
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fwd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine-le
|