Skip to content

Commit

Permalink
Merge pull request #2409 from fluent/improve-storage-local-plugin
Browse files Browse the repository at this point in the history
Improve storage local plugin
  • Loading branch information
repeatedly committed May 13, 2019
2 parents 77983d0 + a9a8586 commit 6e80961
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/fluent/plugin/storage_local.rb
Expand Up @@ -87,7 +87,12 @@ def configure(conf)
if File.exist?(@path)
raise Fluent::ConfigError, "Plugin storage path '#{@path}' is not readable/writable" unless File.readable?(@path) && File.writable?(@path)
begin
data = Yajl::Parser.parse(open(@path, 'r:utf-8'){ |io| io.read })
data = open(@path, 'r:utf-8') { |io| io.read }
if data.empty?
log.warn "detect empty plugin storage file during startup. Ignored: #{@path}"
return
end
data = Yajl::Parser.parse(data)
raise Fluent::ConfigError, "Invalid contents (not object) in plugin storage file: '#{@path}'" unless data.is_a?(Hash)
rescue => e
log.error "failed to read data from plugin storage file", path: @path, error: e
Expand Down Expand Up @@ -128,7 +133,7 @@ def save
tmp_path = @path + '.tmp'
begin
json_string = Yajl::Encoder.encode(@store, pretty: @pretty_print)
open(tmp_path, 'w:utf-8', @mode){ |io| io.write json_string }
open(tmp_path, 'w:utf-8', @mode) { |io| io.write json_string; io.fsync }
File.rename(tmp_path, @path)
rescue => e
log.error "failed to save data for plugin storage to file", path: @path, tmp: tmp_path, error: e
Expand Down

0 comments on commit 6e80961

Please sign in to comment.