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

View file

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