Sha256: 5c1ccc1a61d740dc0da766288401e43a4a02380fc912feb79460e35e828f9ffa

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# encoding: utf-8

require 'uuid'
require 'logstash/outputs/oss/gzip_file'
require 'logstash/outputs/oss/temporary_file'

module LogStash
  module Outputs
    class OSS
      class FileGenerator
        FILE_MODE = "a"
        STRFTIME = "%Y-%m-%dT%H.%M"

        attr_accessor :index, :prefix, :encoding, :temporary_directory, :current_file

        # `prefix`/logstash.oss.{random-uuid}.{%Y-%m-%dT%H.%M}.part-{index}.{extension}
        def initialize(prefix, encoding, temporary_directory)
          @index = 0
          @prefix = prefix
          # gzip or plain
          @encoding = encoding
          # temporary directory to save temporary file before upload to OSS
          @temporary_directory = temporary_directory

          @lock = Mutex.new

          rotate
        end

        def rotate
          @current_file = create_file
          @index += 1
          @current_file
        end

        def with_lock
          @lock.synchronize do
            yield self
          end
        end

        def extension
          @encoding == "gzip" ? "gz" : "data"
        end

        def gzip?
          @encoding == "gzip"
        end

        private
        def create_file
          uuid = UUID.new.generate
          file_name = "ls.oss.#{uuid}.#{Time.now.strftime(STRFTIME)}.part-#{index}.#{extension}"
          object_key = ::File.join(prefix, file_name)
          local_path = ::File.join(temporary_directory, uuid)

          FileUtils.mkdir_p(::File.join(local_path, prefix))
          file = if gzip?
                   GzipFile.new(::File.open(::File.join(local_path, object_key), FILE_MODE))
                 else
                   ::File.open(::File.join(local_path, object_key), FILE_MODE)
                 end

          TemporaryFile.new(file, object_key, local_path)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logstash-output-oss-0.1.1-java lib/logstash/outputs/oss/file_generator.rb