private void ProcessCandidates(IAsyncCursor <AuditItem> cursor, long expectedCount, CancellationToken cancellationToken) { var count = 0; while (cursor.MoveNextAsync(cancellationToken).Result&& !cancellationToken.IsCancellationRequested) { var batch = cursor.Current.ToList(); if (batch.Count == 0) { continue; } _logService.Info($"Processing {batch.Count} audit items"); var maxAuditEvent = batch.Max(a => a.EventDate); //Process candidate deltions var candidateDeletions = batch.Where(a => a.EventType == AuditEventTypes.HardDeleteCandidateUser); var candidateGuids = _candidateRepository.GetCandidateIdsByGuid(candidateDeletions.Select(a => a.PrimaryEntityId)); count += batch.Count; _logService.Info($"Processing {batch.Count} candidate deletions and {candidateGuids.Count} deleted candidates"); BulkDelete(candidateGuids); var syncParams = _syncRepository.GetSyncParams(); syncParams.LastAuditEventDate = maxAuditEvent; _syncRepository.SetAuditEventSyncParams(syncParams); var percentage = ((double)count / expectedCount) * 100; _logService.Info($"Processed batch of {batch.Count} candidate deletions and {candidateGuids.Count} deleted candidates out of {expectedCount} in total. {Math.Round(percentage, 2)}% complete. LastAuditEventDate: {syncParams.LastAuditEventDate}"); } }