@@ -121,22 +121,53 @@ def multi_receive_encoded(events_and_encoded)
121
121
# append to the file
122
122
chunks . each { |chunk | fd . write ( chunk ) }
123
123
end
124
- fd . flush unless @flusher && @flusher . alive?
124
+ on_flush ( fd , path ) unless @flusher && @flusher . alive?
125
125
end
126
126
127
127
close_stale_files
128
128
end
129
129
end
130
130
131
+ def on_flush ( fd , path )
132
+ fd . flush
133
+ if fd . is_temp
134
+ copy_to_gzip ( fd , path )
135
+ end
136
+ end
137
+
138
+ def copy_to_gzip ( fd , path )
139
+ zipfd = get_file ( path )
140
+ zipfd = Zlib ::GzipWriter . new ( zipfd )
141
+ fd . seek ( 0 , IO ::SEEK_SET )
142
+ data = fd . read
143
+ fd . truncate ( 0 )
144
+ fd . seek ( 0 , IO ::SEEK_SET )
145
+ if @write_behavior == "overwrite"
146
+ zipfd . to_io . truncate ( 0 )
147
+ zipfd . to_io . seek ( 0 , IO ::SEEK_SET )
148
+ end
149
+ zipfd . write ( data )
150
+ zipfd . flush
151
+ zipfd . to_io . flush
152
+ zipfd . close
153
+ end
154
+
131
155
def close
132
156
@flusher . stop unless @flusher . nil?
133
157
@io_mutex . synchronize do
134
158
@logger . debug ( "Close: closing files" )
135
159
136
160
@files . each do |path , fd |
137
161
begin
162
+ if fd . is_temp
163
+ copy_to_gzip ( fd , path )
164
+ end
138
165
fd . close
139
- @logger . debug ( "Closed file #{ path } " , :fd => fd )
166
+ if fd . is_temp && File . exist? ( fd . path )
167
+ File . delete ( fd . path )
168
+ @logger . debug ( "Deleted temp file " , :path => fd . path )
169
+ end
170
+ @logger . debug ( "Closed file #{ fd } " , :fd => fd )
140
171
rescue Exception => e
141
172
@logger . error ( "Exception while flushing and closing files." , :exception => e )
142
173
end
@@ -202,6 +233,9 @@ def flush_pending_files
202
233
@files . each do |path , fd |
203
234
@logger . debug ( "Flushing file" , :path => path , :fd => fd )
204
235
fd . flush
236
+ if fd . is_temp
237
+ copy_to_gzip ( fd , path )
238
+ end
205
239
end
206
240
end
207
241
rescue => e
@@ -220,6 +254,9 @@ def close_stale_files
220
254
inactive_files . each do |path , fd |
221
255
@logger . info ( "Closing file %s" % path )
222
256
fd . close
257
+ if fd . is_temp && File . exist? ( fd . path )
258
+ File . delete ( fd . path )
259
+ end
223
260
@files . delete ( path )
224
261
end
225
262
# mark all files as inactive, a call to write will mark them as active again
@@ -236,6 +273,7 @@ def deleted?(path)
236
273
end
237
274
238
275
def open ( path )
276
+ originalPath = path
239
277
if !deleted? ( path ) && cached? ( path )
240
278
return @files [ path ]
241
279
end
@@ -249,8 +287,27 @@ def open(path)
249
287
end
250
288
end
251
289
290
+ #Fix for broken gzip issue.
291
+ if gzip
292
+ tmpfile = java . io . File . createTempFile ( "outfile-" , "-temp" ) ;
293
+ path = tmpfile . path
294
+ #create file at original path also, so that temp file is not created again
295
+ make_dir ( originalPath )
296
+ gzFile = get_file ( originalPath )
297
+ #if gzFile is fifo type, file writer object is returned that needs to closed.
298
+ if gzFile . class == Java ::JavaIo ::FileWriter
299
+ gzFile . close
300
+ end
301
+ end
302
+
252
303
@logger . info ( "Opening file" , :path => path )
304
+ make_dir ( path )
305
+ fd = get_file ( path )
306
+ @files [ originalPath ] = IOWriter . new ( fd , gzip )
307
+ return @files [ originalPath ]
308
+ end
253
309
310
+ def make_dir ( path )
254
311
dir = File . dirname ( path )
255
312
if !Dir . exist? ( dir )
256
313
@logger . info ( "Creating directory" , :directory => dir )
@@ -260,7 +317,9 @@ def open(path)
260
317
FileUtils . mkdir_p ( dir )
261
318
end
262
319
end
320
+ end
263
321
322
+ def get_file ( path )
264
323
# work around a bug opening fifos (bug JRUBY-6280)
265
324
stat = File . stat ( path ) rescue nil
266
325
if stat && stat . ftype == "fifo"
@@ -272,10 +331,7 @@ def open(path)
272
331
fd = File . new ( path , "a+" )
273
332
end
274
333
end
275
- if gzip
276
- fd = Zlib ::GzipWriter . new ( fd )
277
- end
278
- @files [ path ] = IOWriter . new ( fd )
334
+ return fd
279
335
end
280
336
281
337
##
@@ -356,8 +412,9 @@ def run
356
412
class IOWriter
357
413
attr_accessor :active
358
414
359
- def initialize ( io )
415
+ def initialize ( io , is_temp )
360
416
@io = io
417
+ @is_temp = is_temp
361
418
end
362
419
363
420
def write ( *args )
@@ -372,6 +429,10 @@ def flush
372
429
end
373
430
end
374
431
432
+ def is_temp
433
+ return @is_temp
434
+ end
435
+
375
436
def method_missing ( method_name , *args , &block )
376
437
if @io . respond_to? ( method_name )
377
438
0 commit comments