class LogStash::Outputs::LogstashAzureBlobOutput::TemporaryFileFactory

a sub class of LogstashAzureBlobOutput creates the temporary files to write and later upload

Constants

FILE_MODE
GZIP_ENCODING
GZIP_EXTENSION
STRFTIME
TXT_EXTENSION

Attributes

counter[RW]
current[RW]
encoding[RW]
prefix[RW]
tags[RW]
temporary_directory[RW]

Public Class Methods

new(prefix, tags, encoding, temporary_directory) click to toggle source

initialize the class

# File lib/logstash/outputs/blob/temporary_file_factory.rb, line 22
def initialize(prefix, tags, encoding, temporary_directory)
  @counter = 0
  @prefix = prefix

  @tags = tags
  @encoding = encoding
  @temporary_directory = temporary_directory
  @lock = Mutex.new

  rotate!
end

Public Instance Methods

rotate!() click to toggle source

do the rotation

# File lib/logstash/outputs/blob/temporary_file_factory.rb, line 35
def rotate!
  @lock.synchronize do
    @current = new_file
    increment_counter
    @current
  end
end

Private Instance Methods

current_time() click to toggle source

gets the current time

# File lib/logstash/outputs/blob/temporary_file_factory.rb, line 61
def current_time
  Time.now.strftime(STRFTIME)
end
extension() click to toggle source

if it is not gzip ecoding, then it is txt extension

# File lib/logstash/outputs/blob/temporary_file_factory.rb, line 46
def extension
  gzip? ? GZIP_EXTENSION : TXT_EXTENSION
end
generate_name() click to toggle source

method that generate the name of the file to be saved in blob storage

# File lib/logstash/outputs/blob/temporary_file_factory.rb, line 66
def generate_name
  filename = "#{current_time}.#{SecureRandom.uuid}"

  if !tags.empty?
    "#{filename}.tag_#{tags.join('.')}.part#{counter}.#{extension}"
  else
    "#{filename}.part#{counter}.#{extension}"
  end
end
gzip?() click to toggle source

boolean method to check if its gzip encoding

# File lib/logstash/outputs/blob/temporary_file_factory.rb, line 51
def gzip?
  encoding == GZIP_ENCODING
end
increment_counter() click to toggle source

increment the counter in 1 unit

# File lib/logstash/outputs/blob/temporary_file_factory.rb, line 56
def increment_counter
  @counter += 1
end
new_file() click to toggle source

create the file to be saved in blob storage

# File lib/logstash/outputs/blob/temporary_file_factory.rb, line 77
def new_file
  uuid = SecureRandom.uuid
  name = generate_name
  path = ::File.join(temporary_directory, uuid)
  key = ::File.join(prefix, name)

  FileUtils.mkdir_p(::File.join(path, prefix))

  io = if gzip?
         # We have to use this wrapper because we cannot access the size of the
         # file directly on the gzip writer.
         IOWrappedGzip.new(::File.open(::File.join(path, key), FILE_MODE))
       else
         ::File.open(::File.join(path, key), FILE_MODE)
       end

  TemporaryFile.new(key, io, path)
end