evanescent 1.0.0 → 1.0.1

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. checksums.yaml +4 -4
  2. data/lib/evanescent.rb +43 -24
  3. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9756a1c8493afab25b856431ec5359429ec7a36
4
- data.tar.gz: 7a649af3f5758d2d7d8bb180787d8ff334ab30c1
3
+ metadata.gz: 9ff1e9e7823c63c657230fa20dd406dacb2897c0
4
+ data.tar.gz: ccc22b1ec8761409e924d053b1aa3922fb3df822
5
5
  SHA512:
6
- metadata.gz: 22f7837d5e81ea7e90fa53b490a838a95f3ce8b865aa4b962fa86ce6cb3ac1d47deecfa0d09110b1b7e36fc7e77e72e319a255598e629cf0df46998d929583d7
7
- data.tar.gz: 4af0198e703e5972b924a6ad1d8f70ab32eb4942c18674b962c0a5a754a84d44040f430ae9b1870e23d476c0b14ccf8133f7b3d697dec5852f07950b8de8c211
6
+ metadata.gz: 148420dc06bc9e69b70e402b12f22119e63166b146c057811c34c461cef9cd86dbbb047f2d75a88ed9a5ff02efbdc2b1bae17341733aa597dd5614c15a6a80c0
7
+ data.tar.gz: a46387fccc6656c52639775dcfad5603eb7e8ac03ab73d5afb247312ed15aba58f831e57764d6f362028e3c0a2046676151bcc9b46ec8d1f8bf12100df0fcfbb
data/lib/evanescent.rb CHANGED
@@ -27,7 +27,8 @@ class Evanescent
27
27
  @keep = ChronicDuration.parse(opts[:keep])
28
28
  @mutex = Mutex.new
29
29
  @last_prefix = make_prefix(Time.now)
30
- open_file
30
+ @io = nil
31
+ @compress_thread = nil
31
32
  end
32
33
 
33
34
  # Writes to #path and rotate, compress and purge if necessary.
@@ -36,6 +37,7 @@ class Evanescent
36
37
  rotate
37
38
  compress
38
39
  purge
40
+ open_io
39
41
  @io.write(string)
40
42
  end
41
43
  end
@@ -47,11 +49,26 @@ class Evanescent
47
49
  end
48
50
  end
49
51
 
52
+ # Compression is done in a separate thread. Thus method suspends current thread execution until existing compression thread returns. If no compression thread is running, returns immediately.
53
+ def wait_compression
54
+ if @compress_thread
55
+ begin
56
+ @compress_thread.join
57
+ rescue
58
+ warn("Compression thread failed: #{$!}")
59
+ ensure
60
+ @compress_thread = nil
61
+ end
62
+ end
63
+ end
64
+
50
65
  private
51
66
 
52
- def open_file
53
- @io = File.open(path, File::APPEND | File::CREAT | File::WRONLY)
54
- @io.sync = true
67
+ def open_io
68
+ unless @io
69
+ @io = File.open(path, File::APPEND | File::CREAT | File::WRONLY)
70
+ @io.sync = true
71
+ end
55
72
  end
56
73
 
57
74
  PARAMS = {
@@ -71,33 +88,35 @@ class Evanescent
71
88
 
72
89
  def rotate
73
90
  curr_suffix = make_prefix(Time.now)
74
- if curr_suffix != @last_prefix
75
- @io.close
76
- rotated = "#{path}.#{curr_suffix}"
77
- begin
78
- FileUtils.mv(path, rotated)
79
- rescue
80
- warn("Error renaming '#{path}' to '#{rotated}': #{$!}")
81
- end
82
- open_file
83
- @last_prefix = curr_suffix
91
+ return if curr_suffix == @last_prefix
92
+ @io.close
93
+ @io = nil
94
+ rotated = "#{path}.#{curr_suffix}"
95
+ begin
96
+ FileUtils.mv(path, rotated)
97
+ rescue
98
+ warn("Error renaming '#{path}' to '#{rotated}': #{$!}")
84
99
  end
100
+ @last_prefix = curr_suffix
85
101
  end
86
102
 
87
103
  def compress
88
- Dir.glob("#{path}.#{PARAMS[rotation][:glob]}").each do |uncompressed|
89
- compressed = "#{uncompressed}.gz"
90
- Zlib::GzipWriter.open(compressed) do |gz|
91
- gz.mtime = File.mtime(uncompressed)
92
- gz.orig_name = uncompressed
93
- File.open(uncompressed, 'r') do |io|
94
- io.binmode
95
- io.each do |data|
96
- gz.write(data)
104
+ wait_compression
105
+ @compress_thread = Thread.new do
106
+ Dir.glob("#{path}.#{PARAMS[rotation][:glob]}").each do |uncompressed|
107
+ compressed = "#{uncompressed}.gz"
108
+ Zlib::GzipWriter.open(compressed) do |gz|
109
+ gz.mtime = File.mtime(uncompressed)
110
+ gz.orig_name = uncompressed
111
+ File.open(uncompressed, 'r') do |io|
112
+ io.binmode
113
+ io.each do |data|
114
+ gz.write(data)
115
+ end
97
116
  end
98
117
  end
118
+ File.delete(uncompressed)
99
119
  end
100
- File.delete(uncompressed)
101
120
  end
102
121
  rescue
103
122
  warn("Error compressing files: #{$!}")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evanescent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Pugliese Ornellas
@@ -144,8 +144,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  version: '0'
145
145
  requirements: []
146
146
  rubyforge_project:
147
- rubygems_version: 2.4.5
147
+ rubygems_version: 2.4.5.1
148
148
  signing_key:
149
149
  specification_version: 4
150
150
  summary: IO like object that allows logging rotation, compression and purging.
151
151
  test_files: []
152
+ has_rdoc: