Add better error handling for single domain renewal tasks

This commit is contained in:
Alex Sherman 2020-12-15 13:54:47 +05:00
parent a1b3398e0e
commit 95a789ab60
2 changed files with 28 additions and 10 deletions

View file

@ -10,12 +10,11 @@ module Domains
def execute def execute
in_transaction_with_retries do in_transaction_with_retries do
success = domain.renew(domain.valid_to, period, unit) success = domain.renew(domain.valid_to, period, unit)
if success if success
check_balance check_balance
reduce_balance reduce_balance
else else
errors.add(:domain, I18n.t('domain_renew_error_for_domain', domain: domain.name)) add_error
end end
end end
end end
@ -36,11 +35,11 @@ module Domains
price: domain_pricelist) price: domain_pricelist)
end end
def in_transaction_with_retries(&block) def in_transaction_with_retries
if Rails.env.test? if Rails.env.test?
yield yield
else else
transaction_wrapper(block) transaction_wrapper { yield }
end end
rescue ActiveRecord::StatementInvalid rescue ActiveRecord::StatementInvalid
sleep rand / 100 sleep rand / 100
@ -52,6 +51,12 @@ module Domains
yield if block_given? yield if block_given?
end end
end end
private
def add_error
errors.add(:domain, I18n.t('domain_renew_error_for_domain', domain: domain.name))
end
end end
end end
end end

View file

@ -8,20 +8,22 @@ module Domains
object :registrar object :registrar
def execute def execute
if mass_check_balance.valid? && mass_check_balance.result if renewable?
domains.each do |domain| domains.each do |domain|
Domains::BulkRenew::SingleDomainRenew.run(domain: domain, task = run_task(domain)
period: period, manage_errors(task)
unit: unit,
registrar: registrar)
end end
else else
errors.merge!(mass_check_balance.errors) manage_errors(mass_check_balance)
end end
end end
private private
def renewable?
mass_check_balance.valid? && mass_check_balance.result
end
def period def period
period_element.to_i.zero? ? 1 : period_element.to_i period_element.to_i.zero? ? 1 : period_element.to_i
end end
@ -37,6 +39,17 @@ module Domains
unit: unit, unit: unit,
balance: registrar.balance) balance: registrar.balance)
end end
def manage_errors(task)
errors.merge!(task.errors) unless task.valid?
end
def run_task(domain)
Domains::BulkRenew::SingleDomainRenew.run(domain: domain,
period: period,
unit: unit,
registrar: registrar)
end
end end
end end
end end