Sha256: a157246123549662fd23c77284950ce156ebbef989affc7bd09214864b361b71

Contents?: true

Size: 910 Bytes

Versions: 2

Compression:

Stored size: 910 Bytes

Contents

module Fluent
  class FilePositionEntry
    require 'pathname'

    def initialize(file_path)
      @pos_file = file_path
    end

    def update_pos(pos)
      return unless @pos_file

      begin
        f = Pathname.new(@pos_file)
        f.open('wb') do |fp|
          Marshal.dump({
            :date => "#{pos[:date]}",
            :id   => "#{pos[:id]}",
          }, fp)
        end
      rescue => e
        $log.warn "Can't write pos_file #{e}"
      end
    end

    def read_pos
      min_date = "0000-01-01T00:00:00.000Z"

      return unless @pos_file
      f = Pathname.new(@pos_file)
      unless f.exist? then
        return ({:date=> min_date, :id=> ""})
      end
      
      pos = {}
      begin
        f.open('rb') do |_f|
          pos = Marshal.load(_f)
        end
      rescue => e
        return ({:date=> min_date, :id=> ""})
      end
      
      return (pos)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fluent-plugin-ncmb-0.1.1 lib/fluent/plugin/file_pos_entry.rb
fluent-plugin-ncmb-0.1.0 lib/fluent/plugin/file_pos_entry.rb