/// <summary> /// Records a new available and accessible data source file in the log. /// </summary> public bool Add(DataFileLog log, string addressId, DateTime dateLastUpdated) { bool containsEntry; lock (_syncLock) { Guard.ArgumentNotNull(log, nameof(log)); Guard.ArgumentNotNullOrEmptyOrWhiteSpace(addressId, nameof(addressId)); containsEntry = log.Entries .Any(m => string.Equals(addressId, m.AddressId, StringComparison.InvariantCultureIgnoreCase) && m.DateLastUpdated == dateLastUpdated); } if (!containsEntry) { log.Entries.Add(new DataFileLogEntry(addressId, dateLastUpdated)); Save(log); // update log return(true); } else { // already added } return(false); }
/// <summary> /// Updates a given flow log file to match the entries in the target /// directory. /// </summary> /// <param name="log">The log file to update.</param> /// <returns>True if the log was updated with new entries.</returns> public bool UpdateLog(DataFileLog log) { Guard.ArgumentNotNull(log, nameof(log)); bool hasChanges = false; foreach (var directory in _sourceDirectories) { FileInfo[] files; if (string.IsNullOrWhiteSpace(log.FileTypes)) { files = directory.GetFiles(); } else { files = directory.GetFiles(log.FileTypes); } foreach (FileInfo file in files) { try { using (file.OpenRead()) { // try to read - succeeded bool added = _repository.Add(log, file.FullName, file.LastWriteTimeUtc); if (added) { hasChanges = added; } } } catch (Exception ex) { if (_logger.IsErrorEnabled) { _logger.Error(ex, $"File {file.FullName} was not added to the data source log due to an unexpected error."); // can't read } } } } return(hasChanges); }
/// <summary> /// Saves the result to the data dir. /// </summary> /// <param name="data"> /// The log to save. /// </param> public string Save(DataFileLog data) { Guard.ArgumentNotNull(data, nameof(data)); lock (_syncLock) { CreateDirIfNotExists(); var fileName = $@"{DataDir}\flow-{data.EntityType.EntityTypeId}-log.json"; if (_logger.IsInfoEnabled) { _logger.Info($"Saving data source log to {fileName}."); } string body = JsonConvert .SerializeObject(data, new JsonSerializerSettings()); if (File.Exists(fileName)) { if (_logger.IsDebugEnabled) { _logger.Debug("Backing up old archive file."); } // back up older file, don't delete string backFile = fileName + ".bak"; if (File.Exists(backFile)) { File.Delete(backFile); } File.Move(fileName, backFile); } File.WriteAllText(fileName, body); if (_logger.IsInfoEnabled) { _logger.Info($"Saved data source log to {fileName}."); } return(fileName); } }