refactored task

This commit is contained in:
olegphenomenon 2021-09-22 12:56:14 +03:00
parent 651f542fcb
commit 41ee61d57a
7 changed files with 67 additions and 74 deletions

View file

@ -1,20 +1,21 @@
class ReplaceUpdToObjUpdProhibitedJob < ApplicationJob class ReplaceUpdToObjUpdProhibitedJob < ApplicationJob
def perform(action:, rollback: false) def perform(rollback: false)
logger.info 'Ran ReplaceUpdToObjUpdProhibitedJob!' logger.info 'Ran ReplaceUpdToObjUpdProhibitedJob!'
start_adding_new_status_for_locked_domains(action: action, rollback: rollback) start_replace_status_for_locked_domains(rollback: rollback)
end end
private
def start_adding_new_status_for_locked_domains(action:, rollback:) def start_replace_status_for_locked_domains(rollback:)
count = 0 count = 0
Domain.all.find_in_batches do |domain_batches| Domain.all.find_in_batches do |domain_batches|
count += domain_batches.count count += domain_batches.count
logger.info "Proccesing #{count} domains of #{Domain.count}" logger.info "Proccesing #{count} domains of #{Domain.count}"
domain_batches.each do |domain| domain_batches.each do |domain|
make_actions_with_statuses(domain: domain, action: action, rollback: rollback) if domain.locked_by_registrant?
process_domain_status_replacment(domain: domain, rollback: rollback)
end
end end
logger.info "Successfully proccesed #{count} domains of #{Domain.count}" logger.info "Successfully proccesed #{count} domains of #{Domain.count}"
@ -23,31 +24,13 @@ class ReplaceUpdToObjUpdProhibitedJob < ApplicationJob
private private
def make_actions_with_statuses(domain:, action:, rollback:) def process_domain_status_replacment(domain:, rollback:)
if domain.locked_by_registrant? && rollback domain.statuses = domain.statuses - ["serverUpdateProhibited"] + ["serverObjUpdateProhibited"] unless rollback
rollback_actions(action: action, domain: domain) domain.statuses = domain.statuses - ["serverObjUpdateProhibited"] + ["serverUpdateProhibited"] if rollback
elsif domain.locked_by_registrant? && !rollback if domain.save
add_actions(action: action, domain: domain) logger.info "#{domain.name} has next statuses #{domain.statuses}"
end else
end logger.warn "#{domain.name} - something goes wrong!"
def rollback_actions(action:, domain:)
if action == :add && !domain.statuses.include?('serverUpdateProhibited')
domain.statuses = domain.statuses + ['serverUpdateProhibited']
domain.save!
elsif action == :remove && domain.statuses.include?('serverObjUpdateProhibited')
domain.statuses = domain.statuses - ['serverObjUpdateProhibited']
domain.save!
end
end
def add_actions(action:, domain:)
if action == :add && !domain.statuses.include?('serverObjUpdateProhibited')
domain.statuses = domain.statuses + ['serverObjUpdateProhibited']
domain.save!
elsif action == :remove && domain.statuses.include?('serverUpdateProhibited')
domain.statuses = domain.statuses - ['serverUpdateProhibited']
domain.save!
end end
end end

View file

@ -1,7 +1,7 @@
module Domain::RegistryLockable module Domain::RegistryLockable
extend ActiveSupport::Concern extend ActiveSupport::Concern
LOCK_STATUSES = if Feature.obj_and_extensions_statuses_enabled? LOCK_STATUSES = if Feature.enable_lock_domain_with_new_statuses?
[DomainStatus::SERVER_OBJ_UPDATE_PROHIBITED, [DomainStatus::SERVER_OBJ_UPDATE_PROHIBITED,
DomainStatus::SERVER_DELETE_PROHIBITED, DomainStatus::SERVER_DELETE_PROHIBITED,
DomainStatus::SERVER_TRANSFER_PROHIBITED].freeze DomainStatus::SERVER_TRANSFER_PROHIBITED].freeze

View file

@ -4,4 +4,10 @@ class Feature
ENV['obj_and_extensions_prohibited'] || false ENV['obj_and_extensions_prohibited'] || false
end end
def self.enable_lock_domain_with_new_statuses?
return false if ENV['enable_lock_domain_with_new_statuses'] == 'false'
ENV['enable_lock_domain_with_new_statuses'] || false
end
end end

View file

@ -60,6 +60,7 @@ contact_org_enabled: 'false'
# legal_document_types: "pdf,asice,sce,asics,scs,adoc,edoc,bdoc,ddoc,zip,rar,gz,tar,7z,odt,doc,docx" # legal_document_types: "pdf,asice,sce,asics,scs,adoc,edoc,bdoc,ddoc,zip,rar,gz,tar,7z,odt,doc,docx"
# obj_and_extensions_prohibited: 'true' # obj_and_extensions_prohibited: 'true'
# enable_lock_domain_with_new_statuses: 'true'
# #
# REGISTRAR configuration (DEPP) # REGISTRAR configuration (DEPP)

View file

@ -1,42 +1,22 @@
require 'benchmark' require 'benchmark'
# INSTRUCTIONS:
# The task works as follows, it finds a domain that has a domain lock mark and replaces the status serverUpdateProhibited with serverObjUpdateProhibited
# For run this task it need to type `rake locked_domains:replace_new_status`
# Whole results will saved into log/replace_upd_to_obj_upd_prohibited.log
# It need to make sure before launching that these statuses mean that the domain has a domain lock, otherwise this scanner will not find the required domains.
# Therefore, it is better that the value `enable_lock_domain_with_new_statuses` in the `application.yml` file is commented out or has the status false before starting. After the task has been completed, set the value `enable_lock_domain_with_new_statuses` to true, and then the domain with the following statuses `serverDeleteProhibited, serverTransferProhibited, serverObjUpdateProhibite` will be considered blocked now.
# If for some reason it need to roll back the result, then this value `enable_lock_domain_with_new_statuses` must be true, and run the command `rake locked_domains:rollback_replacement_new_status`
namespace :locked_domains do namespace :locked_domains do
desc 'Replace serverUpdateProhibited to serverObjUpdateProhibited for locked domains'
# Add new status instruction! task replace_new_status: :environment do
# First run `rake locked_domains:add_new_status` ReplaceUpdToObjUpdProhibitedJob.perform_later
# and then after finish first task run `rake locked_domains:remove_old_status`
desc 'Add serverObjUpdateProhibited for locked domains'
task add_new_status: :environment do
time = Benchmark.realtime do
ReplaceUpdToObjUpdProhibitedJob.perform_later(action: :add)
end
puts "Time is #{time.round(2)} for add serverObjUpdateProhibited status for locked domains"
end end
desc 'Remove serverUpdateProhibited from locked domains' desc 'Replace serverObjUpdateProhibited to serverUpdateProhibited for locked domains'
task remove_old_status: :environment do task rollback_replacement_new_status: :environment do
time = Benchmark.realtime do ReplaceUpdToObjUpdProhibitedJob.perform_later(rollback: true)
ReplaceUpdToObjUpdProhibitedJob.perform_later(action: :remove)
end
puts "Time is #{time.round(2)} for remove serverUpdateProhibited for locked domains"
end
# Rollback instruction!
# First run `rake locked_domains:rollback_remove_old_status`
# and then after finish first task run `rake locked_domains:rollback_add_new_status`
desc 'Rollback remove old serverUpdateProhibited for locked domains'
task rollback_remove_old_status: :environment do
time = Benchmark.realtime do
ReplaceUpdToObjUpdProhibitedJob.perform_later(action: :add, rollback: true)
end
puts "Time is #{time.round(2)} for add serverObjUpdateProhibited status for locked domains"
end
desc 'Rollback add new serverObjUpdateProhibited for locked domains'
task rollback_add_new_status: :environment do
time = Benchmark.realtime do
ReplaceUpdToObjUpdProhibitedJob.perform_later(action: :remove, rollback: true)
end
puts "Time is #{time.round(2)} for add serverObjUpdateProhibited status for locked domains"
end end
end end

View file

@ -13,7 +13,7 @@ class ReplaceUpdToObjUpdProhibitedJobTest < ActiveSupport::TestCase
assert @domain.locked_by_registrant? assert @domain.locked_by_registrant?
perform_enqueued_jobs do perform_enqueued_jobs do
ReplaceUpdToObjUpdProhibitedJob.perform_later(action: :add) ReplaceUpdToObjUpdProhibitedJob.perform_later
end end
@domain.reload @domain.reload
@ -25,14 +25,14 @@ class ReplaceUpdToObjUpdProhibitedJobTest < ActiveSupport::TestCase
assert @domain.locked_by_registrant? assert @domain.locked_by_registrant?
assert @domain.statuses.include? "serverUpdateProhibited" assert @domain.statuses.include? "serverUpdateProhibited"
@domain.statuses += ["serverObjUpdateProhibited"] # @domain.statuses += ["serverObjUpdateProhibited"]
@domain.save # @domain.save
@domain.reload # @domain.reload
assert @domain.statuses.include? "serverObjUpdateProhibited" # assert @domain.statuses.include? "serverObjUpdateProhibited"
perform_enqueued_jobs do perform_enqueued_jobs do
ReplaceUpdToObjUpdProhibitedJob.perform_later(action: :remove) ReplaceUpdToObjUpdProhibitedJob.perform_later
end end
@domain.reload @domain.reload
@ -49,7 +49,7 @@ class ReplaceUpdToObjUpdProhibitedJobTest < ActiveSupport::TestCase
assert_not @domain.locked_by_registrant? assert_not @domain.locked_by_registrant?
perform_enqueued_jobs do perform_enqueued_jobs do
ReplaceUpdToObjUpdProhibitedJob.perform_later(action: :add) ReplaceUpdToObjUpdProhibitedJob.perform_later
end end
assert_not @domain.statuses.include? "serverObjUpdateProhibited" assert_not @domain.statuses.include? "serverObjUpdateProhibited"
@ -64,7 +64,7 @@ class ReplaceUpdToObjUpdProhibitedJobTest < ActiveSupport::TestCase
assert_not @domain.locked_by_registrant? assert_not @domain.locked_by_registrant?
perform_enqueued_jobs do perform_enqueued_jobs do
ReplaceUpdToObjUpdProhibitedJob.perform_later(action: :remove) ReplaceUpdToObjUpdProhibitedJob.perform_later
end end
assert @domain.statuses.include? "serverUpdateProhibited" assert @domain.statuses.include? "serverUpdateProhibited"

View file

@ -1,6 +1,11 @@
require 'test_helper' require 'test_helper'
class FeatureTest < ActiveSupport::TestCase class FeatureTest < ActiveSupport::TestCase
setup do
@domain = domains(:shop)
@domain.apply_registry_lock
end
def test_if_obj_and_extensions_prohibited_enabled def test_if_obj_and_extensions_prohibited_enabled
ENV['obj_and_extensions_prohibited'] = 'true' ENV['obj_and_extensions_prohibited'] = 'true'
@ -27,4 +32,22 @@ class FeatureTest < ActiveSupport::TestCase
statuses = DomainStatus.admin_statuses statuses = DomainStatus.admin_statuses
assert_not statuses.include? DomainStatus::SERVER_OBJ_UPDATE_PROHIBITED assert_not statuses.include? DomainStatus::SERVER_OBJ_UPDATE_PROHIBITED
end end
def test_if_enable_lock_domain_with_new_statuses_is_nil
ENV['enable_lock_domain_with_new_statuses'] = nil
assert_not Feature.enable_lock_domain_with_new_statuses?
assert_equal @domain.statuses, ["serverUpdateProhibited", "serverDeleteProhibited", "serverTransferProhibited"]
assert @domain.locked_by_registrant?
end
def test_if_enable_lock_domain_with_new_statuses_is_false
ENV['enable_lock_domain_with_new_statuses'] = 'false'
assert_not Feature.enable_lock_domain_with_new_statuses?
assert_equal @domain.statuses, ["serverUpdateProhibited", "serverDeleteProhibited", "serverTransferProhibited"]
assert @domain.locked_by_registrant?
end
end end