private async Task <IList <INumericRangeBlockContext> > GetNumericRangeBlocksAsync(ITaskExecutionContext taskExecutionContext) { using (var cs = taskExecutionContext.CreateCriticalSection()) { if (await cs.TryStartAsync()) { long startNumber; var lastBlock = await taskExecutionContext.GetLastNumericRangeBlockAsync(LastBlockOrder.LastCreated); var maxJourneyId = await _travelDataService.GetMaxJourneyIdAsync(); // if this is the first run then just process the last 1000 if (lastBlock == null) { startNumber = maxJourneyId - 1000; } // if there is no new data then just return any old blocks that have failed or died else if (lastBlock.EndNumber == maxJourneyId) { return(await taskExecutionContext.GetNumericRangeBlocksAsync(x => x.OnlyOldNumericBlocks())); } // startNumber is the next unprocessed id else { startNumber = lastBlock.EndNumber + 1; } int maxBlockSize = 500; return(await taskExecutionContext.GetNumericRangeBlocksAsync(x => x.WithRange(startNumber, maxJourneyId, maxBlockSize))); } throw new Exception("Could not acquire a critical section, aborted task"); } }