Sha256: b7c03367c40ef7822d60928e9b7ecc9b8f24456eb99009421a1793723790eb4e

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

module Fluent
  class SystemdInput < Input
    class PosWriter
      def initialize(pos_file)
        @path = pos_file
        setup
      end

      attr_reader :cursor

      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)
        return unless path
        lock.synchronize { @cursor = c }
      end

      private

      attr_reader :path, :lock, :thread, :running

      def setup
        return unless path
        @lock = Mutex.new
        @cursor = IO.read(path).chomp if File.exist?(path)
      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+')
            file.print cursor
            file.close
            @written_cursor = cursor
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fluent-plugin-systemd-0.0.2 lib/fluent/plugin/systemd/pos_writer.rb
fluent-plugin-systemd-0.0.1 lib/fluent/plugin/systemd/pos_writer.rb