Sha256: 86842381501f1792a1d4be8beacaafb040f32d2bc9538db54374367c3426f6ec

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module ForemanInventoryUpload
  module Async
    class ProgressOutput
      def self.get(label)
        ProgressOutput.new(label, :reader)
      end

      def self.register(label)
        ProgressOutput.new(label, :writer)
      end

      def initialize(label, mode)
        @label = label
        @mode = mode
      end

      def buffer
        @buffer ||= begin
                      File.open(file_name, file_mode)
                    rescue Errno::ENOENT
                      StringIO.new
                    end
      end

      def full_output
        buffer.read
      end

      def write_line(line)
        buffer << line
        buffer.fsync
      end

      def close
        @buffer&.close
      end

      def status
        File.read(file_name(:status))
      rescue Errno::ENOENT
        ''
      end

      def status=(status)
        File.atomic_write(file_name(:status)) do |status_file|
          status_file.write(status)
        end
      end

      private

      def file_mode
        @mode == :reader ? 'r' : 'w'
      end

      def file_name(type = 'out')
        File.join(ForemanInventoryUpload.outputs_folder, "#{@label}.#{type}")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foreman_inventory_upload-0.0.1.dev1 lib/foreman_inventory_upload/async/progress_output.rb