Method: Fluent::SwiftOutput#write

Defined in:
lib/fluent/plugin/out_swift.rb

#write(chunk) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/fluent/plugin/out_swift.rb', line 119

def write(chunk)
  i = 0

  begin
    path = @path_slicer.call(@path)
    values_for_swift_object_key = {
      "path" => path,
      "time_slice" => chunk.key,
      "file_extension" => @ext,
      "index" => i
    }
    swift_path = @swift_object_key_format.gsub(%r(%{[^}]+})) { |expr|
      values_for_swift_object_key[expr[2...expr.size-1]]
    }
    i += 1
  end while check_object_exists(@swift_container, swift_path)

  tmp = Tempfile.new("swift-")
  begin
    if @store_as == "gzip"
      w = Zlib::GzipWriter.new(tmp)
      chunk.write_to(w)
      w.close
    elsif @store_as == "lzo"
      w = Tempfile.new("chunk-tmp")
      chunk.write_to(w)
      w.close
      tmp.close
      # We don't check the return code because we can't recover lzop failure.
      system "lzop -qf1 -o #{tmp.path} #{w.path}"
    else
      chunk.write_to(tmp)
      tmp.close
    end
    File.open(tmp.path) do |file|
      @storage.put_object(@swift_container, swift_path, file, {:content_type => @mime_type})
    end
    $log.info "Put Log to Swift. container=#{@swift_container} object=#{swift_path}"
  ensure
    tmp.close(true) rescue nil
    w.close rescue nil
    w.unlink rescue nil
  end
end