Move DomainCron#clean_expired_pendings to interactor

This commit is contained in:
Alex Sherman 2020-12-01 15:23:23 +05:00
parent 42b9adbf44
commit e8fa79304f
5 changed files with 112 additions and 33 deletions

View file

@ -0,0 +1,10 @@
module Domains
module ExpiredPendings
class Base < ActiveInteraction::Base
def to_stdout(message)
time = Time.zone.now.utc
STDOUT << "#{time} - #{message}\n" unless Rails.env.test?
end
end
end
end

View file

@ -0,0 +1,35 @@
module Domains
module ExpiredPendings
class CleanAll < Base
def execute
to_stdout('Clean expired domain pendings')
::PaperTrail.request.whodunnit = "cron - #{self.class.name}"
count = 0
expired_pending_domains.each do |domain|
log_error(domain) && next unless need_to_be_cleared?(domain)
count += 1
Domains::ExpiredPendings::ProcessClean.run(domain: domain)
end
to_stdout("Successfully cancelled #{count} domain pendings")
end
private
def need_to_be_cleared?(domain)
domain.pending_update? || domain.pending_delete? || domain.pending_delete_confirmation?
end
def log_error(domain)
to_stdout("ISSUE: DOMAIN #{domain.id}: #{domain.name} IS IN EXPIRED PENDING LIST, "\
'but no pendingDelete/pendingUpdate state present!')
end
def expired_pending_domains
expire_at = Setting.expire_pending_confirmation.hours.ago
Domain.where('registrant_verification_asked_at <= ?', expire_at)
end
end
end
end

View file

@ -0,0 +1,60 @@
module Domains
module ExpiredPendings
class ProcessClean < Base
object :domain,
class: Domain
def execute
check_notify
clean_pendings
to_stdout("DomainCron.clean_expired_pendings: ##{domain.id} (#{domain.name})")
UpdateWhoisRecordJob.enqueue domain.name, 'domain'
end
private
def notify_pending_update
RegistrantChangeMailer.expired(domain: domain,
registrar: domain.registrar,
registrant: domain.registrant).deliver_later
end
def notify_pending_delete
DomainDeleteMailer.expired(domain).deliver_later
end
def clean_pendings
clean_verification_data
domain.pending_json = {}
clean_statuses
domain.save
end
def statuses_to_clean
[DomainStatus::PENDING_DELETE_CONFIRMATION,
DomainStatus::PENDING_UPDATE,
DomainStatus::PENDING_DELETE]
end
def clean_statuses
domain.statuses = domain.statuses - statuses_to_clean
domain.status_notes[DomainStatus::PENDING_UPDATE] = ''
domain.status_notes[DomainStatus::PENDING_DELETE] = ''
end
def clean_verification_data
domain.registrant_verification_token = nil
domain.registrant_verification_asked_at = nil
end
def check_notify
notify_pending_update if domain.pending_update?
return unless domain.pending_delete? || domain.pending_delete_confirmation?
notify_pending_delete
end
end
end
end

View file

@ -1,36 +1,6 @@
class DomainCron
def self.clean_expired_pendings
STDOUT << "#{Time.zone.now.utc} - Clean expired domain pendings\n" unless Rails.env.test?
::PaperTrail.request.whodunnit = "cron - #{__method__}"
expire_at = Setting.expire_pending_confirmation.hours.ago
count = 0
expired_pending_domains = Domain.where('registrant_verification_asked_at <= ?', expire_at)
expired_pending_domains.each do |domain|
unless domain.pending_update? || domain.pending_delete? || domain.pending_delete_confirmation?
msg = "#{Time.zone.now.utc} - ISSUE: DOMAIN #{domain.id}: #{domain.name} IS IN EXPIRED PENDING LIST, " \
"but no pendingDelete/pendingUpdate state present!\n"
STDOUT << msg unless Rails.env.test?
next
end
count += 1
if domain.pending_update?
RegistrantChangeExpiredEmailJob.enqueue(domain.id)
end
if domain.pending_delete? || domain.pending_delete_confirmation?
DomainDeleteMailer.expired(domain).deliver_now
end
domain.preclean_pendings
domain.clean_pendings!
unless Rails.env.test?
STDOUT << "#{Time.zone.now.utc} DomainCron.clean_expired_pendings: ##{domain.id} (#{domain.name})\n"
end
UpdateWhoisRecordJob.enqueue domain.name, 'domain'
end
STDOUT << "#{Time.zone.now.utc} - Successfully cancelled #{count} domain pendings\n" unless Rails.env.test?
count
Domains::ExpiredPendings::CleanAll.run!
end
def self.start_expire_period

View file

@ -19,7 +19,9 @@ class DomainCronTest < ActiveSupport::TestCase
registrant_verification_token: 'test',
statuses: [DomainStatus::PENDING_DELETE_CONFIRMATION])
perform_enqueued_jobs do
DomainCron.clean_expired_pendings
end
assert_emails 1
end
@ -84,7 +86,9 @@ class DomainCronTest < ActiveSupport::TestCase
assert @domain.pending_update?
@domain.reload
perform_enqueued_jobs do
DomainCron.clean_expired_pendings
end
@domain.reload
assert_not @domain.pending_update?