Class: AdSpendSelfHealWorker

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

Overview

Re-runs an ad-spend day the nightly never finished.

Amazon report requests and polls now remain inside the owning Sidekiq batch:
a pending or throttled report schedules another short transition before the
current job finishes. A day can still remain unmarked after a terminal API
failure, a dropped cron, or a batch that never ran; this outside sweep finds
those cases without creating duplicate reports for a healthy in-flight day.

Sweeping from the outside rather than making the finalizer retry itself is
the point. The same sweep also covers a day whose batch never ran at all — a
dropped cron, a queue nobody was processing — which a self-rescheduling
finalizer cannot see, because it only runs when a batch completed.

One day per run, deliberately: each day requests one report per eligible
US/CA seller profile and ad product (six with the currently observed profile
set). Firing a week of them at once is the shape of the original quota
incident. The hourly schedule bounds self-healing to one day per pass; the
provider cooldown remains the hard API backpressure control as profiles vary.
Yesterday stays excluded until 06:00 so this never preempts the normal 03:00
sync; older incomplete dates remain eligible overnight.

Constant Summary collapse

LOOKBACK_DAYS =

How far back to look for an unfinished day. Well inside
Marketing::AdSpend::RunState::COMPLETED_TTL, so a day that genuinely
completed is never mistaken for one that never ran.

7
NIGHTLY_TARGET_READY_HOUR =

The scheduled 03:00 sync owns yesterday first. Amazon report generation can
take up to two hours, so self-heal does not consider that date until 06:00.
Older holes remain eligible during the overnight hourly passes.

6
BACKLOG_WARNING_THRESHOLD =

A backlog is worth a warning and a gauge, but not an exception: this worker
is the recovery mechanism and the day remains observable until it completes.

3

Instance Method Summary collapse

Instance Method Details

#performString?

Returns the date dispatched, nil when nothing needed healing.

Returns:

  • (String, nil)

    the date dispatched, nil when nothing needed healing



43
44
45
46
47
48
49
50
51
# File 'app/workers/ad_spend_self_heal_worker.rb', line 43

def perform
  cooldown = Marketing::AdSpend::RunState.cooldown_active?
  pending = pending_days
  report_backlog(pending, cooldown:)
  # A provider is rate-limiting us; report the pending work, then stand down.
  return if pending.empty? || cooldown

  heal(pending.first, pending.size)
end