Esempio n. 1
0
        public async Task StartFilterAsync(CancellationToken cancellationToken)
        {
            var emails = new List <EmailContent>();
            var validationHistories = new List <ValidationHistory>();

            while (true)
            {
                var isEmpty = _emailContentQueue.IsEmpty;
                if (!isEmpty)
                {
                    EmailContent email;
                    var          result = _emailContentQueue.TryDequeue(out email);
                    if (result && email != null)
                    {
                        // Get FilterResults from filters
                        var filterResults = Filters.Select(s => s.CheckMail(email)).ToList();
                        email.Status = filterResults.Any(s => s.Status == EmailStatus.Violated)
                            ? EmailStatus.Violated
                            : EmailStatus.NotViolated;

                        // Add email to the template list
                        emails.Add(email);

                        // Generate ValidationHistory Object
                        var validationHistory = GetValidationHistory(email, filterResults);

                        // Add ValidationHistory to the template list
                        validationHistories.Add(validationHistory);

                        // Update working list
                        DequeueProgress.Report(email);
                    }
                }

                // If queue is empty or there are 100 emails in the queue => Save data into the storage
                if (isEmpty || emails.Count >= 100)
                {
                    await _mailRepository.SaveCheckedEmailsAsync(emails);

                    await _validationHistoryRepository.AddRangeAsync(validationHistories);

                    emails.Clear();
                    validationHistories.Clear();
                    if (isEmpty)
                    {
                        await Task.Delay(1000, cancellationToken);
                    }
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                }
            }
        }