Class: MicrosoftAdsTokenRefreshWorker

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

Overview

Exercises the system-level Microsoft Advertising OAuth refresh token daily.

Microsoft access tokens are short-lived (~1 hour) and
MicrosoftAds::OauthService#access_token! refreshes them lazily on demand,
so — unlike PinterestTokenRefreshWorker — there is no access-token-expiry
window to chase. This worker instead refreshes the credential
unconditionally once a day to:

  1. keep the long-lived refresh token exercised (Microsoft may rotate it
    on use, and the new value is persisted by the OAuth service), and
  2. surface an invalidated grant (revoked consent, changed password,
    removed app permission) proactively via
    OauthCredential#record_refresh_failure! — alerting the connecting
    admin rather than waiting for the next campaign sync or conversion
    upload to fail.

Instance Method Summary collapse

Instance Method Details

#performObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/workers/microsoft_ads_token_refresh_worker.rb', line 25

def perform
  # Advisory lock guards against concurrent refreshes (scheduler overlap,
  # Sidekiq retry, manual enqueue). Microsoft may rotate the refresh token
  # on use, so two overlapping runs could invalidate each other.
  # timeout_seconds: 0 — if another run holds the lock, skip; it has it covered.
  OauthCredential.with_advisory_lock('microsoft_ads_token_refresh', timeout_seconds: 0) do
    credentials = OauthCredential.where(provider: MicrosoftAds::OauthService::PROVIDER)

    if credentials.none?
      Rails.logger.info('[MicrosoftAdsTokenRefreshWorker] No Microsoft Advertising credential — nothing to refresh')
      next
    end

    credentials.find_each { |cred| refresh_credential(cred) }
  end
end