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 CHANGED
@@ -1,2 +1,3 @@
1
1
  tmp/
2
2
  log/
3
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fwd (0.3.0)
4
+ fwd (0.3.1)
5
5
  connection_pool
6
6
  eventmachine-le
7
7
  servolux
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.0"
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, :path
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
- (@rate > 0 && @count >= @rate) || (@limit > 0 && @path.size >= @limit)
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
- FileUtils.mv(@path.to_s, @path.to_s.sub(/\.open$/, ".closed")) if @path
46
- @fd, @path = new_file
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
- !@fd || @path.size > MAX_SIZE
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 = File.open(path, "wb")
72
+ file = path.open("wb")
64
73
  file.sync = true
65
- [file, path]
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
- if yield(target.read)
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
@@ -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.path.size
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
- it { should change { buffer.path } }
44
- it { should change { buffer.fd } }
45
- it { should change { files.size }.by(1) }
42
+ describe "when changed" do
43
+ before { buffer.concat("x" * 1024) }
46
44
 
47
- it 'should archive previous file' do
48
- previous = buffer.path
49
- subject.call
50
- files.should include(previous.sub("open", "closed").to_s)
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
- lambda { subject.flush! }.should change { subject.path }
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.0
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-19 00:00:00.000000000 Z
12
+ date: 2013-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine-le