Improve domain's outzone and delete logic

- Encapsulate outzone and delete candidate logic into domain model
- Fix the logic itself

Fixes #238
This commit is contained in:
Artur Beljajev 2016-11-08 02:30:20 +02:00
parent e653d8b56e
commit aa29c781f6
3 changed files with 58 additions and 8 deletions

View file

@ -11,6 +11,9 @@ class Domain < ActiveRecord::Base
attr_accessor :legal_document_id attr_accessor :legal_document_id
alias_attribute :outzone_time, :outzone_at
alias_attribute :delete_time, :delete_at
# TODO: whois requests ip whitelist for full info for own domains and partial info for other domains # TODO: whois requests ip whitelist for full info for own domains and partial info for other domains
# TODO: most inputs should be trimmed before validatation, probably some global logic? # TODO: most inputs should be trimmed before validatation, probably some global logic?
@ -739,5 +742,13 @@ class Domain < ActiveRecord::Base
def self.redemption_grace_period def self.redemption_grace_period
Setting.redemption_grace_period.days Setting.redemption_grace_period.days
end end
def self.outzone_candidates
where("#{attribute_alias(:outzone_time)} < ?", Time.zone.now)
end
def self.delete_candidates
where("#{attribute_alias(:delete_time)} < ?", Time.zone.now)
end
end end
# rubocop: enable Metrics/ClassLength # rubocop: enable Metrics/ClassLength

View file

@ -53,10 +53,12 @@ class DomainCron
STDOUT << "#{Time.zone.now.utc} - Setting server_hold to domains\n" unless Rails.env.test? STDOUT << "#{Time.zone.now.utc} - Setting server_hold to domains\n" unless Rails.env.test?
::PaperTrail.whodunnit = "cron - #{__method__}" ::PaperTrail.whodunnit = "cron - #{__method__}"
d = Domain.where('outzone_at <= ?', Time.zone.now)
domains = Domain.outzone_candidates
marked = 0 marked = 0
real = 0 real = 0
d.each do |domain|
domains.each do |domain|
next unless domain.server_holdable? next unless domain.server_holdable?
real += 1 real += 1
domain.statuses << DomainStatus::SERVER_HOLD domain.statuses << DomainStatus::SERVER_HOLD
@ -96,16 +98,18 @@ class DomainCron
c = 0 c = 0
Domain.where('delete_at <= ?', Time.zone.now.end_of_day.utc).each do |x| domains = Domain.delete_candidates
next unless x.delete_candidateable?
x.statuses << DomainStatus::DELETE_CANDIDATE domains.each do |domain|
next unless domain.delete_candidateable?
domain.statuses << DomainStatus::DELETE_CANDIDATE
# If domain successfully saved, add it to delete schedule # If domain successfully saved, add it to delete schedule
if x.save(validate: false) if domain.save(validate: false)
::PaperTrail.whodunnit = "cron - #{__method__}" ::PaperTrail.whodunnit = "cron - #{__method__}"
DomainDeleteJob.enqueue(x.id, run_at: rand(((24*60) - (DateTime.now.hour * 60 + DateTime.now.minute))).minutes.from_now) DomainDeleteJob.enqueue(domain.id, run_at: rand(((24*60) - (DateTime.now.hour * 60 + DateTime.now.minute))).minutes.from_now)
STDOUT << "#{Time.zone.now.utc} Domain.destroy_delete_candidates: job added by deleteCandidate status ##{x.id} (#{x.name})\n" unless Rails.env.test? STDOUT << "#{Time.zone.now.utc} Domain.destroy_delete_candidates: job added by deleteCandidate status ##{domain.id} (#{domain.name})\n" unless Rails.env.test?
c += 1 c += 1
end end
end end

View file

@ -826,6 +826,9 @@ RSpec.describe Domain do
end end
RSpec.describe Domain, db: false do RSpec.describe Domain, db: false do
it { is_expected.to alias_attribute(:outzone_time, :outzone_at) }
it { is_expected.to alias_attribute(:delete_time, :delete_at) }
describe '::expire_warning_period', db: true do describe '::expire_warning_period', db: true do
before :example do before :example do
Setting.expire_warning_period = 1 Setting.expire_warning_period = 1
@ -882,4 +885,36 @@ RSpec.describe Domain, db: false do
expect(domain.delete_at).to eq(Time.zone.parse('08.07.2010 10:30')) expect(domain.delete_at).to eq(Time.zone.parse('08.07.2010 10:30'))
end end
end end
describe '::outzone_candidates', db: true do
before :example do
travel_to Time.zone.parse('05.07.2010 00:00')
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate.create(:domain, id: 1, outzone_time: Time.zone.parse('04.07.2010 23:59'))
Fabricate.create(:domain, id: 2, outzone_time: Time.zone.parse('05.07.2010 00:00'))
Fabricate.create(:domain, id: 3, outzone_time: Time.zone.parse('05.07.2010 00:01'))
end
it 'returns domains with outzone time in the past' do
expect(described_class.outzone_candidates.ids).to eq([1])
end
end
describe '::delete_candidates', db: true do
before :example do
travel_to Time.zone.parse('05.07.2010 00:00')
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate.create(:domain, id: 1, delete_time: Time.zone.parse('04.07.2010 23:59'))
Fabricate.create(:domain, id: 2, delete_time: Time.zone.parse('05.07.2010 00:00'))
Fabricate.create(:domain, id: 3, delete_time: Time.zone.parse('05.07.2010 00:01'))
end
it 'returns domains with delete time in the past' do
expect(described_class.delete_candidates.ids).to eq([1])
end
end
end end