Class: AdSpendAmazonRangeFinalizer

Inherits:
Object
  • Object
show all
Defined in:
app/workers/ad_spend_amazon_range_finalizer.rb

Overview

Advances the Amazon-only range chain only after its rescheduled report polls
have downloaded and recorded every report in the chunk.

Instance Method Summary collapse

Instance Method Details

#on_complete(status, options) ⇒ Object

Parameters:

  • status (Sidekiq::Batch::Status)

    batch status for the poll chunk

  • options (Hash)

    batch payload serialized by the dispatcher

Options Hash (options):

  • started_at (String)

    ISO8601 time the chunk started

  • start_date (String)

    first date of the overall range

  • chunk_end (String)

    last date covered by this chunk

  • end_date (String)

    last date of the overall range



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/workers/ad_spend_amazon_range_finalizer.rb', line 12

def on_complete(status, options)
  failures = status.failures.to_i
  duration = Time.current - Time.iso8601(options.fetch('started_at'))
  start_date = Date.parse(options.fetch('start_date'))
  chunk_end = Date.parse(options.fetch('chunk_end'))
  end_date = Date.parse(options.fetch('end_date'))

  Rails.event.notify(
    'ad_spend.amazon_range',
    action: 'completed',
    batch_id: status.bid,
    failures:,
    start_date: start_date.to_s,
    end_date: chunk_end.to_s,
    duration_seconds: duration.round(1)
  )
  Appsignal.add_distribution_value('ad_spend.amazon_range.duration_seconds', duration) if defined?(Appsignal) && Appsignal.active?

  if failures.positive?
    ErrorReporting.error(
      "[AdSpendAmazonRangeFinalizer] #{start_date}..#{chunk_end}: #{failures} report poll job(s) failed. " \
      "Restart remaining range with AdSpendAmazonRangeBackfillWorker.perform_async('#{start_date}', '#{end_date}')"
    )
    return
  end

  return if chunk_end >= end_date

  AdSpendAmazonRangeBackfillWorker.perform_async((chunk_end + 1).to_s, end_date.to_s)
end