Updated whois tasks and added whois_json

This commit is contained in:
Priit Tark 2015-04-22 11:51:33 +03:00
parent 3d0d98d9b1
commit 8dae9d9deb
4 changed files with 44 additions and 29 deletions

View file

@ -226,6 +226,9 @@ class Domain < ActiveRecord::Base
domain_statuses.find_by(value: DomainStatus::OK).try(:destroy) domain_statuses.find_by(value: DomainStatus::OK).try(:destroy)
end end
# otherwise domain_statuses are in old state for domain object
domain_statuses.reload
# contacts.includes(:address).each(&:manage_statuses) # contacts.includes(:address).each(&:manage_statuses)
end end
@ -234,14 +237,17 @@ class Domain < ActiveRecord::Base
log[:admin_contacts] = admin_contacts.map(&:attributes) log[:admin_contacts] = admin_contacts.map(&:attributes)
log[:tech_contacts] = tech_contacts.map(&:attributes) log[:tech_contacts] = tech_contacts.map(&:attributes)
log[:nameservers] = nameservers.map(&:attributes) log[:nameservers] = nameservers.map(&:attributes)
log[:registrant] = [registrant.try(:attributes)] log[:registrant] = [registrant.try(:attributes)]
log log
end end
def update_whois_body def update_whois_body
whois = Whois::Body.new(self) whois = Whois::Body.new(self)
self.whois_json = whois.whois_json # validations, callbacks and updated_at are skipped
self.whois_body = whois.whois_body update_columns(
whois_json: whois.whois_json,
whois_body: whois.whois_body
)
end end
def update_whois_server def update_whois_server

View file

@ -19,6 +19,7 @@ ActiveRecord::Schema.define(version: 20150113113236) do
create_table "domains", force: :cascade do |t| create_table "domains", force: :cascade do |t|
t.string "name" t.string "name"
t.text "whois_body" t.text "whois_body"
t.json "whois_json"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end

View file

@ -33,7 +33,7 @@ namespace :db do
task create: [:environment, :load_config] do task create: [:environment, :load_config] do
databases.each do |name| databases.each do |name|
begin begin
puts "\n---------------------------- Create #{name} ----------------------------------------\n" puts "\n------------------------ Create #{name} ---------------------------------------\n"
ActiveRecord::Base.clear_all_connections! ActiveRecord::Base.clear_all_connections!
conf = ActiveRecord::Base.configurations conf = ActiveRecord::Base.configurations
@ -75,7 +75,7 @@ namespace :db do
task load: [:environment, :load_config] do task load: [:environment, :load_config] do
databases.each do |name| databases.each do |name|
begin begin
puts "\n---------------------------- #{name} schema loaded ----------------------------------------\n" puts "\n------------------------ #{name} schema loading -----------------------------\n"
ActiveRecord::Base.clear_all_connections! ActiveRecord::Base.clear_all_connections!
ActiveRecord::Base.establish_connection(name.to_sym) ActiveRecord::Base.establish_connection(name.to_sym)
if ActiveRecord::Base.connection.table_exists?('schema_migrations') if ActiveRecord::Base.connection.table_exists?('schema_migrations')

View file

@ -1,33 +1,41 @@
namespace :whois do namespace :whois do
desc 'Delete whois database data and import all from Registry (fast)' desc 'Regenerate whois_body and whois_json at Registry master database (slow)'
task reset: :environment do task regenerate: :environment do
start = Time.zone.now.to_f start = Time.zone.now.to_f
print "-----> Reset whois database and sync..." print "-----> Regenerate whois_body and whois_json at Registry master database..."
domains = Domain.pluck(:name, :whois_body) Domain.included.find_each(batch_size: 50000).with_index do |d, index|
d.update_whois_body
print '.' if index % 100 == 0
end
puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds"
end
desc 'Delete whois database data and sync with Registry master database (fast)'
task export: :environment do
start = Time.zone.now.to_f
print "-----> Delete whois database data and sync with Registry master database..."
domains = Domain.pluck(:name, :whois_body, :whois_json)
Whois::Domain.delete_all Whois::Domain.delete_all
Whois::Domain.import([:name, :whois_body], domains) Whois::Domain.import([:name, :whois_body, :whois_json], domains)
puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds"
end end
desc 'Sync whois database without reset (slow)' namespace :schema do
task sync: :environment do desc 'Load whois schema'
start = Time.zone.now.to_f task load: [:environment] do
print "-----> Sync whois database..." whois_db = "whois_#{Rails.env}"
Domain.select(:id, :name, :whois_body).find_each(batch_size: 100000).with_index do |d, index| begin
d.update_whois_server puts "\n------------------------ #{whois_db} schema loading ------------------------------\n"
print '.' if index % 100 == 0 ActiveRecord::Base.clear_all_connections!
ActiveRecord::Base.establish_connection(whois_db.to_sym)
if ActiveRecord::Base.connection.table_exists?('schema_migrations')
puts 'Found tables, skip schema load!'
else
load("#{Rails.root}/db/#{schema_file(whois_db)}")
end
rescue => e
puts "\n#{e}"
end
end end
puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds"
end
desc 'Regenerate whois_body at Registry master database (slow)'
task generate: :environment do
start = Time.zone.now.to_f
print "-----> Update Registry records..."
Domain.included.find_each(batch_size: 100000).with_index do |d, index|
d.update_columns(whois_body: d.update_whois_body)
print '.' if index % 100 == 0
end
puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds"
end end
end end