module NFAgent class ChunkHandler def initialize(chunk_size = 500) @mutex = Mutex.new @chunk_size = chunk_size make_new_chunk end def submit(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 != current_chunk.created_at.day Log.info("Expiring chunk due to date rollover") reset_chunk end current_chunk << line end def periodically_check_expired Thread.new do loop do check_full_or_expired sleep 5 end end end private def check_full_or_expired if current_chunk.full? || current_chunk.expired? reset_chunk end end def reset_chunk outgoing_chunk = current_chunk make_new_chunk Thread.new do outgoing_chunk.submit_to_server end end def make_new_chunk @mutex.synchronize do @current_chunk = Chunk.new(@chunk_size) end end def current_chunk @mutex.synchronize do return @current_chunk end end end end