Move jobs from Que to ActiveJob

This commit is contained in:
Alex Sherman 2020-10-19 16:01:20 +05:00
parent 1d3be40e14
commit 313731232e
53 changed files with 390 additions and 157 deletions

View file

@ -9,7 +9,7 @@ module Domains
clean_pendings
to_stdout("DomainCron.clean_expired_pendings: ##{domain.id} (#{domain.name})")
UpdateWhoisRecordJob.enqueue domain.name, 'domain'
UpdateWhoisRecordJob.perform_later domain.name, 'domain'
end
private

View file

@ -17,7 +17,7 @@ module Domains
update_domain
clean_pendings!
WhoisRecord.find_by(domain_id: domain.id).save # need to reload model
WhoisRecord.find_by(domain_id: domain.id)&.save # need to reload model
end
def update_domain

View file

@ -1,2 +1,4 @@
class ApplicationJob < ActiveJob::Base
discard_on NoMethodError
queue_as :default
end

View file

@ -1,5 +1,5 @@
class DirectoInvoiceForwardJob < Que::Job
def run(monthly: false, dry: false)
class DirectoInvoiceForwardJob < ApplicationJob
def perform(monthly: false, dry: false)
@dry = dry
(@month = Time.zone.now - 1.month) if monthly

View file

@ -1,5 +1,5 @@
class DisputeStatusUpdateJob < Que::Job
def run(logger: Logger.new(STDOUT))
class DisputeStatusUpdateJob < ApplicationJob
def perform(logger: Logger.new(STDOUT))
@logger = logger
@backlog = { 'activated': 0, 'closed': 0, 'activate_fail': [], 'close_fail': [] }

View file

@ -1,6 +1,4 @@
class DomainDeleteConfirmJob < ApplicationJob
queue_as :default
def perform(domain_id, action, initiator = nil)
domain = Epp::Domain.find(domain_id)
@ -8,4 +6,34 @@ class DomainDeleteConfirmJob < ApplicationJob
action: action,
initiator: initiator)
end
private
def action_confirmed(domain)
domain.notify_registrar(:poll_pending_delete_confirmed_by_registrant)
domain.apply_pending_delete!
raise_errors!(domain)
end
def action_rejected(domain)
domain.statuses.delete(DomainStatus::PENDING_DELETE_CONFIRMATION)
domain.notify_registrar(:poll_pending_delete_rejected_by_registrant)
domain.cancel_pending_delete
domain.save(validate: false)
raise_errors!(domain)
notify_on_domain(domain)
end
def notify_on_domain(domain)
if domain.registrant_verification_token.blank?
Rails.logger.warn 'EMAIL NOT DELIVERED: registrant_verification_token is missing for '\
"#{domain.name}"
elsif domain.registrant_verification_asked_at.blank?
Rails.logger.warn 'EMAIL NOT DELIVERED: registrant_verification_asked_at is missing for '\
"#{domain.name}"
else
DomainDeleteMailer.rejected(domain).deliver_now
end
end
end

View file

@ -1,6 +1,5 @@
class DomainDeleteJob < Que::Job
def run(domain_id)
class DomainDeleteJob < ApplicationJob
def perform(domain_id)
domain = Domain.find(domain_id)
Domains::Delete::DoDelete.run(domain: domain)

View file

@ -1,5 +1,5 @@
class DomainExpireEmailJob < Que::Job
def run(domain_id, email)
def perform(domain_id, email)
domain = Domain.find(domain_id)
return if domain.registered?

View file

@ -1,6 +1,4 @@
class DomainUpdateConfirmJob < ApplicationJob
queue_as :default
def perform(domain_id, action, initiator = nil)
domain = Epp::Domain.find(domain_id)
Domains::UpdateConfirm::ProcessAction.run(domain: domain,

View file

@ -1,10 +1,12 @@
class RegenerateRegistrarWhoisesJob < Que::Job
def run(registrar_id)
class RegenerateRegistrarWhoisesJob < ApplicationJob
retry_on StandardError, wait: 2.seconds, attempts: 3
def perform(registrar_id)
# no return as we want restart job if fails
registrar = Registrar.find(registrar_id)
registrar.whois_records.select(:name).find_in_batches(batch_size: 20) do |group|
UpdateWhoisRecordJob.enqueue group.map(&:name), 'domain'
UpdateWhoisRecordJob.perform_later group.map(&:name), 'domain'
end
end
end
end

View file

@ -1,11 +1,11 @@
class RegenerateSubzoneWhoisesJob < Que::Job
def run
class RegenerateSubzoneWhoisesJob < ApplicationJob
def perform
subzones = DNS::Zone.all
subzones.each do |zone|
next unless zone.subzone?
UpdateWhoisRecordJob.enqueue zone.origin, 'zone'
UpdateWhoisRecordJob.perform_later zone.origin, 'zone'
end
end
end

View file

@ -1,5 +1,5 @@
class RegistrantChangeConfirmEmailJob < Que::Job
def run(domain_id, new_registrant_id)
class RegistrantChangeConfirmEmailJob < ApplicationJob
def perform(domain_id, new_registrant_id)
domain = Domain.find(domain_id)
new_registrant = Registrant.find(new_registrant_id)

View file

@ -0,0 +1,22 @@
class RegistrantChangeExpiredEmailJob < ApplicationJob
def perform(domain_id)
domain = Domain.find(domain_id)
log(domain)
RegistrantChangeMailer.expired(domain: domain,
registrar: domain.registrar,
registrant: domain.registrant,
send_to: [domain.new_registrant_email,
domain.registrant.email]).deliver_now
end
private
def log(domain)
message = "Send RegistrantChangeMailer#expired email for domain #{domain.name} (##{domain.id}) to #{domain.new_registrant_email}"
logger.info(message)
end
def logger
Rails.logger
end
end

View file

@ -1,5 +1,5 @@
class RegistrantChangeNoticeEmailJob < Que::Job
def run(domain_id, new_registrant_id)
class RegistrantChangeNoticeEmailJob < ApplicationJob
def perform(domain_id, new_registrant_id)
domain = Domain.find(domain_id)
new_registrant = Registrant.find(new_registrant_id)
log(domain, new_registrant)

View file

@ -1,13 +1,11 @@
class SendEInvoiceJob < Que::Job
def run(invoice_id, payable = true)
invoice = run_condition(Invoice.find_by(id: invoice_id), payable: payable)
class SendEInvoiceJob < ApplicationJob
discard_on HTTPClient::TimeoutError
invoice.to_e_invoice(payable: payable).deliver
ActiveRecord::Base.transaction do
invoice.update(e_invoice_sent_at: Time.zone.now)
log_success(invoice)
destroy
end
def perform(invoice_id, payable = true)
invoice = Invoice.find_by(id: invoice_id)
return unless need_to_process_invoice?(invoice: invoice, payable: payable)
process(invoice: invoice, payable: payable)
rescue StandardError => e
log_error(invoice: invoice, error: e)
raise e
@ -15,10 +13,17 @@ class SendEInvoiceJob < Que::Job
private
def run_condition(invoice, payable: true)
destroy unless invoice
destroy if invoice.do_not_send_e_invoice? && payable
invoice
def need_to_process_invoice?(invoice:, payable:)
return false if invoice.blank?
return false if invoice.do_not_send_e_invoice? && payable
true
end
def process(invoice:, payable:)
invoice.to_e_invoice(payable: payable).deliver
invoice.update(e_invoice_sent_at: Time.zone.now)
log_success(invoice)
end
def log_success(invoice)

View file

@ -1,5 +1,5 @@
class UpdateWhoisRecordJob < Que::Job
def run(names, type)
class UpdateWhoisRecordJob < ApplicationJob
def perform(names, type)
Whois::Update.run(names: [names].flatten, type: type)
end
end

View file

@ -1,14 +1,11 @@
class VerifyEmailsJob < Que::Job
def run(verification_id)
email_address_verification = run_condition(EmailAddressVerification.find(verification_id))
class VerifyEmailsJob < ApplicationJob
discard_on StandardError
return if email_address_verification.recently_verified?
def perform(verification_id)
email_address_verification = EmailAddressVerification.find(verification_id)
return unless need_to_verify?(email_address_verification)
ActiveRecord::Base.transaction do
email_address_verification.verify
log_success(email_address_verification)
destroy
end
process(email_address_verification)
rescue StandardError => e
log_error(verification: email_address_verification, error: e)
raise e
@ -16,11 +13,16 @@ class VerifyEmailsJob < Que::Job
private
def run_condition(email_address_verification)
destroy unless email_address_verification
destroy if email_address_verification.recently_verified?
def need_to_verify?(email_address_verification)
return false if email_address_verification.blank?
return false if email_address_verification.recently_verified?
email_address_verification
true
end
def process(email_address_verification)
email_address_verification.verify
log_success(email_address_verification)
end
def logger

View file

@ -34,6 +34,6 @@ class BlockedDomain < ApplicationRecord
end
def remove_data
UpdateWhoisRecordJob.enqueue name, 'blocked'
UpdateWhoisRecordJob.perform_later name, 'blocked'
end
end

View file

@ -7,11 +7,14 @@ module Domain::Deletable
DomainStatus::FORCE_DELETE,
].freeze
def deletion_time
@deletion_time ||= Time.zone.at(rand(deletion_time_span))
end
private
def delete_later
deletion_time = Time.zone.at(rand(deletion_time_span))
DomainDeleteJob.enqueue(id, run_at: deletion_time, priority: 1)
DomainDeleteJob.set(wait_until: deletion_time).perform_later(id)
logger.info "Domain #{name} is scheduled to be deleted around #{deletion_time}"
end

View file

@ -11,7 +11,7 @@ module Invoice::Payable
end
def paid?
account_activity
account_activity.present?
end
def receipt_date

View file

@ -11,7 +11,7 @@ module Zone::WhoisQueryable
end
def update_whois_record
UpdateWhoisRecordJob.enqueue origin, 'zone'
UpdateWhoisRecordJob.perform_later origin, 'zone'
end
def generate_data

View file

@ -492,7 +492,7 @@ class Contact < ApplicationRecord
return if saved_changes.slice(*(self.class.column_names - ignored_columns)).empty?
names = related_domain_descriptions.keys
UpdateWhoisRecordJob.enqueue(names, 'domain') if names.present?
UpdateWhoisRecordJob.perform_later(names, 'domain') if names.present?
end
def children_log

View file

@ -84,7 +84,7 @@ class Dispute < ApplicationRecord
end
def remove_data
UpdateWhoisRecordJob.enqueue domain_name, 'disputed'
UpdateWhoisRecordJob.perform_later domain_name, 'disputed'
end
def fill_empty_passwords

View file

@ -425,8 +425,8 @@ class Domain < ApplicationRecord
new_registrant_email = registrant.email
new_registrant_name = registrant.name
RegistrantChangeConfirmEmailJob.enqueue(id, new_registrant_id)
RegistrantChangeNoticeEmailJob.enqueue(id, new_registrant_id)
RegistrantChangeConfirmEmailJob.perform_later(id, new_registrant_id)
RegistrantChangeNoticeEmailJob.perform_later(id, new_registrant_id)
reload
@ -670,7 +670,7 @@ class Domain < ApplicationRecord
end
def update_whois_record
UpdateWhoisRecordJob.enqueue name, 'domain'
UpdateWhoisRecordJob.perform_later name, 'domain'
end
def status_notes_array=(notes)

View file

@ -30,12 +30,12 @@ class RegistrantVerification < ApplicationRecord
def domain_registrant_delete_confirm!(initiator)
self.action_type = DOMAIN_DELETE
self.action = CONFIRMED
DomainDeleteConfirmJob.perform_later domain.id, CONFIRMED, initiator if save
DomainDeleteConfirmJob.perform_later(domain.id, CONFIRMED, initiator) if save
end
def domain_registrant_delete_reject!(initiator)
self.action_type = DOMAIN_DELETE
self.action = REJECTED
DomainDeleteConfirmJob.perform_later domain.id, REJECTED, initiator if save
DomainDeleteConfirmJob.perform_later(domain.id, REJECTED, initiator) if save
end
end

View file

@ -43,7 +43,7 @@ class Registrar < ApplicationRecord
after_commit :update_whois_records
def update_whois_records
return true unless changed? && (changes.keys & WHOIS_TRIGGERS).present?
RegenerateRegistrarWhoisesJob.enqueue id
RegenerateRegistrarWhoisesJob.perform_later id
end
self.ignored_columns = %w[legacy_id]
@ -104,7 +104,7 @@ class Registrar < ApplicationRecord
InvoiceMailer.invoice_email(invoice: invoice, recipient: billing_email).deliver_now
end
SendEInvoiceJob.enqueue(invoice.id, payable)
SendEInvoiceJob.perform_later(invoice.id, payable)
invoice
end

View file

@ -59,6 +59,6 @@ class ReservedDomain < ApplicationRecord
alias_method :update_whois_record, :generate_data
def remove_data
UpdateWhoisRecordJob.enqueue name, 'reserved'
UpdateWhoisRecordJob.perform_later name, 'reserved'
end
end