private async Task <long> WorkAsync(IConnectionFactory factory, CancellationToken token) { try { // then get _all_ the file updates that we want to do. var pendingUpdates = await GetPendingFolderUpdatesAndMarkDirectoryProcessedAsync(factory, token).ConfigureAwait(false); if (null != pendingUpdates) { // process the update. await ProcessFolderUpdatesAsync(factory, pendingUpdates, token).ConfigureAwait(false); } _persister.Commit(factory); // we processed one update return(pendingUpdates?.Count ?? 0); } catch (OperationCanceledException) { _persister.Rollback(factory); _logger.Warning("Directory processor: Received cancellation request - Directories Processor - Work"); throw; } catch (Exception e) { _persister.Rollback(factory); _logger.Exception("Directory processor: ", e); throw; } }
/// <inheritdoc /> public async Task MaintenanceAsync(CancellationToken token) { lock (_taskLock) { // are we still running the start? if (!_runningTask?.IsCompleted ?? true) { return; } } var factory = await _persister.BeginWrite(token).ConfigureAwait(false); try { const string configParse = "maintenance.parse"; var lastParse = await _persister.Config.GetConfigValueAsync(configParse, DateTime.MinValue, factory, token).ConfigureAwait(false); // between 3 and 5 hours to prevent running at the same time as others. var randomHours = (new Random(DateTime.UtcNow.Millisecond)).Next(3, 5); if ((DateTime.UtcNow - lastParse).TotalHours > randomHours) { // // Do maintenance work // _logger.Information("Maintenance files/folders parser"); // save the date and commit ... because the parser needs its own factory. await _persister.Config.SetConfigValueAsync(configParse, DateTime.UtcNow, factory, token).ConfigureAwait(false); } _persister.Commit(factory); } catch (Exception e) { _persister.Rollback(factory); _logger.Exception("There was an exception re-parsing the directories.", e); throw; } }