Move #process_expired to interactor

This commit is contained in:
Alex Sherman 2020-12-01 16:44:31 +05:00
parent e8fa79304f
commit 849010b118
5 changed files with 58 additions and 27 deletions

View file

@ -0,0 +1,10 @@
module Domains
module ExpirePeriod
class Base < ActiveInteraction::Base
def to_stdout(message)
time = Time.zone.now.utc
STDOUT << "#{time} - #{message}\n" unless Rails.env.test?
end
end
end
end

View file

@ -0,0 +1,28 @@
module Domains
module ExpirePeriod
class ProcessExpired < Base
object :domain,
class: Domain,
description: 'Domain to set expiration'
def execute
set_graceful_expired
to_stdout("start_expire_period: ##{domain.id} (#{domain.name}) #{domain.changes}")
saved = domain.save(validate: false)
DomainExpireEmailJob.enqueue(domain.id, run_at: send_time) if saved
end
def set_graceful_expired
domain.outzone_at = domain.expire_time + Domain.expire_warning_period
domain.delete_date = domain.outzone_at + Domain.redemption_grace_period
domain.statuses |= [DomainStatus::EXPIRED]
end
def send_time
domain.valid_to + Setting.expiration_reminder_mail.to_i.days
end
end
end
end

View file

@ -0,0 +1,19 @@
module Domains
module ExpirePeriod
class Start < Base
def execute
::PaperTrail.request.whodunnit = "cron - #{self.class.name}"
count = 0
Domain.expired.each do |domain|
next unless domain.expirable?
count += 1
Domains::ExpirePeriod::ProcessExpired.run(domain: domain)
end
to_stdout("Successfully expired #{count}")
end
end
end
end

View file

@ -482,12 +482,6 @@ class Domain < ApplicationRecord
Registrant.find_by(id: pending_json['new_registrant_id'])
end
def set_graceful_expired
self.outzone_at = expire_time + self.class.expire_warning_period
self.delete_date = outzone_at + self.class.redemption_grace_period
self.statuses |= [DomainStatus::EXPIRED]
end
def pending_update?
statuses.include?(DomainStatus::PENDING_UPDATE)
end

View file

@ -4,27 +4,7 @@ class DomainCron
end
def self.start_expire_period
::PaperTrail.request.whodunnit = "cron - #{__method__}"
domains = Domain.expired
marked = 0
real = 0
domains.each do |domain|
next unless domain.expirable?
real += 1
domain.set_graceful_expired
STDOUT << "#{Time.zone.now.utc} DomainCron.start_expire_period: ##{domain.id} (#{domain.name}) #{domain.changes}\n" unless Rails.env.test?
send_time = domain.valid_to + Setting.expiration_reminder_mail.to_i.days
saved = domain.save(validate: false)
if saved
DomainExpireEmailJob.enqueue(domain.id, run_at: send_time)
marked += 1
end
end
STDOUT << "#{Time.zone.now.utc} - Successfully expired #{marked} of #{real} domains\n" unless Rails.env.test?
Domains::ExpirePeriod::Start.run!
end
def self.start_redemption_grace_period