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