Skip to content

Commit

Permalink
Added syncing loop with delay for contact requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsoganov committed Mar 19, 2024
1 parent a039b44 commit f6a8aab
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions app/controllers/contact_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class ContactRequestsController < ApplicationController
before_action :set_contact_request, only: %i[edit update show]
before_action :check_for_replay, only: %i[edit update show]

MAX_SYNC_WAIT_TIME = 5 # seconds

rescue_from ActionController::UnknownFormat do
logger.warn("The unlucky customer was using format of: #{request.format}")
raise
Expand All @@ -17,20 +19,15 @@ def new
def create
@contact_request = ContactRequest.new(contact_request_params)
result = @contact_request.save_to_registry
@contact_request = result ? ContactRequest.find_by(id: result['id']) : nil
fetch_contact_request(result)

if @contact_request
update_request_secret
@contact_request.send_confirmation_email
@contact_request.reload
logger.warn("Confirmation request email registered to #{@contact_request.email}" \
" (IP: #{request.ip})")
render :confirmation_completed
process_contact_request
else
redirect_to(:root, alert: t('contact_requests.registry_link_error'))
end
rescue Net::SMTPServerBusy => e
logger.warn("Failed confirmation request email to #{@contact_request.email}. #{e.message}")
redirect_to(:root, alert: t('contact_requests.smtp_error'))
handle_smtp_error(e)
end

def redirect_to_main
Expand Down Expand Up @@ -87,4 +84,28 @@ def check_for_replay
def contact_request_params
params.require(:contact_request).permit(:email, :whois_record_id, :name)
end

def fetch_contact_request(result)
start_time = Time.now
while Time.now - start_time < MAX_SYNC_WAIT_TIME
@contact_request = result ? ContactRequest.find_by(id: result['id']) : nil
break if @contact_request

sleep(0.5) # Sleep for 0.5 seconds before retrying
end
end

def process_contact_request
update_request_secret
@contact_request.send_confirmation_email
@contact_request.reload
logger.warn("Confirmation request email registered to #{@contact_request.email}" \
" (IP: #{request.ip})")
render :confirmation_completed
end

def handle_smtp_error(exception)
logger.warn("Failed confirmation request email to #{@contact_request.email}. #{exception.message}")
redirect_to(:root, alert: t('contact_requests.smtp_error'))
end
end

0 comments on commit f6a8aab

Please sign in to comment.