added validation for single domain contacts

This commit is contained in:
olegphenomenon 2022-04-25 14:37:46 +03:00
parent d7df763295
commit 43bd6f2907
3 changed files with 17 additions and 48 deletions

View file

@ -66,9 +66,11 @@ module Actions
result_validation.present? ? result.success = true : result.success = false result_validation.present? ? result.success = true : result.success = false
end end
contacts.each do |contact| contacts.find_in_batches(batch_size: 500) do |contact_batches|
contact_batches.each do |contact|
contact.validation_events.create(validation_event_attrs(result)) contact.validation_events.create(validation_event_attrs(result))
end end
end
rescue ActiveRecord::RecordNotSaved rescue ActiveRecord::RecordNotSaved
logger.info "Cannot save validation result for #{log_object_id}" logger.info "Cannot save validation result for #{log_object_id}"
true true

View file

@ -2,9 +2,9 @@ class VerifyEmailsJob < ApplicationJob
discard_on StandardError discard_on StandardError
def perform(email:, check_level: 'mx') def perform(email:, check_level: 'mx')
# contact_not_found(contact.id) unless contact contact = Contact.find_by(email: email)
contact = Contact.find_by_email(email) return Rails.logger.info "No found #{email} contact" if contact.nil?
return unless filter_check_level(contact) return unless filter_check_level(contact)

View file

@ -18,29 +18,13 @@ namespace :verify_email do
options = RakeOptionParserBoilerplate.process_args(options: options, options = RakeOptionParserBoilerplate.process_args(options: options,
banner: banner, banner: banner,
hash: opts_hash) hash: opts_hash)
# batch_contacts = prepare_contacts(options)
# logger.info 'No contacts to check email selected' and next if batch_contacts.blank?
# batch_contacts.find_in_batches(batch_size: 10_000) do |contacts|
# contacts.each do |contact|
# VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)).perform_later(
# contact: contact,
# check_level: check_level(options)
# ) if filter_check_level(contact)
# end
# end
email_contacts = prepare_contacts(options) email_contacts = prepare_contacts(options)
email_contacts.each do |email| email_contacts.each do |email|
VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)).perform_later( VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)).perform_later(
# contact: contact,
email: email, email: email,
check_level: check_level(options) check_level: check_level(options)
) )
# if filter_check_level(contact)
end end
end end
end end
@ -63,40 +47,23 @@ end
def prepare_contacts(options) def prepare_contacts(options)
if options[:domain_name].present? if options[:domain_name].present?
Rails.logger.info 'NEED TO TODO' Rails.logger.info 'NEED TO TODO'
# contacts_by_domain(options[:domain_name]) contacts_by_domain(options[:domain_name])
else else
time = Time.zone.now - ValidationEvent::VALIDATION_PERIOD time = Time.zone.now - ValidationEvent::VALIDATION_PERIOD
validation_events_ids = ValidationEvent.where('created_at > ?', time).distinct.pluck(:validation_eventable_id) validation_events_ids = ValidationEvent.where('created_at > ?', time).distinct.pluck(:validation_eventable_id)
contacts_ids = Contact.where.not(id: validation_events_ids).pluck(:id) contacts_emails = Contact.where.not(id: validation_events_ids).pluck(:email)
Contact.where(id: contacts_ids + failed_contacts).group_by(&:email).keys # Contact.where(id: contacts_ids + failed_contacts).pluck(:email).uniq
(contacts_emails + failed_email_contacts).uniq
# Contact.all.group_by(&:email).keys
end end
end end
def filter_check_level(contact) def failed_email_contacts
return true unless contact.validation_events.exists?
data = contact.validation_events.order(created_at: :asc).last
return true if data.successful? && data.created_at < (Time.zone.now - ValidationEvent::VALIDATION_PERIOD)
if data.failed?
return false if data.event_data['check_level'] == 'regex'
return true
end
false
end
def failed_contacts
failed_contacts = [] failed_contacts = []
failed_validations_ids = ValidationEvent.failed.distinct.pluck(:validation_eventable_id) failed_validations_ids = ValidationEvent.failed.distinct.pluck(:validation_eventable_id)
contacts = Contact.where(id: failed_validations_ids).includes(:validation_events) contacts = Contact.where(id: failed_validations_ids).includes(:validation_events)
contacts.find_each(batch_size: 10_000) do |contact| contacts.find_each(batch_size: 10_000) do |contact|
failed_contacts << contact.id if filter_check_level(contact) failed_contacts << contact.email
end end
failed_contacts.uniq failed_contacts.uniq
@ -106,7 +73,7 @@ def contacts_by_domain(domain_name)
domain = ::Domain.find_by(name: domain_name) domain = ::Domain.find_by(name: domain_name)
return unless domain return unless domain
domain.contacts domain.contacts.pluck(:email).uniq
end end
def opts_hash def opts_hash