Add a rake task running job & test for job

This commit is contained in:
Alex Sherman 2021-07-01 16:00:35 +05:00
parent e110924968
commit 5f0c031410
8 changed files with 92 additions and 105 deletions

View file

@ -1,35 +1,16 @@
require 'optparse'
require 'rake_option_parser_boilerplate'
require 'syslog/logger'
namespace :verify_email do
desc 'Stars verifying email jobs for all the domain'
task all_domains: :environment do
verifications_by_domain = EmailAddressVerification.not_verified_recently.group_by(&:domain)
verifications_by_domain.each do |_domain, verifications|
ver = verifications.sample # Verify random email to not to clog the SMTP servers
VerifyEmailsJob.perform_later(ver.id)
next
end
end
# Need to be run like 'bundle exec rake verify_email:domain['gmail.com']'
# In zsh syntax will be 'bundle exec rake verify_email:domain\['gmail.com'\]'
# Default 'bundle exec rake verify_email:domain' wil use 'internet.ee' domain
desc 'Stars verifying email jobs for domain stated in argument'
task :domain, [:domain_name] => [:environment] do |_task, args|
args.with_defaults(domain_name: 'internet.ee')
verifications_by_domain = EmailAddressVerification.not_verified_recently
.by_domain(args[:domain_name])
verifications_by_domain.map { |ver| VerifyEmailsJob.perform_later(ver.id) }
end
# bundle exec rake verify_email:check_all -- -d=shop.test --check_level=mx --spam_protect=true
# bundle exec rake verify_email:check_all -- -dshop.test -cmx -strue
desc 'Starts verifying email jobs with optional check level and spam protection'
task :check_all do
task check_all: :environment do
SPAM_PROTECT_TIMEOUT = 30.seconds
options = {
domain_name: 'shop.test',
domain_name: nil,
check_level: 'regex',
spam_protect: false,
}
@ -37,9 +18,50 @@ namespace :verify_email do
options = RakeOptionParserBoilerplate.process_args(options: options,
banner: banner,
hash: opts_hash)
contacts = prepare_contacts(options)
logger.info 'No contacts to check email selected' and next if contacts.blank?
contacts.find_each do |contact|
VerifyEmailsJob.set(wait_until: spam_protect_timeout(options)).perform_later(
contact_id: contact.id,
check_level: check_level(options)
)
end
end
end
def check_level(options)
options[:check_level]
end
def spam_protect(options)
options[:spam_protect]
end
def spam_protect_timeout(options)
spam_protect(options) ? 0.seconds : SPAM_PROTECT_TIMEOUT
end
def logger
@logger ||= ActiveSupport::TaggedLogging.new(Syslog::Logger.new('registry'))
end
def prepare_contacts(options)
if options[:domain_name].present?
contacts_by_domain(options[:domain_name])
else
Contact.recently_not_validated
end
end
def contacts_by_domain(domain_name)
domain = ::Domain.find_by(name: domain_name)
return unless domain
domain.contacts.recently_not_validated
end
def opts_hash
{
domain_name: ['-d [DOMAIN_NAME]', '--domain_name [DOMAIN_NAME]', String],