Skip to content

Commit

Permalink
Merge pull request #2294 from internetee/2236-removing-old-validation…
Browse files Browse the repository at this point in the history
…-events

Removing expired records from the validations table
  • Loading branch information
vohmar authored May 19, 2022
2 parents a7d05e2 + de53f04 commit c1ecdbc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
8 changes: 2 additions & 6 deletions app/interactions/actions/email_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ def save_result(result)

if !result.success && @check_level == 'mx'
result_validation = Actions::AAndAaaaEmailValidation.call(email: @email, value: 'A')
output_a_and_aaaa_validation_results(email: @email,
result: result_validation,
type: 'A')
output_a_and_aaaa_validation_results(email: @email, result: result_validation, type: 'A')

result_validation = Actions::AAndAaaaEmailValidation.call(email: @email, value: 'AAAA') if result_validation.empty?
output_a_and_aaaa_validation_results(email: @email,
result: result_validation,
type: 'AAAA')
output_a_and_aaaa_validation_results(email: @email, result: result_validation, type: 'AAAA')
result.success = result_validation.present?
end

Expand Down
8 changes: 0 additions & 8 deletions app/jobs/verify_emails_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def perform(email:, check_level: 'mx')

private

def contact_not_found(contact_id)
raise StandardError, "Contact with contact_id #{contact_id} not found"
end

def validate_check_level(check_level)
return if valid_check_levels.include? check_level

Expand All @@ -38,10 +34,6 @@ def valid_check_levels
ValidationEvent::VALID_CHECK_LEVELS
end

def get_validation_results(contact)
ValidationEvent.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
end

def filter_check_level(contact)
return true unless contact.validation_events.exists?

Expand Down
8 changes: 5 additions & 3 deletions app/models/validation_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ValidationEvent < ApplicationRecord

belongs_to :validation_eventable, polymorphic: true

scope :recent, -> { where('created_at < ?', Time.zone.now - VALIDATION_PERIOD) }
scope :old_records, -> { where('created_at < ?', Time.zone.now - VALIDATION_PERIOD) }
scope :successful, -> { where(success: true) }
scope :failed, -> { where(success: false) }
scope :regex, -> { where('event_data @> ?', { 'check_level': 'regex' }.to_json) }
Expand All @@ -36,8 +36,10 @@ class ValidationEvent < ApplicationRecord
scope :by_object, ->(object) { where(validation_eventable: object) }

def self.validated_ids_by(klass)
recent.successful.where('validation_eventable_type = ?', klass)
.pluck(:validation_eventable_id)
old_records
.successful
.where('validation_eventable_type = ?', klass)
.pluck(:validation_eventable_id)
end

def failed?
Expand Down
11 changes: 3 additions & 8 deletions lib/tasks/verify_email.rake
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ namespace :verify_email do
options = RakeOptionParserBoilerplate.process_args(options: options,
banner: banner,
hash: opts_hash)
ValidationEvent.old_records.destroy_all
email_contacts = prepare_contacts(options)
email_contacts.each do |email|
VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)).perform_later(
email: email,
check_level: check_level(options)
)
VerifyEmailsJob.set(wait_until: spam_protect_timeout(options))
.perform_later(email: email, check_level: check_level(options))
end
end
end
Expand All @@ -40,10 +39,6 @@ def spam_protect_timeout(options)
spam_protect(options) ? 0.seconds : SPAM_PROTECT_TIMEOUT
end

def logger
@logger ||= ActiveSupport::TaggedLogging.new(Syslog::Logger.new('registry'))
end

def prepare_contacts(options)
if options[:domain_name].present?
contacts_by_domain(options[:domain_name])
Expand Down
25 changes: 22 additions & 3 deletions test/tasks/emails/verify_email_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,32 @@ def test_fd_should_not_removed_if_change_email_to_another_invalid_one
)
Spy.on_instance_method(Actions::EmailCheck, :check_email).and_return(trumail_results)

1.times do
run_task
end
run_task

assert contact.domains.last.force_delete_scheduled?
end

def test_should_remove_old_validation_records
trumail_results = OpenStruct.new(success: false,
email: @contact.email,
domain: 'box.tests',
errors: { mx: 'target host(s) not found' })

Spy.on_instance_method(Actions::EmailCheck, :check_email).and_return(trumail_results)
Spy.on_instance_method(Actions::AAndAaaaEmailValidation, :call).and_return([true])

Actions::EmailCheck.new(email: @contact.email,
validation_eventable: @contact,
check_level: 'regex').call

travel_to(Time.zone.now + ::ValidationEvent::VALIDATION_PERIOD + 1.minute)
assert_equal ValidationEvent.old_records.count, 1

run_task

assert_predicate ValidationEvent.old_records.count, :zero?
end

def run_task
perform_enqueued_jobs do
Rake::Task['verify_email:check_all'].execute
Expand Down

0 comments on commit c1ecdbc

Please sign in to comment.