Merge branch 'story/113066359-optimization' into staging

# Conflicts:
#	app/jobs/regenerate_registrar_whoises_job.rb
#	app/jobs/regenerate_whois_record_job.rb
#	app/models/blocked_domain.rb
This commit is contained in:
Vladimir Krylov 2016-02-11 12:26:58 +02:00
commit af28b9e3f2
7 changed files with 82 additions and 64 deletions

View file

@ -3,8 +3,8 @@ class RegenerateRegistrarWhoisesJob < Que::Job
# no return as we want restart job if fails # no return as we want restart job if fails
registrar = Registrar.find(registrar_id) registrar = Registrar.find(registrar_id)
registrar.whois_records.select(:id).find_in_batches(batch_size: 20) do |group| registrar.whois_records.select(:name).find_in_batches(batch_size: 20) do |group|
RegenerateWhoisRecordJob.enqueue group.map(&:id), :id UpdateWhoisRecordJob.enqueue group.map(&:name), 'domain'
end end
end end
end end

View file

@ -1,10 +0,0 @@
class RegenerateWhoisRecordJob < Que::Job
def run(ids, attr = :id)
ids.each do |id|
record = WhoisRecord.find_by(attr => id)
return unless record
record.save
end
end
end

View file

@ -1,16 +1,52 @@
class UpdateWhoisRecordJob < Que::Job class UpdateWhoisRecordJob < Que::Job
def run(ids, type) def run(names, type)
klass = case type klass = case type
when 'reserved'then ReservedDomain when 'reserved'then ReservedDomain
when 'blocked' then BlockedDomain when 'blocked' then BlockedDomain
else Domain when 'domain' then Domain
end end
ids.each do |id| Array(names).each do |name|
record = klass.find_by(id: id) record = klass.find_by(name: name)
next unless record if record
record.update_whois_record send "update_#{type}", record
else
send "delete_#{type}", name
end
end end
end end
def update_domain(domain)
domain.whois_record ? domain.whois_record.save : domain.create_whois_record
end
def update_reserved(record)
record.generate_data
end
def update_blocked(record)
update_reserved(record)
end
# 1. deleting own
# 2. trying to regenerate reserved in order domain is still in the list
def delete_domain(name)
WhoisRecord.where(name: name).destroy_all
BlockedDomain.find_by(name: name).try(:generate_data)
ReservedDomain.find_by(name: name).try(:generate_data)
end
def delete_reserved(name)
Domain.where(name: name).any?
Whois::Record.where(name: name).delete_all
end
def delete_blocked(name)
delete_reserved(name)
end
end end

View file

@ -1,7 +1,8 @@
class BlockedDomain < ActiveRecord::Base class BlockedDomain < ActiveRecord::Base
include Versions include Versions
before_save :generate_data before_save :generate_data
before_destroy :remove_data after_destroy :remove_data
validates :name, domain_name: true, uniqueness: true validates :name, domain_name: true, uniqueness: true
@ -22,19 +23,14 @@ class BlockedDomain < ActiveRecord::Base
def generate_data def generate_data
return if Domain.where(name: name).any? return if Domain.where(name: name).any?
@json = generate_json wr = Whois::Record.find_or_initialize_by(name: name)
@body = generate_body wr.json = @json = generate_json # we need @json to bind to class
update_whois_server wr.body = generate_body
wr.save
end end
alias_method :update_whois_record, :generate_data alias_method :update_whois_record, :generate_data
def update_whois_server
wr = Whois::Record.find_or_initialize_by(name: name)
wr.body = @body
wr.json = @json
wr.save
end
def generate_body def generate_body
template = Rails.root.join("app/views/for_models/whois_other.erb".freeze) template = Rails.root.join("app/views/for_models/whois_other.erb".freeze)
@ -49,8 +45,6 @@ class BlockedDomain < ActiveRecord::Base
end end
def remove_data def remove_data
return if Domain.where(name: name).any? UpdateWhoisRecordJob.enqueue name, 'blocked'
Whois::Record.where(name: name).delete_all
end end
end end

View file

@ -45,7 +45,7 @@ class Domain < ActiveRecord::Base
has_many :dnskeys, dependent: :destroy has_many :dnskeys, dependent: :destroy
has_many :keyrelays has_many :keyrelays
has_one :whois_record, dependent: :destroy has_one :whois_record # destroyment will be done in after_commit
accepts_nested_attributes_for :dnskeys, allow_destroy: true accepts_nested_attributes_for :dnskeys, allow_destroy: true
@ -89,14 +89,11 @@ class Domain < ActiveRecord::Base
true true
end end
after_save :update_whois_record after_commit :update_whois_record
after_create :update_reserved_domains after_create :update_reserved_domains
def update_reserved_domains def update_reserved_domains
return unless in_reserved_list? ReservedDomain.new_password_for(name) if in_reserved_list?
rd = ReservedDomain.by_domain(name).first
rd.password = SecureRandom.hex
rd.save
end end
validates :name_dirty, domain_name: true, uniqueness: true validates :name_dirty, domain_name: true, uniqueness: true
@ -286,7 +283,7 @@ class Domain < ActiveRecord::Base
end end
def in_reserved_list? def in_reserved_list?
ReservedDomain.pw_for(name).present? @in_reserved_list ||= ReservedDomain.by_domain(name).any?
end end
def pending_transfer def pending_transfer
@ -758,7 +755,7 @@ class Domain < ActiveRecord::Base
end end
def update_whois_record def update_whois_record
whois_record.blank? ? create_whois_record : whois_record.save UpdateWhoisRecordJob.enqueue name, 'domain'
end end
def status_notes_array=(notes) def status_notes_array=(notes)

View file

@ -2,7 +2,8 @@ class ReservedDomain < ActiveRecord::Base
include Versions # version/reserved_domain_version.rb include Versions # version/reserved_domain_version.rb
before_save :fill_empty_passwords before_save :fill_empty_passwords
before_save :generate_data before_save :generate_data
before_destroy :remove_data after_destroy :remove_data
validates :name, domain_name: true, uniqueness: true validates :name, domain_name: true, uniqueness: true
@ -21,38 +22,40 @@ class ReservedDomain < ActiveRecord::Base
def any_of_domains names def any_of_domains names
where(name: names) where(name: names)
end end
end
def new_password_for name
record = by_domain(name).first
return unless record
def fill_empty_passwords record.regenerate_password
record.save
if self.password.empty?
self.password = SecureRandom.hex
end end
end end
def name= val def name= val
super SimpleIDN.to_unicode(val) super SimpleIDN.to_unicode(val)
end end
def fill_empty_passwords
regenerate_password if self.password.blank?
end
def regenerate_password
self.password = SecureRandom.hex
end
def generate_data def generate_data
return if Domain.where(name: name).any? return if Domain.where(name: name).any?
@json = generate_json
@body = generate_body
update_whois_server
end
alias_method :update_whois_record, :generate_data
def update_whois_server
wr = Whois::Record.find_or_initialize_by(name: name) wr = Whois::Record.find_or_initialize_by(name: name)
wr.body = @body wr.json = @json = generate_json # we need @json to bind to class
wr.json = @json wr.body = generate_body
wr.save wr.save
end end
alias_method :update_whois_record, :generate_data
def generate_body def generate_body
template = Rails.root.join("app/views/for_models/whois_other.erb".freeze) template = Rails.root.join("app/views/for_models/whois_other.erb".freeze)
@ -67,9 +70,7 @@ class ReservedDomain < ActiveRecord::Base
end end
def remove_data def remove_data
return if Domain.where(name: name).any? UpdateWhoisRecordJob.enqueue name, 'reserved'
Whois::Record.where(name: name).delete_all
end end
end end

View file

@ -8,17 +8,17 @@ namespace :whois do
print "\n-----> Update domains whois_records" print "\n-----> Update domains whois_records"
Domain.find_in_batches.each do |group| Domain.find_in_batches.each do |group|
UpdateWhoisRecordJob.enqueue group.map(&:id), 'domain' UpdateWhoisRecordJob.enqueue group.map(&:name), 'domain'
end end
print "\n-----> Update blocked domains whois_records" print "\n-----> Update blocked domains whois_records"
BlockedDomain.find_in_batches.each do |group| BlockedDomain.find_in_batches.each do |group|
UpdateWhoisRecordJob.enqueue group.map(&:id), 'blocked' UpdateWhoisRecordJob.enqueue group.map(&:name), 'blocked'
end end
print "\n-----> Update reserved domains whois_records" print "\n-----> Update reserved domains whois_records"
ReservedDomain.find_in_batches.each do |group| ReservedDomain.find_in_batches.each do |group|
UpdateWhoisRecordJob.enqueue group.map(&:id), 'reserved' UpdateWhoisRecordJob.enqueue group.map(&:name), 'reserved'
end end
end end