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)
end
# otherwise domain_statuses are in old state for domain object
domain_statuses.reload
# contacts.includes(:address).each(&:manage_statuses)
end
@ -240,8 +243,11 @@ class Domain < ActiveRecord::Base
def update_whois_body
whois = Whois::Body.new(self)
self.whois_json = whois.whois_json
self.whois_body = whois.whois_body
# validations, callbacks and updated_at are skipped
update_columns(
whois_json: whois.whois_json,
whois_body: whois.whois_body
)
end
def update_whois_server

View file

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

View file

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

View file

@ -1,33 +1,41 @@
namespace :whois do
desc 'Delete whois database data and import all from Registry (fast)'
task reset: :environment do
desc 'Regenerate whois_body and whois_json at Registry master database (slow)'
task regenerate: :environment do
start = Time.zone.now.to_f
print "-----> Reset whois database and sync..."
domains = Domain.pluck(:name, :whois_body)
print "-----> Regenerate whois_body and whois_json at Registry master database..."
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.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"
end
desc 'Sync whois database without reset (slow)'
task sync: :environment do
start = Time.zone.now.to_f
print "-----> Sync whois database..."
Domain.select(:id, :name, :whois_body).find_each(batch_size: 100000).with_index do |d, index|
d.update_whois_server
print '.' if index % 100 == 0
namespace :schema do
desc 'Load whois schema'
task load: [:environment] do
whois_db = "whois_#{Rails.env}"
begin
puts "\n------------------------ #{whois_db} schema loading ------------------------------\n"
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
puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds"
rescue => e
puts "\n#{e}"
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