/// <summary> /// Processes the blocks dictated by the progress service /// </summary> /// <returns>Returns the BlockRange processed else null if there were no blocks to process</returns> public async Task <BlockRange?> ProcessLatestBlocksAsync(CancellationToken cancellationToken) { _logger.LogInformation("Getting block number range to process"); var nullableRange = await _progressService .GetNextBlockRangeToProcessAsync(_maxNumberOfBlocksPerBatch) .ConfigureAwait(false); if (nullableRange == null) { _logger.LogInformation("No block range to process - the most recent block may already have been processed"); return(null); } var range = nullableRange.Value; _logger.LogInformation($"Processing Block Range. from: {range.From} to {range.To}"); await _processor.ProcessAsync(range, cancellationToken) .ConfigureAwait(false); _logger.LogInformation($"Updating current process progress to: {range.To}"); await _progressService.SaveLastBlockProcessedAsync(range.To) .ConfigureAwait(false); return(range); }
/// <summary> /// Processes the blocks dictated by the progress service /// </summary> /// <returns>Returns the BlockRange processed else null if there were no blocks to process</returns> public async Task <BlockRange?> ProcessOnceAsync(CancellationToken cancellationToken = default) { try { _log.RetrievingBlockNumberRange(); var nullableRange = await _progressService .GetNextBlockRangeToProcessAsync(MaxNumberOfBlocksPerBatch) .ConfigureAwait(false); if (nullableRange == null) { _log.NoBlocksToProcess(); return(null); } var range = nullableRange.Value; _log.ProcessingBlockRange(range); await _processor.ProcessAsync(range, cancellationToken) .ConfigureAwait(false); _log.UpdatingBlockProgress(range); await _progressService.SaveLastBlockProcessedAsync(range.To) .ConfigureAwait(false); return(range); } catch (TooManyRecordsException ex) { _log.TooManyRecords(ex); if (MaxNumberOfBlocksPerBatch > 1 && EnableAutoBatchResizing) // try again with a smaller batch size { uint newBatchLimit = MaxNumberOfBlocksPerBatch / 2; _log.ChangingBlocksPerBatch(MaxNumberOfBlocksPerBatch, newBatchLimit); MaxNumberOfBlocksPerBatch = newBatchLimit; return(await ProcessOnceAsync(cancellationToken).ConfigureAwait(false)); } throw; } }