Class: AmazonAdsTokenRefreshWorker
- Inherits:
-
Object
- Object
- AmazonAdsTokenRefreshWorker
- Includes:
- Sidekiq::Job
- Defined in:
- app/workers/amazon_ads_token_refresh_worker.rb
Overview
Proactively refreshes the system-level Amazon Ads OAuth token every
45 minutes (scheduled in config/sidekiq_production_schedule.yml).
Amazon LWA access tokens are short-lived (~1 hour) — the same as
Microsoft's and YouTube's, so this uses the same every-45-minute cadence.
AmazonAds::OauthService#access_token! still refreshes lazily on demand as
a safety net, but proactive refresh keeps the credential Active on the
admin dashboard and, more importantly, surfaces a dead grant
(invalid_grant: revoked consent, or a refresh token past the 365-day
limit that applies to consent granted after 2026-07-30) via
OauthCredential#record_refresh_failure! — alerting the connecting admin
to re-consent at /amazon_ads/oauth/authorize rather than waiting for the
nightly campaign sync to fail.
Instance Method Summary collapse
-
#perform ⇒ Object
Runs the job.
Instance Method Details
#perform ⇒ Object
Runs the job.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/workers/amazon_ads_token_refresh_worker.rb', line 26 def perform # Advisory lock guards against concurrent refreshes (scheduler overlap, # Sidekiq retry, manual enqueue). # timeout_seconds: 0 — if another run holds the lock, skip; it has it covered. OauthCredential.with_advisory_lock('amazon_ads_token_refresh', timeout_seconds: 0) do credentials = OauthCredential.where(provider: AmazonAds::OauthService::PROVIDER) if credentials.none? Rails.logger.info('[AmazonAdsTokenRefreshWorker] No Amazon Ads credential — nothing to refresh') next end credentials.find_each { |cred| refresh_credential(cred) } end end |