Maintenance::ImportAgentPortCompaniesTask

Source code
# frozen_string_literal: true

class Maintenance::ImportAgentPortCompaniesTask < MaintenanceTasks::Task
  include ArrayHelper

  csv_collection(in_batches: 50)
  attribute :emails, :string
  validates :emails, presence: true, fcm_email_format: true

  CSV_COLUMNS = %w[company_guid tree_node_name brand_name crm_sales_id mbo_id mbo_name country_code status
                   errors].freeze

  after_start    :prepare_csv_path
  after_complete :send_report
  after_error    :send_report

  def process(batch_of_rows)
    File.open(csv_path, 'a') do |file|
      batch_of_rows.each do |row|
        result = AgentPort::RowProcessor.new(row.to_h).call
        file.puts build_csv_line(result)
      end
    end
  end

  def csv_path
    @csv_path ||= Rails.root.join('tmp/import_agent_port_latest.csv').to_s
  end

  private

  def prepare_csv_path
    @csv_path = Rails.root.join('tmp', "import_agent_port_#{Time.now.to_i}.csv").to_s
    File.write(@csv_path, CSV_COLUMNS.to_csv)
  end

  def send_report
    return unless csv_path && File.exist?(csv_path)

    CsvReportMailer.send_report(
      recipients: emails_array(emails),
      file_path: csv_path,
      report_sender: 'Import AgentPort Companies'
    ).deliver_now
  ensure
    File.delete(csv_path) if csv_path && File.exist?(csv_path)
  end

  def build_csv_line(result)
    CSV_COLUMNS.map { |col| %w[status errors].include?(col) ? result[col.to_sym] : result[:row][col] }.to_csv
  end
end

Previous Runs

Succeeded

Processed 52 out of 52 items (100%).

Ran for less than a minute, finished .


Arguments:
emails
[FILTERED]

Metadata:
user_id
Succeeded

Processed 51 out of 51 items (100%).

Ran for less than a minute, finished .


Arguments:
emails
[FILTERED]

Metadata:
user_id
Succeeded

Processed 52 out of 52 items (100%).

Ran for less than a minute, finished .


Arguments:
emails
[FILTERED]

Metadata:
user_id
Succeeded

Processed 1 out of 1 item (100%).

Ran for less than 5 seconds, finished .


Arguments:
emails
[FILTERED]

Metadata:
user_id
Succeeded

Processed 52 out of 52 items (100%).

Ran for 1 minute, finished .


Arguments:
emails
[FILTERED]

Metadata:
user_id
Succeeded

Processed 52 out of 52 items (100%).

Ran for less than 20 seconds, finished .


Arguments:
emails
[FILTERED]

Metadata:
user_id
Succeeded

Processed 52 out of 52 items (100%).

Ran for less than a minute, finished .


Arguments:
emails
[FILTERED]

Metadata:
user_id
Succeeded

Processed 52 out of 52 items (100%).

Ran for less than a minute, finished .


Arguments:
emails
[FILTERED]

Metadata:
user_id
Succeeded

Processed 16 out of 16 items (100%).

Ran for half a minute, finished .


Metadata:
user_id
Errored

Processed 14 out of 16 items (88%).

Ran for 8 minutes until an error happened .

Api::Errors::ServerError

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta content='text/html; charset=utf-8' http-equiv='content-type' /> <style type='text/css'> body { font-family: Arial; margin-left: 40px; } img { border: 0 none; } #content { margin-left: auto; margin-right: auto } #message h1 { font-size: 24px; font-weight: normal; color: #000000; margin: 34px 0px 0px 0px } #message h2 { font-size: 20px; font-weight: normal; color: #000000; margin: 34px 0px 0px 0px } #message p { font-size: 16px; color: #000000; margin: 8px 0px 0px 0px } #message hr { margin: 15px 0px } #errorref { font-size: 11px; color: #737373; margin-top: 41px } </style> <title>Service unavailable</title> </head> <body> <div id='content'> <div id='message'> <h1>504</h1> <h2><span>The service behind this page isn't responding to Azure Front Door.</span> </h2> <hr /> <p>Gateway Timeout</p> <p>Azure Front Door cannot connect to the origin server at this time. <br> The origin might be overloaded, misconfigured or under maintenance now. Please contact the site owner for assistance.</p> <br /> <a href="https://learn.microsoft.com/en-us/azure/frontdoor/troubleshoot-issues#503-or-504-response-from-azure-front-door-after-a-few-seconds" target="blank">Azure Documentation</a> <br /> </div> <div id='errorref'> <span>Error Info:</span><span>OriginTimeout</span><br /> <span>x-azure-ref ID:</span><span>20250814T152503Z-15679c49f58zwvmchC1BRUvx4s0000000ut000000000h3nv </span> </div> </div> </body> </html>

lib/api/base_client.rb:47:in 'Api::BaseClient::Agent#handle_errors'
lib/api/base_client.rb:30:in 'block (2 levels) in <class:Agent>'
lib/api/agent_port.rb:6:in 'Api::AgentPort::Wrapper#company'
app/validators/company_guid_validator.rb:9:in 'CompanyGuidValidator#validate_each'
app/tasks/maintenance/import_agent_port_companies_task.rb:19:in 'Maintenance::ImportAgentPortCompaniesTask#process_one'
app/tasks/maintenance/import_agent_port_companies_task.rb:8:in 'block in Maintenance::ImportAgentPortCompaniesTask#process'
app/tasks/maintenance/import_agent_port_companies_task.rb:7:in 'Array#each'
app/tasks/maintenance/import_agent_port_companies_task.rb:7:in 'Maintenance::ImportAgentPortCompaniesTask#process'

Metadata:
user_id