/// <summary> /// The call back function to check if we are parsing that directory or not. /// </summary> /// <param name="directory"></param> /// <returns></returns> private bool ParseDirectory(DirectoryInfo directory) { if (!helper.File.CanReadDirectory(directory)) { _logger.Warning($"Cannot Parse Directory: {directory.FullName}"); return false; } if (!helper.File.IsSubDirectory(IgnorePaths, directory)) { // we will be parsing it and the sub-directories. return true; } // we are ignoreing this. _logger.Verbose($"Ignoring: {directory.FullName} and sub-directories."); // we are not parsing this return false; }
/// <inheritdoc /> public async Task <long> WorkAsync(CancellationToken token) { // check if we are active at the current time. if (!_active.IsActive()) { // we are not active ... so we have nothing to do. _logger.Verbose("Maintenance Process ignored, out of active hours."); return(0); } var stopwatch = new Stopwatch(); stopwatch.Start(); var success = false; try { _logger.Information("Started Maintenance Process."); await _persister.MaintenanceAsync(token).ConfigureAwait(false); await _parser.MaintenanceAsync(token).ConfigureAwait(false); // it worked success = true; } catch (OperationCanceledException) { // nothing to log throw; } catch (Exception e) { _logger.Exception("Error while processing Maintenance.", e); throw; } finally { _logger.Information(success ? $"Complete Maintenance Process (Time Elapsed: {stopwatch.Elapsed:g})." : $"Complete Maintenance Process with errors (Time Elapsed: {stopwatch.Elapsed:g})."); } return(0); }
/// <summary> /// Called when the token has been cancelled. /// </summary> private void TokenCancellation() { _logger.Verbose("Stopping Events parser"); Stop(); _logger.Verbose("Done events parser"); }
/// <summary> /// Check if we want to parse this directory or not. /// </summary> /// <param name="parent"></param> /// <param name="directories"></param> /// <param name="lastAccessTimeUtc">The last time we checked the accessed time.</param> /// <param name="token"></param> /// <returns></returns> private async Task <bool> PersistDirectoriesAsync( FileSystemInfo parent, IEnumerable <DirectoryInfo> directories, DateTime lastAccessTimeUtc, CancellationToken token ) { // start the stopwatch var stopwatch = new Stopwatch(); stopwatch.Start(); // get all the changed or updated files. var transaction = await _persister.BeginWrite(token).ConfigureAwait(false); if (null == transaction) { throw new Exception("Unable to get transaction!"); } // get the list of directories that have changed. var changedDirectories = new List <DirectoryInfo>(); // all the newly created directories var createdDirectories = new List <DirectoryInfo>(); foreach (var directory in directories) { try { // try and persist this one directory. switch (await GetDirectoryUpdateType(directory, lastAccessTimeUtc, token).ConfigureAwait(false)) { case UpdateType.None: // ignore break; case UpdateType.Created: createdDirectories.Add(directory); break; case UpdateType.Changed: changedDirectories.Add(directory); break; // case UpdateType.Deleted: throw new ArgumentException($"How can a newly parsed directory, {directory.FullName} be deleted??"); default: throw new ArgumentOutOfRangeException(); } } catch (Exception) { _persister.Rollback(transaction); throw; } } // commit the transaction one last time // if we have not done it already. _persister.Commit(transaction); // update the changed directorues await PersistChangedDirectories(changedDirectories, token).ConfigureAwait(false); // process the directories. await ProcessCreatedDirectories(createdDirectories, token).ConfigureAwait(false); // log that we are done. var sb = new StringBuilder(); sb.AppendLine($"Finishing directory parsing: {parent.FullName}"); sb.AppendLine($" Created: {createdDirectories.Count} directories"); sb.Append($" Changed: {changedDirectories.Count} directories"); _logger.Verbose(sb.ToString()); // stop the watch and log how many items we found. stopwatch.Stop(); _logger.Information($"Completed directory parsing: {parent.FullName} (Time Elapsed: {stopwatch.Elapsed:g})"); // all done return(true); }