protected void WhenBookmarkIsClosed() { Target.Dispose(); Target = null; }
protected void WhenBookmarkIsCreated() { Target = PersistedBookmark.Create(BookmarkFileName); }
private void ShipLogs(IPersistedBookmark bookmark) { do { string currentFilePath = bookmark.FileName; SelfLog.WriteLine("Bookmark is currently at offset {0} in '{1}'", bookmark.Position, currentFilePath); var fileSet = GetFileSet(); if (currentFilePath == null) { currentFilePath = fileSet.FirstOrDefault(); SelfLog.WriteLine("New log file is {0}", currentFilePath); bookmark.UpdateFileNameAndPosition(currentFilePath, 0L); } else if (fileSet.All(f => CompareFileNames(f, currentFilePath) != 0)) { currentFilePath = fileSet.FirstOrDefault(f => CompareFileNames(f, currentFilePath) >= 0); SelfLog.WriteLine("New log file is {0}", currentFilePath); bookmark.UpdateFileNameAndPosition(currentFilePath, 0L); } // delete all previous files - we will not read them anyway if (currentFilePath == null) { foreach (var fileToDelete in fileSet) { TryDeleteFile(fileToDelete); } } else { foreach (var fileToDelete in fileSet.TakeWhile(f => CompareFileNames(f, currentFilePath) < 0)) { TryDeleteFile(fileToDelete); } } if (currentFilePath == null) { SelfLog.WriteLine("No log file is found. Nothing to do."); break; } // now we are interested in current file and all after it. fileSet = fileSet.SkipWhile(f => CompareFileNames(f, currentFilePath) < 0) .ToArray(); var initialPosition = bookmark.Position; List <TRecord> records; do { var batch = ReadRecordBatch(currentFilePath, bookmark.Position, _batchPostingLimit); records = batch.Item2; if (records.Count > 0) { bool successful; var response = SendRecords(records, out successful); if (!successful) { SelfLog.WriteLine("SendRecords failed for {0} records.", records.Count); HandleError(response, records.Count); return; } } var newPosition = batch.Item1; if (initialPosition < newPosition) { SelfLog.WriteLine("Advancing bookmark from {0} to {1} on {2}", initialPosition, newPosition, currentFilePath); bookmark.UpdatePosition(newPosition); } else if (initialPosition > newPosition) { newPosition = 0; SelfLog.WriteLine("File {2} has been truncated or re-created, bookmark reset from {0} to {1}", initialPosition, newPosition, currentFilePath); bookmark.UpdatePosition(newPosition); } } while (records.Count >= _batchPostingLimit); if (initialPosition == bookmark.Position) { SelfLog.WriteLine("Found no records to process"); // Only advance the bookmark if there is next file in the queue // and no other process has the current file locked, and its length is as we found it. if (fileSet.Length > 1) { SelfLog.WriteLine("BufferedFilesCount: {0}; checking if can advance to the next file", fileSet.Length); var weAreAtEndOfTheFileAndItIsNotLockedByAnotherThread = WeAreAtEndOfTheFileAndItIsNotLockedByAnotherThread(currentFilePath, bookmark.Position); if (weAreAtEndOfTheFileAndItIsNotLockedByAnotherThread) { SelfLog.WriteLine("Advancing bookmark from '{0}' to '{1}'", currentFilePath, fileSet[1]); bookmark.UpdateFileNameAndPosition(fileSet[1], 0); } else { break; } } else { SelfLog.WriteLine("This is a single log file, and we are in the end of it. Nothing to do."); break; } } } while (true); }