Delete discarded domains at the random moment within next 24h

#790
This commit is contained in:
Artur Beljajev 2018-04-20 16:55:36 +03:00
parent 60a66942d1
commit 3e94c10da1
8 changed files with 105 additions and 31 deletions

View file

@ -4,11 +4,21 @@ module Concerns::Domain::Deletable
private
def delete_later
run_at = rand(((24 * 60) - (DateTime.now.hour * 60 + DateTime.now.minute))).minutes.from_now
DomainDeleteJob.enqueue(id, run_at: run_at)
deletion_time = Time.zone.at(rand(deletion_time_span))
DomainDeleteJob.enqueue(id, run_at: deletion_time)
logger.info "Domain #{name} is scheduled to be deleted around #{deletion_time}"
end
def do_not_delete_later
QueJob.find_by!("args->>0 = '#{id}'", job_class: DomainDeleteJob.name).delete
QueJob.find_by!("args->>0 = '#{id}'", job_class: DomainDeleteJob.name).destroy!
end
def deletion_time_span
# 5 minutes to ensure we don't create a background job with past `run_at`
((Time.zone.now + 5.minutes).to_i)..(deletion_deadline.to_i)
end
def deletion_deadline
delete_at + 24.hours
end
end

View file

@ -13,17 +13,21 @@ module Concerns::Domain::Discardable
end
def discard
raise 'Domain is already discarded' if discarded?
statuses << DomainStatus::DELETE_CANDIDATE
# We don't validate deliberately since nobody is interested in fixing discarded domain
save(validate: false)
delete_later
logger.info "Domain #{name} (ID: #{id}) is scheduled to be deleted"
transaction do
save(validate: false)
delete_later
end
end
def keep
statuses.delete(DomainStatus::DELETE_CANDIDATE)
save(validate: false)
do_not_delete_later
transaction do
save(validate: false)
do_not_delete_later
end
end
def discarded?