Sha256: 8d512c3cac97aadbb49094f659fbca38d3416cc1d0ba395a1fbf9902075ea33b

Contents?: true

Size: 984 Bytes

Versions: 6

Compression:

Stored size: 984 Bytes

Contents

module NFAgent
  class ChunkHandler

    # TODO: Rename this to Controller later
    def initialize(chunk_size = 500)
      @chunk = Chunk.new(chunk_size)
    end

    def append(line)
      # if current day is > day of last entry on current_chunk 
      # then submit and reset the chunk before adding the line
      current_day = Time.now.day
      if current_day != @chunk.created_at.day
        Log.info("Expiring chunk due to date rollover")
        reset_chunk
      end
      @chunk << line
    end

    def check_full_or_expired
      if @chunk.full?
        Log.info("Chunk Full: Resetting...")
        reset_chunk
      elsif @chunk.expired?
        Log.info("Chunk Expired: Resetting...")
        reset_chunk
      end
    end

    private
      def reset_chunk
        submitter = Submitter.new(@chunk.dump)
        submitter.errback { |payload|
          payload.write_to_disk(Config.dump_dir)
        }
        @chunk.clear
        submitter.perform
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nfagent-0.9.31 lib/nfagent/chunk_handler.rb
nfagent-0.9.19 lib/nfagent/chunk_handler.rb
nfagent-0.9.17 lib/nfagent/chunk_handler.rb
nfagent-0.9.15 lib/nfagent/chunk_handler.rb
nfagent-0.9.13 lib/nfagent/chunk_handler.rb
nfagent-0.9.11 lib/nfagent/chunk_handler.rb