Sha256: cc5dc839a53d2748015b4df0927f944e38017e433102704d66f9256ef0651b13

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require "fluent/plugin/input"

module Fluent
  module Plugin
    class SystemdInput < Input
      class PosWriter
        def initialize(pos_file)
          @path = pos_file
          @lock = Mutex.new
          @cursor = nil
          @written_cursor = nil
          setup
        end

        attr_reader :cursor, :path

        def start
          return unless @path
          @running = true
          @thread = Thread.new(&method(:work))
        end

        def shutdown
          return unless @path
          @running = false
          @thread.join
          write_pos
        end

        def update(c)
          @lock.synchronize { @cursor = c }
        end

        private

        def setup
          return unless @path && File.exist?(@path)
          @cursor = IO.read(@path).chomp
        end

        def work
          while @running
            write_pos
            sleep 1
          end
        end

        def write_pos
          @lock.synchronize do
            if @written_cursor != @cursor
              file = File.open(@path, "w+", 0644)
              file.print @cursor
              file.close
              @written_cursor = @cursor
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-systemd-0.1.1 lib/fluent/plugin/systemd/pos_writer.rb