Class: AdSpendAmazonRangeBackfillWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/ad_spend_amazon_range_backfill_worker.rb

Overview

Requests one Amazon report set for a bounded date range, then hands status
waiting to AdSpendAmazonReportPollWorker. A 30-day chunk is six report
requests total (seller US/CA x three ad products), never six per day.

Constant Summary collapse

MAX_RANGE_DAYS =

Max request age.

30
MAX_REQUEST_AGE =

Constant.

2.hours

Instance Method Summary collapse

Instance Method Details

#perform(start_date_string, end_date_string, request_started_at = nil, requested_reports = [], request_attempt = 1) ⇒ Object

Runs the job.

Parameters:

  • start_date_string (Object)

    the start date string

  • end_date_string (Object)

    the end date string

  • request_started_at (Time, nil) (defaults to: nil)

    the request started at

  • requested_reports (Array) (defaults to: [])

    the requested reports

  • request_attempt (Integer) (defaults to: 1)

    the request attempt

Returns:

  • (Object)

    the result



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/workers/ad_spend_amazon_range_backfill_worker.rb', line 24

def perform(start_date_string, end_date_string, request_started_at = nil, requested_reports = [],
            request_attempt = 1)
  start_date = Date.parse(start_date_string)
  end_date = Date.parse(end_date_string)
  return Rails.logger.info('[AdSpendAmazonRangeBackfill] nothing to do') if start_date > end_date

  request_started_at ||= Time.current.iso8601
  ensure_request_not_expired!(request_started_at)
  chunk_end = [start_date + (MAX_RANGE_DAYS - 1), end_date].min
  reports = Marketing::AdSpend::AmazonAdapter.request_reports(
    start_date, chunk_end, reports: requested_reports
  )
  dispatch_poll_batch(reports, start_date, chunk_end, end_date)
rescue Marketing::AdSpend::AmazonAdapter::ReportsThrottledError => e
  reschedule_request(
    start_date_string, end_date_string, request_started_at, e.reports, request_attempt, e
  )
rescue AmazonAds::ApiClient::ThrottledError => e
  reschedule_request(
    start_date_string, end_date_string, request_started_at, requested_reports, request_attempt, e
  )
rescue StandardError => e
  ErrorReporting.error(
    "[AdSpendAmazonRangeBackfill] #{start_date_string}..#{end_date_string} aborted: #{e.message}. " \
    "Range NOT complete — restart with " \
    "AdSpendAmazonRangeBackfillWorker.perform_async('#{start_date_string}', '#{end_date_string}')"
  )
  raise
end