mirror of
https://github.com/internetee/registry.git
synced 2025-08-11 12:09:34 +02:00
Improve check force delete query
This commit is contained in:
parent
ee76deaf52
commit
27fbf224f9
2 changed files with 23 additions and 25 deletions
|
@ -1,29 +1,8 @@
|
||||||
desc 'Check Force Delete'
|
desc 'Check Force Delete'
|
||||||
task :check_force_delete, %i[batch batch_size batches_delay] => :environment do |_t, args|
|
task check_force_delete: :environment do
|
||||||
args.with_defaults(batch: false, batch_size: 1_000, batches_delay: 15)
|
|
||||||
|
|
||||||
batch = ActiveModel::Type::Boolean.new.cast(args[:batch])
|
validations = ValidationEvent.select(:validation_eventable_id).failed.where(validation_eventable_type: 'Contact').group(:validation_eventable_id)
|
||||||
batch_size = args[:batch_size].to_i
|
invalid_contact_ids = (validations.mx.having("count(event_data ->> 'success') > 2") + validations.regex).pluck(:validation_eventable_id)
|
||||||
batches_delay = args[:batches_delay].to_i.minutes
|
|
||||||
|
|
||||||
invalid_contacts = Contact.joins(:validation_events).select do |contact|
|
CheckForceDeleteJob.perform_later(invalid_contact_ids)
|
||||||
events = contact.validation_events
|
|
||||||
mx = events.mx.select(&:failed?).count >= ValidationEvent::MX_CHECK
|
|
||||||
regex = events.regex.select(&:failed?).present?
|
|
||||||
|
|
||||||
(contact.need_to_start_force_delete? || contact.need_to_lift_force_delete?) && (mx || regex)
|
|
||||||
end.uniq
|
|
||||||
|
|
||||||
if batch
|
|
||||||
waiting_minutes = 0.minutes
|
|
||||||
|
|
||||||
invalid_contacts.find_in_batches(batch_size: batch_size) do |contact_batches|
|
|
||||||
CheckForceDeleteJob.set(wait: waiting_minutes).perform_later(contact_batches)
|
|
||||||
waiting_minutes += batches_delay
|
|
||||||
end
|
|
||||||
else
|
|
||||||
invalid_contacts.each do |contact|
|
|
||||||
CheckForceDeleteJob.perform_later([contact.id])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,6 +38,25 @@ class CheckForceDeleteTaskTest < ActiveSupport::TestCase
|
||||||
assert_enqueued_with(job: CheckForceDeleteJob, args: [[@invalid_contact.id]])
|
assert_enqueued_with(job: CheckForceDeleteJob, args: [[@invalid_contact.id]])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_not_enque_force_delete
|
||||||
|
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([])
|
||||||
|
|
||||||
|
action = Actions::EmailCheck.new(email: @contact.email,
|
||||||
|
validation_eventable: @contact,
|
||||||
|
check_level: 'mx')
|
||||||
|
2.times do
|
||||||
|
action.call
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_enqueued_jobs 0
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def run_task
|
def run_task
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue